【问题标题】:AppleScript -> Activate window of a non-scriptable applicationAppleScript -> 激活不可编写脚本的应用程序的窗口
【发布时间】:2010-09-07 03:15:00
【问题描述】:

我打开了2个“Finder”窗口A&B,A在前面,B在下面,下面的sn-p把B带到最前面:

tell application "Finder"
    activate
    activate window 2
end tell

但对于不支持脚本的应用程序,刚才提到的代码将无济于事。

关于激活非脚本应用程序窗口的任何想法。

【问题讨论】:

    标签: applescript


    【解决方案1】:

    在这些情况下,您通常可以求助于系统事件。系统事件知道正在运行的进程的窗口,您通常可以操纵这些窗口。这样的事情会告诉你一些你可以做的事情。随便玩弄代码,看看你能不能做你想做的事。

    tell application "System Events"
        tell process "Whatever"
            properties of windows
        end tell
    end tell
    

    编辑: 窗口的属性之一是它的“标题”。所以你也许可以使用它。这种方法使用的事实是,许多应用程序都有一个“窗口”菜单,并且在该菜单下多次列出窗口的名称,您可以通过单击适当的菜单项来切换窗口。所以这样的事情可能会起作用......我的示例使用 TextEdit。

    tell application "TextEdit" to activate
    tell application "System Events"
        tell process "TextEdit"
            set windowTitle to title of window 2
            click menu item windowTitle of menu 1 of menu bar item "Window" of menu bar 1
        end tell
    end tell
    

    【讨论】:

    • 感谢 regulus6633,但在分别为“Finder”和另一个应用程序运行代码时,我看不出结果有什么不同。
    • 我在帖子中添加了另一种方法。也许会有所帮助。
    • 兴奋,您的方法确实有效,至少对我的应用程序而言!非常感谢!
    • 太棒了。很高兴您找到了解决方案。
    • 这仅适用于标题很短并且在窗口和菜单上看起来相同的情况。换句话说,它不适用于 Safari 之类的东西。
    【解决方案2】:

    您对不可编写脚本的定义是什么?几乎所有内容在某种程度上都是可编写脚本的,但为了举例说明,我们可以使用,不包含 AppleScript dictionary,例如AppName.sdef 在其应用程序包中。

    例如,ma​​cOS 包含的 Stickies 应用程序 不包含 Stickies.sdef 文件,当尝试将其添加到 Script Editor 中的 Library 时,会显示“无法添加该项目,因为它不可编写脚本。”

    在这种情况下,则需要系统事件应用程序进程对话,例如:

    示例 AppleScript 代码

    if running of application "Stickies" then
        tell application "System Events"
            tell application process "Stickies"
                set frontmost to true
                if exists window 2 then ¬
                    perform action "AXRaise" of window 2
            end tell
        end tell
    end if
    

    注意事项:

    我在 示例 AppleScript 代码 中包含了错误处理,如果您愿意,可以将其删除.

    【讨论】:

      猜你喜欢
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多