MacOS – How to make clicking on icon in dock == open new instance

dockmacossettings

I have several applications in my dock that can have multiple instances open (eg. Chrome, iTerm, Sublime, etc.)

When I click on one of the icons, I want a new window to open (this is what happens if no windows are currently open) However, if an instance is already open it just takes me to that instance.

This gets especially annoying when the instance is on a whole other desktop instance and cause my desktop to change.

Is there a setting you can set to make it so when you click the icon, it opens a new window instead of redirecting you to an already opened window?

Best Answer

You can kludge something together with some AppleScript and some clever icon image pasting. Here's how you'd do it for iTerm/iTerm2.

Open the AppleScript Editor and paste in the following AppleScript:

set myapp to "iTerm"
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
    # What you do here will depend on the program. This works for
    # iTerm but you'll need to look up the actions for other programs
    # since this bit is iTerm-specific.
    tell application "iTerm"
        set myterm to (make new terminal)
        tell myterm
            launch session "Default"
        end tell
    end tell
else
    tell application myapp
        activate
    end tell
end if

You can use the Run button in the editor to test that it works. If you want a different profile opened change "Default" on line 12 to the name of the profile you want opened.

Now save this AppleScript. In the Save dialog under "File Format" at the bottom select "Application" from the list.

I saved mine as "New iTerm.app" to my Desktop.

Now every time I click on "New iTerm.app" on my Desktop I get a new iTerm window. I can drag "New iTerm.app" to my Dock and it'll be a Dock icon I can click on any time I want to get a new iTerm window open. But the icon is the generic AppleScript icon.

Let's change it to the iTerm icon. Select "New iTerm.app" on the Desktop and click Cmd-I to bring up the properties screen for the application.

Now navigate to /Applications in a Finder window, find iTerm in the list of installed applications, select it and click Cmd-I to open the properties screen for the iTerm application.

Click the icon in the iTerm info window so it gets a soft blue shadow around it. Press Cmd-C to copy the icon to the clipboard.

Now click the icon in the "New iTerm.app" info window and press Cmd-V to paste the iTerm icon on to the "New iTerm.app" application.

Copying the icon from iTerm to New iTerm

It should now have an iTerm icon.

You can move the "New iTerm.app" to your /Applications folder if you like. Drag it from the folder you decide to keep it in, back to your dock, and you can now click on it in the Dock to open up a new iTerm window with every click.

If you wanted to be able to distinguish it from the running iTerm.app Dock icon you could consider pasting the icon in to an image editor like Pixelmator and adding a big red + to it so it's clearly identified as the icon to press for a new iTerm window instead of the icon to press to see the exiting, open iTerm application.

Here are some more scripts for some other applications to get you started customizing things:

Google Chrome (New Window)

set myapp to "Google Chrome"
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
    tell application "Google Chrome"
        make new window
    end tell
else
    tell application myapp
        activate
    end tell
end if

Google Chrome (New Tab)

set myapp to "Google Chrome"
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
    tell application "Google Chrome"
        make new tab at end of tabs of window 1
    end tell
else
    tell application myapp
        activate
    end tell
end if

Sublime Text 2

Unfortunately ST2 doesn't seem to have an AppleScript accessible extensions. You can always take a look at the OS X command line tool that ST2 ships with. You can call that from a simple script in the Dock to open a new window in ST2. So:

set myapp to "Sublime Text 2"
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
    # Assumes you've installed the subl command line tool for ST2
    # in to /usr/local/bin. Adjust accordingly.
    do shell script "/usr/local/bin/subl --new-window"
else
    tell application myapp
        activate
    end tell
end if