12

In a script I'm trying to find the Terminal.app window containing a specific tty device and raise that window to the top. Here's what I have in Applescript so far:

tell application "System Events"
  set foundWin to false
  if (name of processes) contains "Terminal" then
    tell application "Terminal"
      set theTabs to first tab of every window where tty is "$(tty)"
      repeat with theTab in theTabs
        if class of theTab is tab then
          set theWin to (first window whose tabs contains theTab)
          set selected tab of theWin to theTab
          set foundWin to true
        end if
      end repeat
    end tell
  end if
  if foundWin then
    --RAISE THE WINDOW?!
  end if
end tell

Where I'm getting stuck is the "raising the window" part.

Here are some things that are not quite what I want:

set frontmost of theWin to true -- this brings the window to the front of the group of Terminal.app windows but doesn't raise it above any other windows.

tell application "Terminal" to activate -- this brings every Terminal window to the front in a big stack. I just want the one window.

tell application "System Events"
    set theSysWin to first window of process "Terminal" whose name is (name of theWin)
    perform action "AXRaise" of theSysWin
end tell

This almost does it, but what it does is to raise the terminal window to the #2 position, still underneath the active window (if the active app is something other than Terminal.app.)

click theSysWin at {10,50} -- doesn't seem to do anything.

click first static text of theSysWin -- doesn't seem to do anything.

Is there a way to do this? It doesn't have to be in Applescript.

EDIT I did find this web page ( http://blog.coriolis.ch/2008/03/04/bring-any-window-to-the-front/ ) quoting an Obj-C/Cocoa call:

SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly);

I'm not familiar with either ObjC or Cocoa, and this is (ultimately) getting invoked from a shell script, so not sure where to go from there.

4 Answers 4

7

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
1
  • I mean, clicking on the window has the desired effect. So not sure why it would be impossible programatically. I did find this web page ( blog.coriolis.ch/2008/03/04/bring-any-window-to-the-front ) with Obj-C/Cocoa call: SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly); I'm not familiar enough with either objc or cocoa to know where to go from there though.
    – crowding
    Commented Feb 4, 2012 at 4:58
4

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.

2
  • Almost! It raises the window above all other windows EXCEPT the topmost window of the active application. So if I've got a web browser maximized or something, it doesn't have a visible effect :/
    – crowding
    Commented May 10, 2012 at 20:44
  • This solved the issue I was having where I was getting an error with "AXRaise" not understanding the "perform" action...
    – ttt
    Commented Feb 28, 2020 at 21:31
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.

1

Here's another attempt, tested on macOS High Sierra. Alas, this is only works when the given window is on the current desktop! The AppleScript API to control windows seems to be incomplete, making this impossible.

Substitute "Safari" and "WhatsApp" for your use case.

tell application "System Events"
    tell process "Safari"
        try
            set theWindow to (first window whose name contains "WhatsApp")
            perform action "AXRaise" of theWindow
            do shell script "open -a Safari"
        end try
    end tell
end tell

This does (visibly) raise the windows in two steps, however. It first raises the window to the top of the windows of the application, and only then raises it to the top of all windows. Not perfect, but functional.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .