1

I want to run a command in a terminal tab which will open a new tab, run a command there too and then get the focus back to the previous Terminal tab (the one i run the first command), is it possible? using of corse osascript.

what i have so far:

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
osascript -e 'tell application "Terminal"' -e 'do script "cd /path/to/dir; clear;" in selected tab of the front window' -e 'end tell' > /dev/null
osascript -e 'tell application "System Events" to tell process "Terminal" to key code "48" using control down' > /dev/null

this will only work if i have only 2 opened tabs.

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60

1 Answers1

1

osascript -e 'tell application "Terminal" to set selected of tab 1 of front window to true'

Replace the 1 with the appropriate tab number to select whichever tab you like.

Or you can save the current tab number:

osascript \
    -e 'tell application "Terminal" to set oldTab to the selected tab of the front window' \
    -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
    -e 'delay 1' \
    -e 'tell application "Terminal" to set selected of oldTab to true'
Luke Miles
  • 941
  • 9
  • 19
Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • like I said it works if i have 2 tabs opened and i run the command from the 1st tab. if you know how to do it dynamically, it would be even better! – Alex Rabinovich Sep 03 '17 at 07:59
  • Works on my machine with three tabs open. – Charles Srstka Sep 03 '17 at 15:18
  • right, but what i meant is that it will go for the first tab (if you ran the command on the second one). what i wanted is that if you run the command on the second tab (out of 3 for example) it will go back to the second instead of to the first. – Alex Rabinovich Sep 04 '17 at 07:32
  • So just save the old selected tab first: `osascript -e 'tell application "Terminal" to set oldTab to the selected tab of the front window' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'delay 1' -e 'tell application "Terminal" to set selected of oldTab to true'` – Charles Srstka Sep 04 '17 at 14:43