0

I am programmatically creating a menu in PyObjC with this hierarchy.

NSMenu (setMainMenu on NSApplication)
- NSMenuItem(1)
  - NSMenu: "App Name"
    - NSMenuItem(1)
    - NSMenuItem(2)
    ...
- NSMenuItem(2)
  - NSMenu: "File"
    - NSMenuItem(1)
    - NSMenuItem(2)
    ...
- NSMenuItem(3)
  - NSMenu: "Edit"
    - NSMenuItem(1)
    - NSMenuItem(2)
    ...
...

However, all PyObjC app shows is a single "Python" menu, that contains items that ought to be under "App Name".

How do I make it behave?

Alex B
  • 82,554
  • 44
  • 203
  • 280
  • You really want a proper app wrapper and to use as much of Cocoa as possible, including a Main xib file. – bbum Jun 17 '13 at 16:53
  • @bbum Yeah I know. I just can't stand Objective-C and IB and trying to find alternatives. – Alex B Jun 17 '13 at 17:03
  • If you want to write a proper Mac app, you are going to need to understand the system APIs thoroughly and that'll require understanding Objective-C. There are numerous articles over the past years (decades?) about writing Cocoa/OpenStep apps in Python, btw. Google knows all. – bbum Jun 17 '13 at 18:00
  • @bbum Oh yes, I totally get the need for knowing the Cocoa API. Countless code samples I look at are all in Objective-C, so are the blog posts and Apple API docs. I just don't want to *write* Objective-C. All Objective-C code translates quite directly into PyObjC code. The problem in my case is that PyObjC obviously does something *extra* to the menus. Unfortunately, the usage of PyObjC dwindles, so Google yields nothing on this topic. – Alex B Jun 18 '13 at 04:24
  • 3
    FWIW I'm still working on PyObjC and will likely work on a good solution for this in the future because the Interface Builder component of Xcode gets less useful for PyObjC with every release. – Ronald Oussoren Jun 18 '13 at 10:50
  • 1
    This (old) webpage appears to document how to create a menu from code, although this uses private APIs and as such is likely not the right solution: – Ronald Oussoren Jun 18 '13 at 10:53
  • 1
    And finally: unless you are using py2app (or some other way to build an app bundle) the "Python" menu you're getting is likely the menu defined in the MainMenu.nib of the Python.app application hidden in Python.framework. – Ronald Oussoren Jun 18 '13 at 10:54
  • Thanks @RonaldOussoren. You should post that as the answer. – bbum Jun 18 '13 at 17:45

1 Answers1

1

The "Python" menu is likely the menu from the MainMenu.nib in the Python.app application hidden in Python.framework. That application makes it possible to use GUI APIs in a command-line script without having to resort to private APIs.

The best way to get the proper behavior is to create an application bundle, either through py2app or manually.

Ronald Oussoren
  • 2,715
  • 20
  • 29
  • You are totally right! And the dock icon I was looking for in PyObjC actually comes from /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app. From the part 1 of the blog posts series you linked it looks like it's not possible to actually create a menu from scratch, because it's created via some private APIs (that's completely mad!). – Alex B Jun 19 '13 at 11:07
  • I don't know if you cannot create the menu from scratch, the page I found uses a private API but I consider it unlikely that this can only be done with private APIs. I hope to have some time to play around with generating a GUI completely from code in the near future (which should be a lot more doable with auto layout than the older struts system) – Ronald Oussoren Jun 19 '13 at 12:16
  • Can't wait for your blog post then! ;) Meanwhile, I give up and will just use a nib. – Alex B Jun 19 '13 at 12:22