且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OSX:启动应用程序的多个实例并传递命令行参数

更新时间:2023-11-22 21:48:58

c> open 是在Mac OS X上启动应用程序的正确方法。请参阅手册页了解更多信息。我现在不在Mac上,所以我不能测试这个,但我相信以下应该工作:

  os.system('open -n ./AppName.app --args -AppCommandLineArg')


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!

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')