Applescript - 将窗口置于前台

19

我有一个应用程序,同时打开了几个窗口。我想将特定的窗口置于前台(我知道它的标题)。

目前,我正在使用组合键来完成此任务,但由于这种方法存在一些问题,我想尝试一些不同的方法。

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

30

通过使用"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

3
在OS X Yosemite上,执行此脚本时我遇到错误(无法直接复制粘贴,因为我收到了本地化的错误信息):“System Events got error: Unable to configure process "appIT" to true.” 是否自2012年以来有所改变?请帮我翻译。 - Anton Koval'
1
@AntonKoval 在 Yosemite 上执行窗口 1 的 "AXRaise" 操作对我有效。 - ling
适用于Catalina。应该是被接受的答案。 - LiMar

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

切换窗口可见性是为了解决应用程序切换时发生的某些奇怪问题。如果您不切换可见性,窗口在从应用程序切换出去再切回来时将不会是第一个显示的窗口。不幸的是,这种可见性切换会使窗口缩小到 Dock,然后恢复,非常明显的界面干扰。

我找到了另一种处理这个问题的方法:

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

-1

我认为系统事件无法更改进程的前置窗口。当然,您可以关闭前置窗口,直到您想要的窗口在顶部。但这并不是真正的解决方案,因为您可能不想关闭窗口。实际上,您唯一能够实现这一点的方法是,如果应用程序本身是可通过Apple脚本控制的,并允许您执行此操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接