Applescript - 将窗口带到前景

Gio*_*gio 11 macos applescript window foreground

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

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

tell application "System Events"
    set frontmost of process "appIT" to true
    keystroke "1" using command down
    delay 0.2
end tell
Run Code Online (Sandbox Code Playgroud)

jac*_*300 22

"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
Run Code Online (Sandbox Code Playgroud)

  • 在 OS X Yosemite 上,执行这个脚本给了我错误(不能只是复制粘贴,因为我有本地化的错误消息):系统事件得到错误:无法将进程“appIT”配置为真。自 2012 年以来发生了什么变化? (3认同)

Mit*_*del 8

如果您的应用程序是可编写脚本的并且允许设置窗口的索引,则可以执行以下操作(基于如何使用AppleScript(优雅地)使Safari窗口处于活动状态的答案?)

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
Run Code Online (Sandbox Code Playgroud)

切换可见性对于处理切换应用程序时出现的一些奇怪现象是必要的.如果不切换可见性,则当您切换回应用程序时,窗口将不是第一个.不幸的是,这种切换使窗口缩小到底座然后恢复它,一个非常戏剧性的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
Run Code Online (Sandbox Code Playgroud)