简体   繁体   English

OS X 以编程方式单击 Dock 菜单的菜单项

[英]OS X Programmatically Click Menu Item of Dock Menu

This is my dock menu:这是我的停靠菜单:

I would like to programmatically click that button "Show Most Recent Window".我想以编程方式单击该按钮“显示最近的窗口”。 Can this programmatically be done using Cocoa or CoreFoundation?这可以使用 Cocoa 或 CoreFoundation 以编程方式完成吗?

I know the PID the dock item is associated with.我知道停靠项所关联的 PID。

There are many ways to achieve this, although generally you can easily set an AppleScript or oascript that can handle this.有很多方法可以实现这一点,尽管通常您可以轻松设置可以处理此问题的AppleScriptoascript Basically it involves using AXRaise , which essentially calls on the function to raise the frontmost window of the application specified.基本上它涉及使用AXRaise ,它本质上调用函数来提升指定应用程序的最前面的窗口。

Code:代码:

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

The above example checks whether or not the process Safari is running, and if it is then raise it's most recent (or frontmost) window to the foreground;上面的示例检查进程 Safari 是否正在运行,如果是,则将其最近(或最前面)的窗口提升到前台; otherwise show a warning that the process is not running.否则显示进程未运行的警告。

这听起来像是可以使用AppleScript使用GUI 脚本来完成的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM