在AppleScript中找不到应用程序时,请删除“选择应用程序”对话框

时间:2011-06-08 08:00:14

标签: applescript

我需要摆脱烦人的对话。我怎样才能做到这一点?

我正在尝试获取活动应用程序的窗口标题。有时应用程序具有有效的应用程序名称,但当我尝试将其插入AppleScript脚本(用于窗口标题检索)时,会出现该对话框。我只需要忽略这些应用程序,而不必通过该对话框来打扰最终用户。

提前感谢!

1 个答案:

答案 0 :(得分:2)

这有时是一个问题,因此我们可以通过其捆绑ID来定位应用程序,这将消除混淆。试试这种方式。

set bundleName to "com.apple.TextEdit"

-- find out if the application is running
set appIsRunning to false
tell application "System Events"
    try
        first process whose bundle identifier is bundleName
        set appIsRunning to true
    end try
end tell

if appIsRunning then
    tell application id bundleName
        -- do something
    end tell
end if

这是一个小脚本,可以帮助您找到应用程序的软件包ID。

try
    tell application "Finder" to set bundleID to id of (choose file)
on error
    return "The chosen file is not an application"
end try
return bundleID

最后,我不确定这会对你的问题有所帮助。如果您的代码中有“告诉应用程序无论什么”行,并且用户的系统上没有任何应用程序,那么对话框很可能无论如何都会启动。这就是applescript一直有效的方式。 Apple试图解决这个问题,大多数程序不再显示“查找应用程序”窗口,但有些人仍然这样做。 iPhoto就是我注意到的一个例子。

当您将代码作为脚本分发时会发生这种情况,因为当脚本打开时,它必须自行编译。在编译阶段,applescript需要确保代码是正确的,因此要检查代码必须检查应用程序的applescript字典...这意味着有时必须启动应用程序。但是有一种可能的解决方案。您必须提供预编译的脚本,这意味着您必须将其作为应用程序而不是脚本进行分发。此外,您必须在应用程序中使用以下构造。换句话说,您将使用“使用术语”来预编译计算机上的脚本,这样用户在运行时无需检查应用程序的字典。

set appName to "TextEdit"

using terms from application "TextEdit"
    tell application appName
        -- do something
    end tell
end using terms from