18

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

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

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

3 回答 3

26

这可以通过使用"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
于 2012-11-01T12:58:28.530 回答
13

如果您的应用程序是可编写脚本的并且允许设置窗口的索引,您可以执行以下操作(基于如何使用 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

可见性的切换对于处理切换应用程序时发生的一些奇怪现象是必要的。如果您不切换可见性,则当您从应用程序切换回应用程序时,该窗口将不会是第一个。不幸的是,这种切换将窗口缩小到停靠栏,然后将其恢复,这是一个非常戏剧性的 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
于 2014-07-08T16:21:00.800 回答
-1

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

于 2012-11-01T12:13:06.120 回答