如何在不启动应用程序的情况下检查 AppleScript 是否正在运行 - 通过 osascript 实用程序

2024-01-08

考虑以下 AppleScript:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
if safRunning then
    tell application "Safari"
      -- Stuff I only want executed if Safari is running goes here.
    end tell
    return "Running"
else
    return "Not running"
end if

问题:当我通过osascript命令行实用程序,如果 Safari 未运行,它将启动并且脚本报告“正在运行”。这不是我想要或期望的行为。请注意,当在 AppleScript 编辑器中运行时,它会按预期/预期工作。

这是一个osascript错误/已知问题?或者这是出于我所缺少的原因而有意为之的行为?任何人都可以让它按预期工作吗? (顺便说一句,我正在运行 OSX 10.7.5;我不知道如何获取osascript报告版本号)。

如果你注释掉tell / end tell行,它的行为正如我所期望的:如果 Safari 没有运行,它不会启动它,并打印“未运行”。所以seems对我来说就像tell是什么导致 Safari 启动,但它不需要实际执行,只存在于脚本中......?有一段时间我想知道这是否就是这样tell应该可以工作,但因为它doesn't在 AppleScript 编辑器中像这样工作,我想不是......

事实上,这是另一个更疯狂的版本,具有类似的行为:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
return safRunning
if false then
    tell application "Safari"
    end tell
end if

这仍然总是启动 Safari,尽管tell是在一个if falsereturn 语句之后的块! (不过,这在 AppleScript 编辑器中没问题。)

顺便说一句,这种行为不仅限于 Safari,但也不是通用的:

  • 受影响的应用程序包括:Safari、TextEdit、iPhoto、AppleScript Editor、iTerm...
  • 不受影响的应用程序包括:Google Chrome、iTunes、Preview、Mail、Terminal、Address Book、Echofon、...

那么,有人对我如何解决或解决这个问题有任何想法吗?是不是一个osascript漏洞?或者我是否遗漏了 AppleScript 语义的某些内容?

对于上下文:我正在尝试编写一个脚本(从某些 python 嵌入/调用),该脚本查询打开的浏览器以获取它们打开的任何选项卡的 URL;我一切正常except它总是启动 Safari,无论它是否打开。我已将这种不良行为归结为上面所示的简单测试用例。我不知道有什么方法可以在不使用 python 的情况下运行这个脚本osascript, 以外应用脚本 http://appscript.sourceforge.net/,我不想使用它,因为不再开发/支持/推荐 http://appscript.sourceforge.net/status.html.

非常感谢您的所有投入/见解!


我怀疑您收到此消息的原因是因为每次您使用 osascript 从命令行调用脚本时,都会编译脚本。

编译的行为告诉应用程序据我所知会使应用程序启动。

使用预编译文件中的 osascript 从命令行调用脚本,即scpt不会导致此行为,因为无需进行编译。

但是从纯文本(.txt、.sh)文件调用它将会启动应用程序。

如果您不想使用 .scpt 文件并想使用纯文本文件,那么您可以尝试将运行脚本applescript 中的命令。

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
if safRunning then
    run script "tell application \"Safari\" 
    
open location \"http://google.com\"
     
    end tell"
    return "Running"
else
    return "Not running"
end if

该脚本在运行脚本仅在需要时才编译。您需要转义任何字符,例如我的示例中的引号。

如果您首先在普通的 applescript 文档中编写脚本并编译它以检查错误,则会更容易。

然后将其复制到纯文本文件中。


UPDATE **

我上面使用的方法来自我在回答这里之前一段时间用来解决这个问题的旧脚本。

答案是有效的,并且并不是试图变得优雅。 ;-)

我其实喜欢用户1804762 method https://stackoverflow.com/a/16069999/1107226以下。因为它确实有效,但感觉答案不够清楚,所以我将给出一个使用它的例子。

set appName to "Safari"

if application appName is running then
    
    tell application id (id of application appName)
        
        open location "http://google.com"
    end tell
    return "Running"
else
    return "Not running"
end if

该脚本可以从命令行运行奥萨脚本

example:

osascript /Users/USERNAME/Desktop/foo.scpt

请注意,该脚本被保存为已编译的脚本。这将正常工作,您也可以将其保存并用作纯文本脚本。

i.e.

osascript /Users/USERNAME/Desktop/foo.applescript

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在不启动应用程序的情况下检查 AppleScript 是否正在运行 - 通过 osascript 实用程序 的相关文章

随机推荐