How to change the name of the application in the OSX menu bar in a set of applications with pure Python?

I am trying to create a pure-Python application package for a wxPython application. I created a .app directory with files described in Apple docs, with an Info.plist file, etc. The only difference between a “regular” application and this package is that the entry point (CFBundleExecutable) is a script that starts with the following line:

#!/usr/bin/env python2.5 

Everything works fine, except that the application name on the OSX menu bar is still “Python”, although I set CFBundleName to Info.plist (I copied the result of py2app, actually). Here you can view the full Info.plist .

How can i change this? I read everywhere that the menu name is determined only by CFBundleName. How is it possible that the Python interpreter can change this at runtime?

Note: I used py2app before, but the result was too big (> 50 MB instead of the current 100 KB), and it was not even portable between Leopard and Snow Leopard ... so it seems much easier to create a pure Python application package manually "than convert py2app output.

+3
source share
3 answers

The “Build Applet.app,” which comes with the Python developer tools, is actually a pure Python application package. It performs the following actions:

  • the Python interpreter is placed (or bound) into the MacOS/ directory
  • the executable script file ( Foo.app/Contents/MacOS/Foo ) sets some environment variables for this interpreter and calls os.execve() .

The executable script looks like this (assuming the program entry point is in Resources/main.py ):

 #!/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python import sys, os execdir = os.path.dirname(sys.argv[0]) executable = os.path.join(execdir, "Python") resdir = os.path.join(os.path.dirname(execdir), "Resources") libdir = os.path.join(os.path.dirname(execdir), "Frameworks") mainprogram = os.path.join(resdir, "main.py") sys.argv.insert(1, mainprogram) pypath = os.getenv("PYTHONPATH", "") if pypath: pypath = ":" + pypath os.environ["PYTHONPATH"] = resdir + pypath os.environ["PYTHONEXECUTABLE"] = executable os.environ["DYLD_LIBRARY_PATH"] = libdir os.environ["DYLD_FRAMEWORK_PATH"] = libdir os.execve(executable, sys.argv, os.environ) 
+3
source

Change the LSHasLocalizedDisplayName key in Info.plist to true , as in:

 <key>LSHasLocalizedDisplayName</key> <true/> 

and then create the file in the executable package

 foo.app/Contents/Resources/English.lproj/InfoPlist.strings 

which has lines

 CFBundleName="name in the menu bar"; CFBundleDisplayName="name in the Finder"; 
+2
source

Actually, if you create a soft link to the python executable and use it, and not the executable itself (inside your MyApp.app/Contents/MacOs/- script-), everything works fine. I personally use the "#! / Bin / sh" script instead and just use the "exec" command. (I still have to use wx.App.SetAppName (MyAppName).) For example:

 #! /bin/sh export PYTHONPATH=/Applications/MyApp.app/Contents/Resources/[myPythonCode] export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/2.6/lib exec "/Applications/MyApp.app/Contents/MacOS/[SoftLinkToPythonExe]" "/Applications/MyApp.app/Contents/Resources/myAppMain.py" 
+1
source

Source: https://habr.com/ru/post/948755/


All Articles