Reputation: 2027
I have this code that sends a specific command to all tabs including the current one, in iTerm2. But, sometimes, some tabs are stuck and I want to get them to a shell prompt before trying to execute the command. However, sending Ctrl+C to the current tab, will stop the script from continuing.
I want to send the kill signal to all other tabs but not the current one.
osascript \
-e 'tell application "iTerm"' \
-e " tell current window" \
-e ' repeat with aTab in tabs' \
-e ' tell aTab' \
-e ' repeat with asession in sessions' \
-e ' tell asession' \
-e ' tell application "System Events" to keystroke "c" using {control down}' \
-e ' end tell' \
-e ' end repeat' \
-e ' end tell' \
-e ' end repeat' \
-e ' end tell' \
-e 'end tell'
I tried conditioning "tell aTab" with "if aTab is not equal current tab", but that still didn't work.
Upvotes: 3
Views: 909
Reputation: 7555
The following example AppleScript code was tested in Script Editor under macOS Catalina and iTerm2, Build 3.3.12, and worked for me without issues as is, with the current window having multiple tabs each with a single session, and except for the tab that was the current tab when executed, stopped the running process in each tab.
tell application "iTerm"
activate
tell current window
set excludeTab to current tab
select tab 1
repeat with aTab in tabs
select aTab
if current tab is not equal to excludeTab then
tell current session
tell application "System Events" to ¬
key code 8 using {control down}
end tell
end if
end repeat
select excludeTab
end tell
end tell
Notes:
Instead of using osascript -e command
on a script such as this, I'd highly recommend creating an executable file with a #!/usr/bin/osascript
shebang and then just copy and paste the example AppleScript code into the executable file as is. This IMO is much easier to maintain and modify as needed then using osascript -e command
on a script such as this.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.
Upvotes: 1