0

所以我在这里有一个非常基本的 Applescript,它基本上隐藏了我所有与社交网络相关的应用程序,然后还有一个类似的应用程序可以取消隐藏它们。问题是,如果其中一个应用程序没有运行,那么整个应用程序都会在该行停止,并且不会将该行下方的应用程序设置为隐藏。

这是脚本:

tell application "System Events" to set visible of process "Twitter" to false
tell application "System Events" to set visible of process "Wedge" to false
tell application "System Events" to set visible of process "Facebook" to false
tell application "System Events" to set visible of process "Adium" to false
tell application "System Events" to set visible of process "Messages" to false

我这样做是错误的吗?我是否必须检查它们中的每一个是否首先运行,或者是否有某种方法可以让它继续执行脚本?

编辑:使用此脚本体验同样的事情,我使用 Alfred 热键退出特定应用程序并在我下班时启动屏幕保护程序:

tell application "Adium" to quit
tell application "Messages" to quit
tell application "1Password" to quit
tell application "iTunes" to quit
tell application "ScreenSaverEngine" to activate

所以基本上如果 iTunes - 例如 - 没有运行,那么屏幕保护程序将不会启动,但该行之前的 3 个应用程序将全部退出。

4

3 回答 3

1

您可以将每个语句包装在 try 块中,这样它就会静默失败:

set myApps to {"iTunes", "1Password"}
repeat with anApp in myApps
    try
        tell application anApp to quit
    end try
end repeat
于 2013-01-02T16:52:15.220 回答
1

提示:使用 [Enhanced Application Object Model][1] 您还可以执行以下操作:

if application "iTunes" is running then
    tell application "iTunes" to quit
end if

或者

tell application "iTunes"
    if it is running then
        pause
    end if
end tell

[如果应用程序正在运行,如何在不启动它的情况下检查 AppleScript - 通过 osascript 实用程序]

于 2013-01-04T10:01:49.547 回答
1

您可以使用上面指定的 try 命令并使用on error do nothing它来执行此操作。

于 2013-01-06T07:17:46.947 回答