Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable path to Mac App

In a py2app/Mac Application Bundle, is there a way to spawn another instance of same app from within the app, by passing different command line arguments?

or given a mac app bundle, how can I run it from command line and pass some arguments too?

Edit1:forking is a limited option, which may not work with 3rd party executables bundle with app+I need to run this on mac and windows.
Edit2: Question is how to run a a bundled python script using subprocess module

Details:

I am using py2app to generate a app bundle for my appilcation. My application has two parts

  1. MainApp: which is the UI
  2. BackgroundApp: a background process, which does the real job

Both MainApp and BackgroundApp have been implemented as python script and actually they are the same python script with different commandline e.g.

python myapp.py
python myapp.py --backgroundprocess

So when I run python myapp.py it automatically starts background process based on program path, but as I have now bundled my app as py2app I am not sure what executable I should be calling and passing --backgroundprocess option?

What I have tried

  1. $ open MyApp.app/ this opens the app but I can't pass the arguments to it, as they will be arguments for open command and will not be passed to my app

  2. $ MyApp.app/Contents/MacOS/MyApp --backgroundprocess opens the app but not the backgroun process as it seems arguments are not being passed to app

also it throws error

  Traceback (most recent call last):
  File "/Users/agyey/projects/myapp/release4.26/py2exe/dist/MyApp.app/Contents/Resources/run.py", line 4, in <module>
    from renderprocess import RenderEngineApp
  File "renderprocess/RenderEngineApp.pyc", line 6, in <module>
  File "wx/__init__.pyc", line 45, in <module>
  File "wx/_core.pyc", line 4, in <module>
  File "wx/_core_.pyc", line 18, in <module>
  File "wx/_core_.pyc", line 11, in __load
ImportError: dlopen(/Users/agyey/projects/myapp/release4.26/py2exe/dist/MyApp.app/Contents/Resources/lib/python2.5/lib-dynload/wx/_core_.so, 2): Library not loaded: @executable_path/../Frameworks/libwx_macud-2.8.0.dylib
  Referenced from: /Users/agyey/projects/myapp/release4.26/py2exe/dist/MyApp.app/Contents/Resources/lib/python2.5/lib-dynload/wx/_core_.so
  Reason: Incompatible library version: _core_.so requires version 7.0.0 or later, but libwx_macud-2.8.0.dylib provides version 2.6.0

Conclusion: it looks like it may not be possible Launch an app on OS X with command line

open doesn't except arguments.

like image 297
Anurag Uniyal Avatar asked Nov 27 '10 13:11

Anurag Uniyal


1 Answers

Howto find cwd and execute an arbitrary supplied binary First place the binary in AppName.app/Contents/Resources then run this code from the python script:

import subprocess
process=subprocess.Popen((os.getcwd() + "/3rd_party_binary","--subprocess")) 
process.poll() # is running?

Howto properly spawn two version of your python app

Fork is the old tried way to do this on MacOSX (unix)

#!/usr/bin/env python
import os, sys

pid = os.fork()
if pid:
    # we are the parent
    background_process.start()
    os.waitpid(pid, 0) # make sure the child process gets cleaned up
else:
    # we are the child
    gui_app.start()
    sys.exit(0)

print "parent: got it; text =", txt

Multiprocessing in Python is apparently something that works on windows as well which I guess would be interesting to you(?).

like image 85
Erik Johansson Avatar answered Oct 10 '22 06:10

Erik Johansson