Applescript 无法在 OSX 启动时运行

发布于 2024-11-10 07:29:25 字数 929 浏览 2 评论 0原文

我编写了 applescript 应用程序来隐藏 wifi 卡的窗口。 我在检查窗口是否可见时遇到了一些问题(以避免 command+h 按键不起作用),因此我决定使用 delay 15 来(完全不)确保弹出窗口。如果我从编辑器或双击应用程序文件运行脚本,它可以工作,但如果我将其设置为在用户登录时启动(在“设置”>“帐户”>“登录元素”下),它就不起作用! 我尝试更改 applescript 编辑器的 另存为... 页面中的复选框:我尝试了 仅执行 的两种设置,但一切都发生了变化。 使用开始屏幕选项实际上它可以工作,但它要求我确认,但我不想要它(我更喜欢按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 重新设置为(每个进程的名称)。现在代码是正确的。

I wrote my applescript app to hide the window of my wifi card.
I ran into some problem checking if the window is visible or not (to avoid the command+h keypress have no effect), so I decided to use a delay 15 to make (not at all) sure the window pops up. If I run the script from the editor or by double-click on the app file, it works, but if I set it to start at the login of the user (under Settings>Account>Login Elements), it doesn't work!
I tried to change the checkbox in the Save as... page of applescript editor: I tried both setting for only execute, but anything change.
With the start screen option in fact it works, but it ask me a confirmation and I don't want it (I'll prefere to press cmd+h instead).
Anyone can explain me why I have this issue?

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

EDIT: Now it seems to work! I forgot to re- set progList to (name of every process). Now the code is correct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-11-17 07:29:25

我看到你的代码正在运行。那太棒了。不过,我发布此内容是为了帮助您学习。我发现您的代码有几个小问题。例如,在重复循环中,您告诉系统事件击键“h”。无需告诉系统事件在该行中执行此操作,因为您已经位于系统事件告诉代码块中,因此系统事件已经知道要执行此操作。

这是我编写您的代码的方式。这不需要敲击键盘,这总是一件好事,而且效率更高一些。它之所以有效,是因为如果进程不存在,则会出现“set theProcess to”行错误,然后代码进入“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

I see your code is working. That's great. However I'm posting this to help you learn. I see a couple small issues with your code. For example in your repeat loop you tell system events to keystroke "h". There's no need to tell system events to do that in that line because you're already in a system events tell block of code, so system events already knows to do that.

Here's how I would write your code. This doesn't require keystrokes, which is always a good thing, and is a little more efficient. It works because if the process doesn't exist then the "set theProcess to" line errors, the code then goes into the "on error" section to delay 5, then the repeat loop tries to find the process again. If the process is found then it sets its visible property which is the same as hiding it.

It also has a timeout mechanism to prevent the script from running forever. Use this if you like. Good luck.

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: I have checked the above code using the TextEdit application and it works fine. To check it with your application run the following. Make sure the application is running when you run this code. If there's an error this will display it. If there's no error 2 dialogs will display showing the progress. Report what you find.

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
戏舞 2024-11-17 07:29:25

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

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

I have used the Login Items to launch an AppleScript applet at login with success, so my first suggestion is to make sure that it's not running. Have it show a custom dialog or beep or something like that to confirm whether it's running or not.

Other than that, I'm not sure what suggestion to offer unless you'd like to post the code you're executing in the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文