【问题标题】:Applescript halts at a line and then won't continue (telling a not running app to hide)Applescript 停在一行然后不会继续(告诉一个未运行的应用程序隐藏)
【发布时间】:2012-12-17 00:46:22
【问题描述】:

所以我在这里有一个非常基本的 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 个应用程序将全部退出。

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

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

    set myApps to {"iTunes", "1Password"}
    repeat with anApp in myApps
        try
            tell application anApp to quit
        end try
    end repeat
    

    【讨论】:

    • 完美!并带有一个循环,所以我可以简化!谢谢你。 :)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      提示:使用[增强型应用程序对象模型][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
      

      [How to check in AppleScript if an app is running, without launching it - via osascript utility]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多