【问题标题】:Applescript - Bring window to foregroundApplescript - 将窗口置于前台
【发布时间】:2012-10-21 19:55:29
【问题描述】:

我有一个同时打开多个窗口的应用程序。 我想把一个特定的窗口带到前台(我知道它的标题)。

目前我正在使用组合键来完成此任务,但我想尝试一些不同的方法,因为我在使用这种方法时遇到了一些问题。

tell application "System Events"
    set frontmost of process "appIT" to true
    keystroke "1" using command down
    delay 0.2
end tell

【问题讨论】:

    标签: macos window applescript foreground


    【解决方案1】:

    这可以通过使用"AXRaise" 操作来实现,但在某些窗口(例如使用 X11 的应用程序)除外。

    试试这个。

    set theTitle to "some title"
    tell application "System Events"
        tell process "appIT"
            set frontmost to true
            perform action "AXRaise" of (windows whose title is theTitle)
        end tell
    end tell
    

    【讨论】:

    • 在 OS X Yosemite 上,执行此脚本给了我错误(不能只是复制粘贴,因为我有本地化错误消息):系统事件出错:无法将进程“appIT”配置为真。自 2012 年以来发生了什么变化?
    • @AntonKoval' 执行窗口 1 的操作“AXRaise”在优胜美地为我工作
    • 适用于 Catalina。应该是公认的答案。
    【解决方案2】:

    如果您的应用程序可编写脚本并允许设置窗口索引,您可以执行以下操作(基于How do I make a Safari window active using AppleScript (elegantly)? 中的答案)

    to raiseWindow of theApplicationName for theName
        tell the application named theApplicationName
            activate
        set theWindow to the first item of ¬
            (get the windows whose name is theName)
        if index of theWindow is not 1 then
                set index to 1
                set visible to false
                set visible to true
            end if
        end tell
    end raiseWindow
    

    可见性的切换对于处理切换应用程序时出现的一些奇怪现象是必要的。如果您不切换可见性,则当您从应用程序切换回应用程序时,该窗口将不会是第一个。不幸的是,这种切换将窗口缩小到停靠栏,然后将其恢复,这是一个非常戏剧性的 UI 中断。

    这是我发现的另一种处理怪异的方法:

    to raiseWindow2 of theApplicationName for theName
        tell the application named theApplicationName
            activate
        set theWindow to the first item of ¬
            (get the windows whose name is theName)
            if the index of theWindow is not 1 then
                set the index of theWindow to 2
            tell application "System Events" to ¬
                tell application process theApplicationName to ¬
                    keystroke "`" using command down
            end if
        end tell
    end raiseWindow2
    

    【讨论】:

      【解决方案3】:

      我不认为系统事件可以改变进程的前窗口。当然,您可以关闭前窗口,直到您想要的窗口位于顶部。尽管您可能不想关闭窗口,但这并不是真正的解决方案。确实,您可以实现此目的的唯一方法是应用程序本身是可编写苹果脚本的并允许您执行此操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-16
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多