1

I tried

#!/bin/bash
osascript -e '
  tell application "Terminal"
  do script "cd ~/www/service/code&& npm install && npm run dev"
  activate
  end tell

  tell application "Terminal"
  do script "cd ~/www/app-ui && npm install && npm start"
  activate
  end tell


'

Nothing happened, no error either. Where went wrong?

4
  • 1
    Do you really need to use osascript for this? Commented Dec 19, 2017 at 16:35
  • Why a second tell application "Terminal" block when you can have both do script commands in the same tell application "Terminal" block, or gang the command together in the same do script command, e.g. do script "command; command" Commented Dec 19, 2017 at 16:38
  • @user3439894, the space is good form, but not strictly necessary. Try running echo foo&&echo bar -- you'll see both commands run. Commented Dec 19, 2017 at 16:39
  • @Charles Duffy, Yeah, I actually know that... wasn't thinking... haven't had my second cup of coffee yet. Commented Dec 19, 2017 at 16:40

1 Answer 1

2

The easiest way to do this is to create a separate script for each piece you want to run in a separate window.

#!/bin/sh

cat >scriptA <<EOF
#!/bin/sh
cd ~/www/service/code && npm install && npm run dev
EOF
chmod +x scriptA

cat >scriptB <<EOF
#!/bin/sh
cd ~/www/service/code && npm install && npm run dev
EOF
chmod +x scriptB

open -a Terminal.app scriptA
open -a Terminal.app scriptB
Sign up to request clarification or add additional context in comments.

8 Comments

sry, crazy new for shell.... but open -a Terminal.app scriptA open -a Terminal.app scriptB cant be executed together no? I tried it, nothing happened when I put them into one .sh file , if I can't run all the script in one stroke I feel very unsatisfied...
Works fine for me. Does need to be two lines, of course.
The exact code I ran myself is at gist.github.com/charles-dyfis-net/… -- pops up one window containing HELLO, and another containing WORLD.
ahhhh man, I thought you literarily means make two files which I did lol its actually one script. gotcha, yes, it works very well!!!! Thanks a lot!
Well -- it does create two files. When this script is run, it creates one file named scriptA and another file named scriptB, and then starts two terminals -- one running scriptA, one running scriptB. In a real-world use case, I'd expect you to just have them already pre-created rather than doing that at runtime.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.