Create a command to focus a specific window from anywhere in OS

Solution 1:

You should give Automator a try. You could "Record" a new service (that you can later publish for all Apps and assign a Keyboard Shortcut), that clicks the app from the Dock. In fact, you could add more actions and perform your "tell that application to execute something".

I've tried creating one (that is relatively useless but works) that clicks on Mail in my Dock, then clicks the get new mail icon in the toolbar. Fun ;)

Attached is the shot of the Automator result:

Automator

I used the Record button on the top right corner and then did the actions myself (clicked on the Mail's dock icon, then clicked on Mail's Get New Mail icon in the toolbar, then clicked "Stop Recording"). Please notice I've selected "Service receives [no input]" in the first combo box, as I wasn't interested in any input.

This gives you multiple possibilities to have a couple of services that go back and forth your TextEditor and your Browser.

Automator is located by using Spotlight and typing Automator (also located in /Applications/Automator.app)

In this case I've created a new "Service" but you could probably get away by creating "scripts". Not sure which one would be faster tho. Experiment. Automator is your friend.

Solution 2:

You might do something like this with AppleScript:

tell application "Terminal"
    activate
    windows where name contains "bash"
    if result is not {} then set index of item 1 of result to 1
end tell

Or using System Events:

tell application "System Events" to tell process "Terminal"
    set frontmost to true
    windows where title contains "bash"
    if result is not {} then perform action "AXRaise" of item 1 of result
end tell

Solution 3:

I had a lot of trouble getting this to work with iTerm2. Here is single command solution:

  osascript -e 'tell application "System Events" to tell process "iTerm2"' \
            -e 'set frontmost to true' \
            -e 'if windows is not {} then perform action "AXRaise" of item 1 of windows' \
            -e 'end tell'

Solution 4:

I use BetterTouchTool for setting different keyboard shortcuts or trackpad gestures on Mac OS X. One of the actions it can launch is Execute Terminal Command

So in the settings of the command I provide following:

osascript -e 'tell application "NameOfTheApplicationHere" to activate'

Here is an example from the BetterTouchTool preferences:

enter image description here