2

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"
        try
            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.

8
  • 1
    Does your window have a unique name? stackoverflow.com/questions/13173514/…
    – Tetsujin
    Commented 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
    Commented 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
    Commented 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
    Commented Jan 14, 2023 at 20:41
  • @Mockman What do you mean by you don't have to use ui scripting ?
    – Philippe
    Commented Jan 14, 2023 at 21:08

2 Answers 2

0
tell application "Terminal"
    activate
    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.

3
  • 1
    I just need to add activate after tell application .... BTW, it works on Ventura.
    – Philippe
    Commented Jan 14, 2023 at 23:21
  • One quick question, is it possible to print the content of the window in the above script ?
    – Philippe
    Commented 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
    Commented Jan 14, 2023 at 23:34
1

The more direct and somehow more portable solution is to use some escape sequences. (Not every terminal emulator supports these advanced xterm features, iTerm2 is especially bad compared to Terminal.app)

Just run any job like this ./shellscript; echo -en "\e[5t" (src)

With some creativity you can even make the window blink by raising and lowering it repeatedly, with an afplay playing a notification...

The nice thing about this method is that it also works when you SSH to some other UNIX. And it also works in PuTTY.

For the original poster's "one quick question" "can I print the content of the window?" I created a github gist to answer it.

7
  • Thank you for the answer, I didn't know that particular escape sequence. How can I compile dumpTerminalWindowByPartialName.m and run it ?
    – Philippe
    Commented May 10 at 19:31
  • I left a comment in the gist that answers that question.
    – hym3242
    Commented May 10 at 19:37
  • I ran ./dumpTerminalWindowByPartialName tmp, and it outputs Found window: tmp — ~/tmp — 97×28. Where does the content go ?
    – Philippe
    Commented May 10 at 20:19
  • It should go directly to stdout, which is printed out in your terminal. I tested it on my system (ventura 13.4) with no issues.
    – hym3242
    Commented May 10 at 20:46
  • I see nothing but Found window: tmp — ~/tmp — 97×28. I'm on Sonoma 14.4.1.
    – Philippe
    Commented May 10 at 21:15

You must log in to answer this question.

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