【问题标题】:Get rid of "Choose application" dialog when app couldn't be found in AppleScript在 AppleScript 中找不到应用程序时摆脱“选择应用程序”对话框
【发布时间】:2011-06-08 08:00:14
【问题描述】:

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

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

提前致谢!

【问题讨论】:

    标签: applescript


    【解决方案1】:

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

    【讨论】:

    • 谢谢! “告诉应用程序 id bundleName”实际上是我搜​​索的内容。我的 Cocoa 部分确实有 bundle id。
    • 请阅读我的其余帖子,这也可能对您产生影响。我在帖子中添加了新内容。
    • 请注意,当前 AppleScript 编辑器的默认脚本文件格式 - *.scpt - 也是预编译的。另外,我认为无论您是否使用using terms from,我认为它没有区别(至少在Mountain Lion) - 只要您运行预编译脚本(无论是带有osascript*.scpt 文件还是@987654328 @文件直接),你应该没问题,因为对话框只在编译期间弹出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    相关资源
    最近更新 更多