Accessing dock icon right-click menu items with AppleScript

后端 未结 5 668
醉酒成梦
醉酒成梦 2020-12-16 23:39

Is there any way to get an AppleScript to access the menu items that come up when you right click on a dock icon?

Specifically, here\'s what I want to do:

I

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 00:14

    For anyone who is interested, I think I've got a reasonable working solution to this problem, but it doesn't involve right-clicking on dock icons.

    First, you have to uncheck "When switching to an application, switch to a space with open windows for the application" in the Spaces preference pane of System Preferences. Then I wrote the following AppleScript:

    tell application "Google Chrome" to activate
    
    tell application "System Events"
        tell process "Google Chrome"
            try
                set var to get name of window 1
            on error
                set var to "no window exists!!!"
            end try
        end tell
    end tell
    
    if var is "no window exists!!!" then
        tell application "System Events"
            tell process "Google Chrome"
                click menu item "New Window" of menu "File" of menu bar 1
            end tell
        end tell
    else
        tell application "System Events"
            tell process "Google Chrome"
                click menu item "New Tab" of menu "File" of menu bar 1
            end tell
        end tell
    end if
    

    I launch this AppleScript using Spark, which allows me to assign a shortcut key to it.

    It's a little bit slow, particularly when the system is under a load, but usually doesn't take longer than a second or so to run. It also avoids the problem I was having with Firefox, where I would end up with dozens of windows open at the end of the day.

提交回复
热议问题