Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Mac OSX Application Menu menu bar item to other than "Python" in my python Qt application

Tags:

I am writing a GUI application using python and Qt. When I launch my application on Mac, the first menu item in the Mac menu bar at the top of the screen is "Python". I would prefer the application name there to be the name of my application. How can I get my program name up there?

The following demo program creates a window with two menus: "Python", and "Foo". I don't like that, because it makes no difference to my users whether I wrote the app in python or COBOL. Instead I want menus "MyApp" and "Foo".

#!/usr/bin/python

# This example demonstrates unwanted "Python"
# application menu name on Mac.

# Makes no difference whether we use PySide or PyQt4
from PySide.QtGui import *
# from PyQt4.QtGui import *

import sys

app = QApplication(sys.argv)
# Mac menubar application menu is always "Python".
# I want "DesiredAppTitle" instead.
# setApplicationName() does not affect Mac menu bar.
app.setApplicationName("DesiredAppTitle")
win = QMainWindow()
# need None parent for menubar on Mac to get custom menus at all
mbar = QMenuBar()
# Add a custom menu to menubar.
fooMenu = QMenu(mbar)
fooMenu.setTitle("Foo")
mbar.addAction(fooMenu.menuAction())
win.setMenuBar(mbar)
win.show()
sys.exit(app.exec_())

How can I change that application menu name on Mac? EDIT: I would prefer to continue to use the system python (or whatever python is on the user PATH) if possible.

like image 472
Christopher Bruns Avatar asked Oct 19 '11 20:10

Christopher Bruns


2 Answers

You seem to need an OSX .app for this to work, as the Info.plist file in there contains the user-visible name for the application that is put there. This defaults to Python, which is the title you see for the program menu. This blog post outlines the steps you need to take, while the OSX Developer Library has the docs on the property list you need to fill.

like image 161
jro Avatar answered Sep 25 '22 12:09

jro


I have found the kernel of an answer to this question. Because I want to award the bounty to someone other than myself (I am the OP), please, anyone, take this kernel and elaborate it into a more complete answer of your own.

I can get the application menu to be "MyApp" as follows:

ln -s /System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python MyApp

./MyApp MyApp.py

There are two elements required to get this to work:

  1. The symbolic link must be named "MyApp" (or whatever you want to appear in the Application Menu)
  2. The symbolic link must point to the Python executable inside the system python app bundle. It does not work if you link to /usr/bin/python, for example.

There must be a clever way to create an app bundle or shell script that exploits this mechanism in a robust way...

like image 20
Christopher Bruns Avatar answered Sep 23 '22 12:09

Christopher Bruns