Applescript在OSX启动时不起作用



我写了我的applescriptpt应用程序来隐藏我的wifi卡窗口。我在检查窗口是否可见时遇到了一些问题(为了避免命令+h键无效),所以我决定使用delay 15来确保(根本不)窗口弹出。如果我从编辑器或双击应用程序文件运行脚本,它是有效的,但如果我将其设置为从用户登录时开始(在"设置">"帐户">"登录元素"下),它就不起作用了!我试图更改appscripteditor的Save as...页面中的复选框:我尝试了only execute的两个设置,但都有变化。事实上,使用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

编辑:现在它似乎起作用了!我忘了复习set progList to (name of every process)。现在代码是正确的。

我看到您的代码正在工作。太好了。然而,我发布这篇文章是为了帮助你学习。我看到你的代码有几个小问题。例如,在重复循环中,您告诉系统事件按"h"键。没有必要在这行中告诉系统事件这样做,因为您已经在一个系统事件告诉代码块中了,所以系统事件已经知道要这样做了。

以下是我如何编写您的代码。这不需要按键,这总是一件好事,而且效率更高。它之所以有效,是因为如果进程不存在,那么"将进程设置为"行错误,然后代码进入"出错"部分延迟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

EDIT:我已经使用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

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

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

最新更新