Script to raise a single window to the front

Solution 1:

open only raises a single window:

do shell script "open -a Terminal"

Another way to raise a window is to set its index to 1 and then use AXRaise:

tell application "Terminal"
    set index of window 2 to 1
end tell
tell application "System Events" to tell process "Terminal"
    perform action "AXRaise" of window 1
end tell

Solution 2:

You can click menu items of applications that aren't active. Use it to click the menu item corresponding to the window in the Window menu.

This is what you need to insert in your script:

tell application "System Events"
    tell application process "Terminal"
        click menu item (name of theWin) of menu of menu bar item "Window" of menu bar 1
    end tell
end tell

While it doesn't activate the program, the window is frontmost.

Solution 3:

I realize this thread is quite old, but I ran across it while fighting the same problem and others may as well. As far as I can tell it cannot be done in raw AppleScript. It can be done in AppleScriptObjC however. To use the example from @Daniel Beck, the new version would be like this.

property NSWorkspace : class "NSWorkspace"
set workspace to NSWorkspace's sharedWorkspace()
tell application "System Events"
    tell application process "Terminal"
        click menu item (name of theWin) of menu of menu bar item "Window" of menu bar 1
        workspace's launchApplication_("Terminal")
    end tell
end tell

The added code will use the Cocoa class NSWorkspace to "launch" the application, which raises only the frontmost window of that application to the front with focus. For more detail on the process that brought me to this, I have a more thorough writeup on my website here.