Based on your reply of "Yes, precisely. Well put." to my comment under the OP...
Could you please clarify what it is you are trying to do? Are you trying to have the shell script itself refocus the tab/window in which the shell script is running? In other word, do you want to start the shell script and when the 15 minutes are up, regardless of what app is frontmost, you want Terminal to come frontmost to the tab/window in which the shell script is running and this all be done programmatically?
The following example AppleScript code used in conjunction with
osascript in a
zsh shell script should do as wanted. Obviously the value of the
sleep command is just for testing purposes, use whatever it is you are now using as the timer within your current
zsh shell script.
# First line of code of shell script.
winID="$(osascript -e 'id of window 1 of app "Terminal"')"
# Function to refocus window of running script.
# Code to refocus window of the
# running shell script using the
# window id retrieved from the first
# line of code in the shell script.
tell app "Terminal"
set frontmost of windows whose id = $winID to true
# Do some stuff...
# Your other code goes here.
In Terminal, it does not matter how may tabs/windows there are as there is only one
window, even if the window appears to have multiple tabs. In other words, asking Terminal to
count windows and
count tabs of windows will return the same number. Thus there it no need to loop thru
tabs to achieve the goal herein, just
windows, and actually in this case there really is no need to loop thru
Note that macOS Sierra what the last version in which Terminal supported multiple
tabs of a
window programmatically. In other words, from the UI a single window with two tabs there would be
tab 1 of window 1 and
tab 2 of window 1, but since macOS High Sierra from the UI a single window with two tabs there would be
tab 1 of window 1 and
tab 1 of window 2, and thus no longer the need to loop thru
There is all so no need to do something like
set selected of tab j of window i to true as once a given
window is set
frontmost, the value of
tab 1 of window i is already set to
true as a result.
The example shell script and AppleScript code, shown above, was tested in Terminal as an executable shell script under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
- 1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
As we do not know how you have your
zsh shell script coded, you'll have to work in the code presented herein to suite your needs.