views:

237

answers:

2

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?

+1  A: 

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.

David Hollman
I had never seen that option on the pref pane before (hidden down the bottom) - so cool.
Gavin Brock
+1  A: 

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
Gavin Brock
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.
David Hollman