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

发布于 2024-11-14 10:06:40 字数 165 浏览 0 评论 0原文

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

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

预先感谢!

I need to to get rid of that annoying dialog. How can I achieve that?

I'm trying to get an window title of the active application. Sometimes there are applications which has valid app name but when I try to insert it into the AppleScript script(for window title retrieving) that dialog is appeared. I need just to ignore those application without bothering an end-user with that dialog.

Thank in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-11-21 10:06:40

这有时是一个问题,因此我们可以通过应用程序的捆绑 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 一直以来的工作方式。苹果已尝试解决此问题,大多数程序不再显示“查找应用程序”窗口,但有些程序仍然显示。 iPhoto 是我注意到的一个例子。

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

set appName to "TextEdit"

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

That is sometimes an issue, therefore we can target applications by their bundle id, which will remove the confusion. Try it this way.

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

Here's a little script to help you find the bundle id of an application.

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

Finally, I'm not sure this will help your problem. If you have a "tell application whatever" line in your code, and the user does not have whatever application on their system, then it's likely that the dialog will launch anyway. That's the way applescript has always worked. Apple has tried to fix this issue and most programs no longer show the "find application" window however some still do. iPhoto is one example I've noticed.

It happens when you distribute your code as a script, because when a script opens it has to compile itself. During the compile phase applescript needs to ensure the code is correct and therefore to check the code the application's applescript dictionary must be checked... which means sometimes the application must be launched. However there is one possible solution. You must deliver your script pre-compiled which means you must distribute it as an application instead of a script. In addition you must use the following construct in the application. So in other words, you will pre-compile the script on your computer with the "using terms from" stuff so the user doesn't have to check the application's dictionary when they run it.

set appName to "TextEdit"

using terms from application "TextEdit"
    tell application appName
        -- do something
    end tell
end using terms from
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文