Executable path to Mac App
Asked Answered
R

3

6

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.

Recollection answered 27/11, 2010 at 13:33 Comment(0)
T
1

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(?).

Teniacide answered 2/12, 2010 at 0:46 Comment(4)
that is an option, but simpler option would be to just execute it separately because I usually just run background process for testing + I may run different background process at different time on user input, anyway I am more interested to know how can I run my background process or any other app included in bundle without complicating the codeRecollection
my question is simply, how to run a a bundled python script using subprocess moduleRecollection
Well it is possible you just have to figure out how py2app does it, which shouldn't be so hard. You want to run "python AppName.app/Contents/Resources/myapp.py", not the OSX binary.Teniacide
Forks are wonderful btw, once you have forked you will never look back.. :-)Teniacide
N
0

Create one application then just call the --bakgroundproccess for the app, then create a shortcut for the backgroundprocess flag. Or two different app files. Ether is a good way

Nigelniger answered 29/11, 2010 at 14:29 Comment(1)
a) i have to call background app from main app, how shortcut will help? b) if I create two app, still how will first app know where is second app, and also how to call a app with arguments?Recollection
S
0

I couldn't get py2app working, but the applications' executable are located in AppName.app/Contents/MacOS/AppName. You can check in AppName.app/Contents/Info.plist file for key <key>CFBundleExecutable</key>, it points to the bundles executable in AppName.app/Contents/MacOS folder.

So you can call it as AppName.app/Contents/MacOS/AppName --backgroundprocess.

If you want the BackgroundApp to run the background process without any arguments, there are two options: a) make the bundle from python script, which runs the background process without any arguments; b) change <key>CFBundleExecutable</key> in Info.plist file to say BackGroundApp_shell, and make a shell script Contents/MacOS/BackgroundApp_shell of the following content:

#!/bin/sh

`dirname "$0"`/BackgroundApp --backgroundprocess

assuming the executable is BackgroundApp.

Subroutine answered 29/11, 2010 at 14:31 Comment(2)
I have already tried something like AppName.app/Contents/MacOS/AppName --backgroundprocess but it fails, looks like py2app or mac sets some environment variables before executing the appRecollection
@Anurag you can try to make a bundle for a simple python script: import sys; print sys.argv to see how the exec created py2app treats command-line arguments. I don't think this is something concerning env vars. Just a guess: py2app creates an exec which doesn't take arguments, but you can try that simple script, to make sure.Subroutine

© 2022 - 2024 — McMap. All rights reserved.