Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac OS app works fine when launched from command line, not when launched from Finder #263

Open
ronaldoussoren opened this issue Apr 15, 2019 · 7 comments

Comments

@ronaldoussoren
Copy link
Owner

Original report by Jonas Nielsen (Bitbucket: jn2002dk, GitHub: jn2002dk).


Hello and thank you for this excellent software

I've made an audio player which uses MPV. Now, when launching the app using open myapp.app everything works as expected. It also works when launching the unix executable both from command line and Finder

However, if i double click the app in Finder it opens and works as expected until i'm trying to play audio. I think it might have something to do with Finder opening with a different working directory compared to when using open command so i doubt this is actually any problem with py2app. Since it's a streaming audio player, i think it may need to write parts of the file temporarily .I am stumped though, i've tried all i could think of and i'm getting nowhere so i'm hoping someone here can point me in the right direction

Cheers

@ronaldoussoren
Copy link
Owner Author

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


The app launcher in py2app sets the working directory to the "Resources" folder inside the application bundle.

@ronaldoussoren
Copy link
Owner Author

Original comment by Jonas Nielsen (Bitbucket: jn2002dk, GitHub: jn2002dk).


Yes, i looked through the boot.py file. Is it possible Finder is overruling this? From what i understand, there is no standard for setting the working directory on Mac OS. From this https://stackoverflow.com/questions/10015770/what-do-i-set-the-working-directory-of-a-macos-app-bundle-launched-from-my-java

Mac apps generally can't rely on the working directory being set to anything in particular. Xcode sets it one way, launching from the Finder sets it a different way (usually to /).

Since my app is working perfectly when launched with the open command, i'm assuming it not working from Finder is related to this Mac OS issue. I've been trying, and failing, to find a workaround. I've tried through shell script, applescript, automator etc. to no avail. The mach-o file works fine when launched by the Finder by the way but that leaves me with an open terminal which is less than ideal

@ronaldoussoren
Copy link
Owner Author

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


As I wrote, the py2app launcher will explicitly set the CWD to the Resources folder in the app bundle, because that’s a much more useful default than the location used by the Finder.

There may be other dependencies to the shell environment (such as environment variables).

One way I use to debug issues is to patch ``__boot__.py`` to redirect sys.stdout and sys.stderr to a file and then launch the application. With some luck this will give you more information (assuming ``__boot__.py`` is executed at all).

Something like:

import os, sys
file = open("/tmp/debuglog.txt", "w")
os.dup2(file.fileno(), 1)
os.dup2(file.fileno(), 2)

This will open /tmp/debuglog.txt, and redirects the stdout/stderr streams to that file (on a low level to ensure this affects all output, and not just that of python code)

@ronaldoussoren
Copy link
Owner Author

Original comment by Wyllys Ingersoll (Bitbucket: wyllys, GitHub: wyllys).


Is there a way to suppress changing the directory, or to discover what
the original directory was?

We've got an application that takes parameters on the command line, and where we take a path, we'd like to support paths relative to the user's current working directory from the shell they're using, like any other shell command.

@ronaldoussoren
Copy link
Owner Author

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


“python setup.py py2app --no-chdir” should disable the code that disables changing the working directory..

@ronaldoussoren
Copy link
Owner Author

Original comment by Wyllys Ingersoll (Bitbucket: wyllys, GitHub: wyllys).


Thanks! That worked.

@LittleRedLynn
Copy link

Hi, Ronald. I'm facing the same problem, everything seems to be invalid when I launch my app through double-clicking in finder as it works fine when launched through Terminal "open". I've tried many ways including changing the working directory u mentioned above and failed. Do u get any further progress in handling this issue? Any useful info will be appreciated.

brechtm added a commit to brechtm/thriftybackup that referenced this issue Aug 10, 2023
We can't stdout output directly, but we can redirect it at least:
ronaldoussoren/py2app#263 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants