How to use osascript to open Iterm2 and run script?

84 views Asked by At

Currently, I have an sh file which I use to run a script as such:

osascript -e "tell application \"Terminal\" to do script \"cd $PWD && npm run build:watch\""
osascript -e "tell application \"Terminal\" to do script \"cd $PWD && firebase emulators:start --inspect-functions\""

This works fine with the default terminal. However, how can I substitute this with Iterm2? I can't seem to find any answer online.

If I substitute Terminal with Iterm2, I get this error:

29:38: syntax error: A “script” can’t go after this identifier. (-2740)
29:38: syntax error: A “script” can’t go after this identifier. (-2740)
1

There are 1 answers

0
tekintosh On

My working example is as follows:

osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to tell current session of current window to write text "echo \"WOW! It works\""'

So, each block coming right after the option "-e" is the actual command you desire to execute.

  • 'tell application "iTerm" to activate' -> does open iTerm and brings it to the front
  • 'tell application "iTerm" to tell current session of current window to write text "echo "WOW! It works""' -> does make the current session in your opened iTerm executes the given command (echo "WOW! It works" in this case)

Hope it helps.