0

I am trying to open multiple iterm tabs and execute commands in them using applescript. But I want to run the command and switch to another iterm window to do other stuff meanwhile. Unfortunately, the focus keeps going back to the window executing the applescript on every "create tab with default profile".

How is it possible to keep the script in that window running in the background without bringing that window to the front?

function iterm_project
osascript \
    -e 'tell application "iTerm"' \
    -e "  tell window id \"$argv[1]\"" \
    -e '    create tab with default profile' \
    -e '    tell current tab' \
    -e '      tell current session' \
    -e "        set name to \"$argv[2]\"" \
    -e "        write text \"cd $argv[3]\"" \
    -e '        split vertically with default profile' \
    -e '      end tell' \
    -e '      tell second session' \
    -e "        set name to \"$argv[2]\"" \
    -e "        write text \"cd $argv[3]\"" \
    -e '        split horizontally with default profile' \
    -e '      end tell' \
    -e '      tell third session' \
    -e "        set name to \"$argv[2]\"" \
    -e "        write text \"cd $argv[3]\"" \
    -e '      end tell' \
    -e '     end tell' \
    -e '  end tell' \
    -e 'end tell'
 end

I am using a fish shell so iterm_project() will be passed three variables the first being the window id this script will be executing in. This function will be called from another function that will decide how many tabs to open and call iterm_project() continuously with same window id but different "name" "path to cd in".

4
  • Show some code - how are you opening the tabs? Note that if you are using GUI scripting, the target needs to be in the front.
    – red_menace
    Commented Jul 1, 2021 at 2:54
  • Yes, absolutely, let me put the code. I thought I was missing a keyword that would put the window in the background. Added the code.
    – SFbay007
    Commented Jul 1, 2021 at 2:55
  • Just an observation... Instead of using -e and all that quote escaping, learn how to write it in the form of a here document. Commented Jul 1, 2021 at 3:23
  • Thanks for the suggestion user3439894, I am not sure what is a here document but will look it up.
    – SFbay007
    Commented Jul 1, 2021 at 3:34

1 Answer 1

1
  1. Try calling System Events to execute your applescript code rather than by invoking iTerm directly. System Events is MacOS dedicated backgrounder process for applescript and may allow for your code execution while preventing iTerms' windows activation. For more on the topic you may start here.

  2. As advised in the comments, using HereDoc statements make for much easier applescripting in the terminal (exemple below).

yourfunction() {
 /usr/bin/osascript 2>&- <<-_EOF
 tell application "System Events"
   ...
   ...
 end tell
_EOF
}

Under this syntax all lines between _EOF markers are interpreted as a single block of text and thus do not require quoting, all thanks to redirection.

2>&- redirection of osascript's error messages
It may safely be removed - especially for debugging purposes.
<<- redirection to allow the usage of tabs in the code
More on heredoc redirection options here.

You must log in to answer this question.

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