Applescript不';不要在OSX启动上工作

Applescript不';不要在OSX启动上工作,applescript,startup,Applescript,Startup,我编写了applescript应用程序来隐藏wifi卡的窗口。 我在检查窗口是否可见时遇到了一些问题(以避免命令+h键无效),因此我决定使用delay 15来确保(根本没有)窗口弹出。如果我从编辑器中运行脚本或双击应用程序文件,它会工作,但如果我将其设置为从用户登录时开始(在“设置”>“帐户”>“登录元素”下),它就不工作了! 我试图更改applescript编辑器的另存为…页面中的复选框:我尝试了的两个设置,仅执行,但没有任何更改。 使用start screen选项,它实际上可以工作,但它要求

我编写了applescript应用程序来隐藏wifi卡的窗口。 我在检查窗口是否可见时遇到了一些问题(以避免命令+h键无效),因此我决定使用
delay 15
来确保(根本没有)窗口弹出。如果我从编辑器中运行脚本或双击应用程序文件,它会工作,但如果我将其设置为从用户登录时开始(在“设置”>“帐户”>“登录元素”下),它就不工作了! 我试图更改applescript编辑器的
另存为…
页面中的复选框:我尝试了
的两个设置,仅执行
,但没有任何更改。
使用
start screen
选项,它实际上可以工作,但它要求我确认,我不想要它(我更喜欢按cmd+h)。 谁能解释我为什么会有这个问题

tell application "System Events"
set progList to (name of every process)
set cond to false
repeat while cond is false
    if (progList contains "WirelessUtilityCardbusPCI") is true then
        delay 5
        activate application "WirelessUtilityCardbusPCI.app"
        tell application "System Events" to keystroke "h" using [command down]
        set cond to true
    else
        delay 5
        set progList to (name of every process)
    end if
end repeat
end tell

编辑:现在它似乎起作用了!我忘了将progList重新设置为(每个进程的名称)
。现在代码是正确的。

我已经使用登录项在成功登录时启动了AppleScript小程序,因此我的第一个建议是确保它没有运行。让它显示一个自定义对话框或嘟嘟声或类似的东西,以确认它是否正在运行


除此之外,我不确定该提供什么建议,除非您想在脚本中发布正在执行的代码。

我看到您的代码正在工作。太好了。然而,我发布这篇文章是为了帮助你学习。我发现你的代码有几个小问题。例如,在重复循环中,告诉系统事件击键“h”。无需告诉系统事件在该行中执行此操作,因为您已经在一个系统事件通知代码块中,所以系统事件已经知道要执行此操作

下面是我将如何编写您的代码。这不需要按键,这总是一件好事,而且效率更高。这是因为如果进程不存在,则“将进程设置为”行错误,然后代码进入“on error”部分延迟5,然后重复循环尝试再次查找进程。如果找到该进程,则它将设置其可见属性,这与隐藏该进程相同

它还有一个超时机制来防止脚本永远运行。如果你喜欢,就用这个。祝你好运

set processName to "WirelessUtilityCardbusPCI"
set maxTime to 180 -- we only check for 3 minutes, then end

set inTime to current date
repeat
    try
        tell application "System Events"
            set theProcess to first process whose name is processName
            set visible of theProcess to false
        end tell
        exit repeat
    on error
        if (current date) - inTime is greater than maxTime then
            tell me
                activate
                display dialog "The process " & processName & " could not be found!" buttons {"OK"} default button 1 with icon 0
            end tell
            exit repeat
        end if
        delay 5
    end try
end repeat
编辑:我已经使用TextEdit应用程序检查了上述代码,它工作正常。要在应用程序中检查它,请运行以下命令。运行此代码时,请确保应用程序正在运行。如果出现错误,将显示它。如果没有错误,将显示2个对话框,显示进度。报告你的发现

set processName to "WirelessUtilityCardbusPCI"

try
    tell application "System Events"
        set theProcess to first process whose name is processName
        display dialog "I have found the process"
        set visible of theProcess to false
        display dialog "I just performed the \"set visible\" code"
    end tell
on error theError number errorNumber
    tell me
        activate
        display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
        return
    end tell
end try

很抱歉,但我不认为代码可以改变某些东西。“第一个”到底要做什么?您的代码非常有趣,但它不起作用:它终止时没有错误对话框,也没有隐藏应用程序。我认为这个过程在“设置可见”方面有一些问题,但这次它没有给我任何错误(与我以前的尝试不同)!我在上面的编辑部分添加了一些代码来帮助您找到问题。请尝试并报告。我不确定原因,但“设置可见”对我的应用程序不起作用!第二个对话框出现,但应用程序仍然可见。我还有另一个问题:如果我查找进程,有时我的应用程序会在窗口尚未绘制时运行。通过这种方式,cmd+h按键将不执行任何操作,脚本将终止而不生效。