当在AppleScript中找不到应用程序时,摆脱“选择应用程序”对话框

问题描述:

我需要摆脱那个讨厌的对话框。我怎样才能做到这一点?当在AppleScript中找不到应用程序时,摆脱“选择应用程序”对话框

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

提前致谢!

这有时是一个问题,因此我们可以通过它们的捆绑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 

最后,我不确定这会帮助您解决问题。如果你的代码中有“tell application whatever”这一行,并且用户在他们的系统上没有任何应用程序,那么对话框很可能会启动。这就是applescript一直工作的方式。苹果试图解决这个问题,大多数程序不再显示“查找应用程序”窗口,但仍有一些仍在使用。 iPhoto是我注意到的一个例子。

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

set appName to "TextEdit" 

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

谢谢! “告诉应用程序ID包名”实际上是我搜​​索的内容。我的Cocoa部分有捆绑ID。 – ixSci 2011-06-08 14:56:48

+0

请阅读我可能会对您造成影响的其他文章。我在帖子中添加了新内容。 – regulus6633 2011-06-08 14:58:10

+0

请注意,当前的AppleScript编辑器的脚本默认文件格式 - “* .scpt” - 也被预编译为_。另外,我认为它不会影响(至少在Mountain Lion上)是否使用''使用术语'',只要您运行预编译的脚本(无论是带有'osascript'的* .scpt'文件或者直接使用'* .app'文件),你应该没问题,因为对话只会在_compilation_期间弹出。 – mklement0 2012-08-31 21:13:50