【发布时间】:2014-10-16 18:28:05
【问题描述】:
这是我的停靠菜单:
我想以编程方式单击“显示最近的窗口”按钮。这可以使用 Cocoa 或 CoreFoundation 以编程方式完成吗?
我知道停靠项关联的 PID。
【问题讨论】:
标签: macos cocoa macos-carbon core-foundation
这是我的停靠菜单:
我想以编程方式单击“显示最近的窗口”按钮。这可以使用 Cocoa 或 CoreFoundation 以编程方式完成吗?
我知道停靠项关联的 PID。
【问题讨论】:
标签: macos cocoa macos-carbon core-foundation
有很多方法可以实现这一点,尽管通常您可以轻松设置一个可以处理此问题的AppleScript 或oascript。基本上它涉及使用AXRaise,它本质上调用函数来提升指定应用程序的最前面的窗口。
代码:
set mostrecentWindow to "mostrecentWindow"
set theApplication to "Safari"
tell application "System Events"
if exists process theApplication then
--raise frontmost window
tell process theApplication
set frontmost to true
perform action "AXRaise" of (windows whose title is mostrecentWindow)
end tell
else if not (exists process theApplication) then
--display alert
display alert "Warning: process " & theApplication & " is not running"
end if
end tell
上面的例子检查 Safari 进程是否正在运行,如果是,则将其最近(或最前面)的窗口提升到前台;否则显示进程未运行的警告。
【讨论】:
AXRaise 在窗口被最小化或位于其他可能有焦点的窗口后面时很有用。此外,您可以更好地控制要控制的窗口。您发布的该示例中列出的所有脚本都需要启用辅助设备。设置窗口的标题实际上或多或少只是碰巧是任何窗口的别名。如果你知道它是什么,你可以通过它的名字专门调用一个特定的窗口。
tell (1st window whose value of attribute "AXMain" is true) *(linebreak)* set theWindow to value of attribute "AXTitle" *(linebreak)* end tell
这听起来像是可以使用GUI Scripting 使用AppleScript 来完成的任务。
【讨论】: