How to focus parent terminal tab of shell script for input? I have a zsh script where I prompt for user input every 15 minutes. How can I make the tab of Terminal that the script is running in open up / focus? open -a Terminal just opens the whole window.
I'm aware of this trick to get current application and re-open it, is there a way to extend it to tabs/sessions?
term=$(/usr/bin/osascript -e &quot;copy path to frontmost application as text to stdout&quot;)
/usr/bin/osascript -e 'tell application $term to activate'

Also aware of this trick to go back to an old tab, although (1) I couldn't get it to work and (2) I need the data in stdout so zsh can use it.
osascript \
    -e 'tell application &quot;Terminal&quot; to set oldTab to the selected tab of the front window' \
    -e 'tell application &quot;System Events&quot; to tell process &quot;Terminal&quot; to keystroke &quot;t&quot; using command down' \
    -e 'delay 1' \
    -e 'tell application &quot;Terminal&quot; to set selected of oldTab to true'
This is a little awkward since I can't run this code from within your script (nor within an applescript) but this osascript should allow you to specify a desired tab (tp) and switch to it from any other within the same window.
osascript -e '
tell application &quot;Terminal&quot; to set tp to item 1 of (tabs of window 1 whose selected is true)
tell application &quot;System Events&quot; to tell process &quot;Terminal&quot; to keystroke &quot;t&quot; using command down
delay 1
tell application &quot;Terminal&quot; to set selected of tp to true'

I think your issue was just a syntax one: e.g. selected tab versus tab whose selected is true.
Each line does the following:

Sets variable tp to the active tab
Creates a new tab using system events
(which becomes the active tab)
Delay 1
Makes tp the active tab

The variable tp will resemble this:
--&gt; tab 3 of window id 360 of application &quot;Terminal&quot;

Note, there are some alternatives to setting tp to the active tab but they're more complex.