I have a shell script which runs a long-running processes in a Terminal window.

I leave it running and work on other Terminal windows.

I'd like to be notified when the shell script finishes, by bringing its window to front.

I have following code, which I wanted to run at the end of the shell script:

tell application "System Events"
    tell process "Terminal"
            set win to (!code to get current window!)
            perform action "AXRaise" of win
        end try
    end tell
  end tell

but I don't know how to get current window.

  • 1
    Does your window have a unique name? stackoverflow.com/questions/13173514/…
    – Tetsujin
    Jan 14, 2023 at 15:38
  • 1
    @Tetsujin All my Terminal windows have generic name like User - Directory - Shell type. But I can give it a unique name. Will take a look at the link.
    – Philippe
    Jan 14, 2023 at 16:50
  • I found a few scripts similar - all seem easy to find the window by name. Couldn't see anything that would find it by number that was reliable.
    – Tetsujin
    Jan 14, 2023 at 16:52
  • Does the window have any unique content? This probably isn't the most efficient approach but if your terminal windows are sane then you could set x to windows whose contents contains "unique string" which will return a list, presumably with a single window item. Then set index of item 1 of x to 1. If the window has but a single tab it will make things easier (as then you can avoid having lists of lists) but you can also monkey around with the contents of tabs of…. Added bonus… you don't have to use ui scripting.
    – Mockman
    Jan 14, 2023 at 20:41
  • @Mockman What do you mean by you don't have to use ui scripting ?
    – Philippe
    Jan 14, 2023 at 21:08

1 Answer 1

tell application "Terminal"
    set hw to windows whose contents contains "new-glasses"
    --> {window id 67 of application "Terminal"}
    set hw1 to item 1 of hw
    --> window id 67 of application "Terminal"
    set index of hw1 to 1
end tell

The above script presumes that only one window will contain the specified string and that it has but one tab. The first line gets a list with a single item — the matching window. The second line gets that specific window. The third line sets its index to 1 (i.e. makes it the front window). Dunno if it makes a difference but I'm running this on Sierra.

  • 1
    I just need to add activate after tell application .... BTW, it works on Ventura.
    – Philippe
    Jan 14, 2023 at 23:21
  • One quick question, is it possible to print the content of the window in the above script ?
    – Philippe
    Jan 14, 2023 at 23:27
  • 1
    Try adding print hw1 with print dialog. There are a handful of print settings that you can apply. The print dialogue is optional. Alternatively, you could always do something directly with the contents, e.g. assign a variable, write to a text file, add to an app's document, etc… and then print that.
    – Mockman
    Jan 14, 2023 at 23:34

You must log in to answer this question.

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