views:

313

answers:

2

There is a program I need to launch multiple times and pass it different arguments each time. To do this I tried writing a simple python script as follows:

import sys, os
from os.path import join

# This works, but will not launch twice
os.system('./AppName.app -AppCommandLineArg')

# This allows launching two instances but without command line arguments
os.system('open --new --background ./AppName.app')

# Attempt #1
os.system('open --new --background ./AppName.app -AppCommandLineArg')

# Attempt #2
os.system('open --new --background "./AppName.app -AppCommandLineArg"')

# Attempt #3
os.system('open --new --background "./AppName.app/Contents/MacOS/AppName -AppCommandLineArg"')

The reason I'm using 'open' is to be able to launch the app multiple time. Is 'open' the correct command to use? Any suggestions on how to do this? Working with linux/mac is very new to me.

Thanks!

Edit - Here is the code that solved the problem for me:

p0   = subprocess.Popen(['./AppName.app/Contents/MacOS/AppName', '-AppCommandLineArg'])
p1   = subprocess.Popen(['./AppName.app/Contents/MacOS/AppName', '-AppCommandLineArg'])

Cheers!

A: 

Yes, open is the correct way of launching applications on Mac OS X. See the man page for more information. I'm not on a Mac right now, so I can't test this, but I believe the following should work:

os.system('open -n ./AppName.app --args -AppCommandLineArg')
Adam Rosenfield
open doesn't take command line args. See the link in my reply for options/details.
psychotik
@psychotik: according to the man page I linked to, yes it does, at least on OS X 10.6
Adam Rosenfield
I tried that but I get this error message:open: unrecognized option `--args'The '--args' doesn't show up as a flag when printing out the help about 'open' either. I do see that the page you pointed me to has that flag. Any idea how the 'open' in my terminal does not support that flag?
This flag might be new in 10.6 - I'm glad to see they added it. It's not going to get too much adoption until a couple years though - I suspect 10.5 is going to remain the most popular version of OSX for a bit. OP - you're probably using 10.5
psychotik
Interesting! Yes, it does seem that --args has been implemented for 10.6.
Ned Deily
+1  A: 

Duplicate: http://stackoverflow.com/questions/1308755/launch-an-app-on-os-x-with-command-line

Also, this doesn't answer the multiple apps part of the question, but might help: http://stackoverflow.com/questions/1081218/ideal-way-to-single-instance-apps-on-the-mac

psychotik