More PyObjC
December 05, 2003
Having posted my first impressions of PyObjC, I decided to tackle a bigger (but still example sized) project with it.
Now, I present you DynamicMenus
This sample is quite old, and doesn’t show modern (py2app) building styles for PyObjC. The Code is still good example code, but I don’t have time to get it to build under teh new hotness. - Ryan Wilcox, February 2007
This sample application shows one how to use PyObjC to:
- Create a dynamic menu
- How to specify Python methods to get called when an action happens
- How to create an NSStatusItem
- Using Xcode with PyObjC
I’d also like to think it does a decent job showing how the breadth of Python’s standard library can reduce the amount of Cocoa routines you have to call to get something done. Because (for example) I’d rather not use NSDirectoryEnumerator when I can use something like for each in os.listdir(scriptPath)
. NSDirectoryEnumerator is fine and all - it just seems that every time I try to use it I spend 10 minutes looking up documentation on to how to use it.
This application will first scan your Scripts folder (in ~/Library/), checking for any Python scripts, ending in .py. Each of these scripts will then become a menu item in the NSStatusItem. Select that script from the status item and your script will run.
There’s also a window with a button. Pressing that button will create a menu via Objective-C routines.
Functionally, this application isn’t very useful - Apple’s Script Menu does a far better job at running (all kinds of) scripts via a menu selection. For someone hungry for PyObjC samples though, this shows some neat tricks.
At about 80 lines for the main controller class, I hope its small enough to be easily understood, and large enough to show real world problems (and solutions).
So check it out!