Accessing dock icon right-click menu items with AppleScript
Asked Answered
D

5

10

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 use Google Chrome as my web browser on MacOS X Snow Leopard. I am a keyboard shortcut addict, and I use QuickSilver to create keyboard shortcuts for anything I can. One of the most common things that I do is to open a new web browser window. But I use Spaces frequently to partition my tasks that I am currently working on, and when I open a web browser or web page with a QuickSilver trigger, spaces switches to the last space that I used Chrome on and opens a new tab, which often distracts me for hours because it brings me to a different space and thus a different task. I can fix this by right-clicking on the Google Chrome icon and clicking the "New Window" option, which opens a new window on the current space. But in AppleScript, to do anything with Google Chrome, the first thing I have to do is activate it, which brings me back to the original problem! Can anyone think of a solution to this problem, AppleScript or otherwise? It has become a serious problem. Back when I used Firefox, I solved the problem by changing a preference item that says "Always open pop-up links in a new window" or something like that, which was kind of a sledge hammer approach, but it worked. I can always go back to Firefox, but I thought I'd ask my question here first. Anyone with any ideas?

Dedra answered 12/5, 2010 at 14:39 Comment(1)
Just found this while looking for right-click applescript examples - for exactly the same reason as you.Cassilda
D
3

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.

Dedra answered 26/8, 2010 at 14:20 Comment(1)
I had never seen that option on the pref pane before (hidden down the bottom) - so cool.Cassilda
K
19

Not sure if you are still interested but...

 tell application "Dock"
    activate
end tell
tell application "System Events"
    tell process "Dock"
        set frontmost to true
        activate
        tell list 1
            perform action "AXShowMenu" of UI element "Google Chrome"
            delay 1
            repeat 4 times -- count number of items to the one you want
                key code 126 -- up arrow
                -- key code 125 -- down arrow
            end repeat
            delay 1
            repeat 2 times
                key code 36 -- return key
            end repeat
        end tell
    end tell
end tell
Karelia answered 26/1, 2011 at 17:9 Comment(5)
actually, quite helpful. ThanksDedra
no problem. I had to do something similar for work and when I was researching it I came across your question but I wasn't sure if you would still be watching it.Karelia
Nice! This helped me control Wimp, an Adobe Air application which won't export its menu items to Quicksilver unless it is the frontmost application.Axe
Thanks for sharing this solution. Just what I needed.Ecclesiastes
The first three lines gave me an error: execution error: Dock got an error: Can’t continue activate. (-1708). So I removed them and the rest works well!Sententious
P
5

As a variation to the most upvoted answer, here's a version of the script that accepts the name of the menu item, which lets you avoid having to do any counting.

This example clicks "Go to Folder..." from Finder's dock menu.

tell application "Dock"
    activate
end tell
tell application "System Events"
    tell process "Dock"
        set frontmost to true
        activate
        tell UI element "Finder" of list 1
            perform action "AXShowMenu"
            click menu item "Go to Folder…" of menu "Finder"
        end tell
    end tell
end tell
Prizewinner answered 16/4, 2020 at 0:45 Comment(1)
This is great! If we're about small improvements, here's another one. This script causes the current active window to lose focus. To restore it, under tell application "System Events" add as the following as the first line: set ActiveApp to item 1 of (get name of processes whose frontmost is true). Then at the end of the file (outside any tell) add these three lines: tell application ActiveApp↵ activate↵ end tell.Sententious
D
3

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.

Dedra answered 26/8, 2010 at 14:20 Comment(1)
I had never seen that option on the pref pane before (hidden down the bottom) - so cool.Cassilda
C
3

The Chromium nightly builds now contain AppleScript support, and this should make it into Chrome before long. It means you can now do:

tell application "Chromium"
    make new window
    activate
end tell
Cassilda answered 27/8, 2010 at 0:23 Comment(1)
thanks for the heads up! Hope this will make it into the stable version soon; I'm not much for nightlies on something as fundamental as a web browser.Dedra
B
0

Alternatively you could also invoke this hidden option:

defaults write com.apple.dock workspaces-auto-swoosh -bool NO
killall Dock

It also has the, for me, positive effect that you aren't thrown around on desktops anymore when switching applications. Simply do Cmd+N with Chrome as the active app.

Btw you can go to spaces directly with Fn+1, Fn+2 etc. if you set it up instead of ⌃1, ⌃2 etc. You have to make the spaces before the keyboard shortcuts can be set in Keyboard -> Shortcuts preferences.

Buckeen answered 29/5, 2019 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.