AppleScript: Pass all command line args to two terminals

I’ve searched high and low, but can’t find something that will get me what I need. I’m looking for a way to, from Terminal, launch two different python processes with the same set of command line arguments. I'm running Yosemite (10.10.5) if that matters.


Currently, I have to open up two Terminal windows or tabs and run a command in each of them:


Terminal 1:

python app1.py -c ~/users/config -v


Terminal 2:

python app2.py -c ~/users/config -v


I’d like to get this down to one executable script. On Linux, I’ve created a script like this:


x-terminal-emulator -e bash -c "python app1.py $@"

x-terminal-emulator -e bash -c “python app2.py $@"


Of course that won’t work in OS X, but I’d love it if there was an equivalent. I could probably background one of the processes and run both in the same window, but I'd prefer to see both. It appears there is not, especially keeping the current working directory, so I looked into AppleScript.


My challenge is passing all the args (there can be more than what’s in the example above). Bash lets me use $@ to pass everything in Linux, but I can’t find an equivalent in AppleScript.


Basically, I’m looking for an AppleScript file similar to this (replacing $@ with something that actually works!):


#!/usr/bin/osascript

tell application "Terminal"

activate

my makeTab()

do script “python app1.py $@” in tab 2 of front window

my makeTab()

do script “python app2.py $@” in tab 3 of front window

end tell


on makeTab()

tell application "System Events" to keystroke "t" using {command down}

delay 0.2

end makeTab


Any help would be much appreciated! This would be my first foray into AppleScript, so forgive me if I've missed something really obvious.

MacBook Pro (Retina, 13-inch,Early 2015), OS X Yosemite (10.10.5)

Posted on Sep 14, 2015 7:31 PM

Reply
5 replies

Sep 15, 2015 8:27 AM in response to bulldoggk

How about a Bash script that passes arguments into an AppleScript HERE document? The do script will put the output in the most recently created tab in the front window. No need to specify that specific tab.

#!/bin/bash



function osxterm {



`osascript <<-END

tell application "Terminal"

activate

makeTab()

do script "python \"$1\".py \"$3\"" in front window

makeTab()

do script "python \"$2\".py \"$3\"" in front window

end tell

return



on makeTab()

tell application "System Events" to keystroke "t" using command down

delay 0.2

return

end makeTab

END`

}



osxterm app1 app2 "-c ~/usrs/config -v"



exit 0

Sep 15, 2015 9:46 AM in response to VikingOSX

Thanks. This looks like a good start, but it looks like it's hardcoding the "-c ~/usrs/config -v" portion. This is the part that can change, and is what I need to pass from the command line into the new tabs. In that case, we don't need a variable for the apps...we can just hardcode that. Something like:


tell application "Terminal"

activate

makeTab()

do script "python app1.py \"$3\"" in front window

makeTab()

do script "python app2.py \"$3\"" in front window

end tell


but I still need a way to get whatever params are passed to the script to make it into the $3 slot (or whatever it should be...$1 I guess). The script could run with any number of params, so I need to just pass them all.


./script.sh ~/users/config -x -vV -c option1

./script.sh config -c option2 -L log.txt


...and about a hundred other combinations of things.

Sep 15, 2015 2:13 PM in response to VikingOSX

That makes sense. When I run it, though, I get an error that says:


45:54: execution error: Terminal got an error: Can’t continue makeTab. (-1708)

I have no idea how to troubleshoot that! Is there some sort of verbose output? That makeTab() function is the same as a test AppleScript that I'd written before that worked, but it was actually AppleScript, not wrapped in a bash script. Here's what I have:


#!/bin/bash


function osxterm {


`osascript <<-END

tell application "Terminal"

activate

makeTab()

do script "python app1.py \"$1\"" in front window

makeTab()

do script "python app2.py \"$1\"" in front window

end tell

return


on makeTab()

tell application "System Events" to keystroke "t" using command down

delay 0.2

return

end makeTab

END`

}


osxterm "$@"


exit 0


Thanks again!

Sep 16, 2015 8:20 AM in response to bulldoggk

Ok, I have updated the syntax, and it now works fine on Yosemite 10.10.5 with my Python applications passed a command-line string.


#!/bin/bash


function osxterm {


`osascript <<-END

tell application "Terminal"

activate

my makeTab()

do script "python app1.py " & "$1" in front window

my makeTab()

do script "python app2.py " & "$1" in front window

end tell

return


on makeTab()

tell application "System Events" to keystroke "t" using command down

delay 0.2

return

end makeTab

END`

}



osxterm "$@"


exit 0

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript: Pass all command line args to two terminals

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.