6

I'm writing a small cross-platform wxPython app, however on every platform I need to use some platform-specific API. On Mac OS it can be done using PyObjC.

I'm searching for tutorial on how to use PyObjC. However, all I found so far were tutorials with Xcode. I want my app to be able to run on mac/win/lin, without changes, and I don't want to develop it in Xcode. Is there a way?

UPD. To be more specific I need to access some pen-tablet events from Mac OS X and I wanted to use PyObjC for that (I don't see any other ways).

Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
  • 2
    I also had a same question. I also searched but could not find a decent solution. I cooked up some AppleScript code as a wrapper and could use my application on Mac. But being able to use PyObjC without XCode will be nice. – Sam Feb 14 '11 at 16:06

2 Answers2

11

You can import the Foundation and AppKit modules, then subclass NSApplication. But maybe this isn't what you're looking for, if your pyobjc code isn't the entry point for your code. Could give more specifics about what you're trying to do with pyobjc?

Here's a quick example using pyobjc to make a simple status bar app, without using xcode:

import objc
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper

class MyApp(NSApplication):

    def finishLaunching(self):
        # Make statusbar item
        statusbar = NSStatusBar.systemStatusBar()
        self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
        self.icon = NSImage.alloc().initByReferencingFile_('icon.png')
        self.icon.setScalesWhenResized_(True)
        self.icon.setSize_((20, 20))
        self.statusitem.setImage_(self.icon)

        #make the menu
        self.menubarMenu = NSMenu.alloc().init()

        self.menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Click Me', 'clicked:', '')
        self.menubarMenu.addItem_(self.menuItem)

        self.quit = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
        self.menubarMenu.addItem_(self.quit)

        #add menu to statusitem
        self.statusitem.setMenu_(self.menubarMenu)
        self.statusitem.setToolTip_('My App')

    def clicked_(self, notification):
        NSLog('clicked!')

if __name__ == "__main__":
    app = MyApp.sharedApplication()
    AppHelper.runEventLoop()

You can then use py2app to make it distributable:

from distutils.core import setup
import py2app

NAME = 'myapp'
SCRIPT = 'myapp.py'
VERSION = '0.1'
ID = 'myapp'

plist = dict(
     CFBundleName                = NAME,
     CFBundleShortVersionString  = ' '.join([NAME, VERSION]),
     CFBundleGetInfoString       = NAME,
     CFBundleExecutable          = NAME,
     CFBundleIdentifier          = 'com.yourdn.%s' % ID,
     LSUIElement                 = '1', #makes it not appear in cmd-tab task list etc.
)


app_data = dict(script=SCRIPT, plist=plist)

setup(
   app = [app_data],
   options = {
       'py2app':{
           'resources':[
               ],
           'excludes':[
               ]
           }
       }
)
ryan
  • 884
  • 1
  • 9
  • 14
0

What do you need Xcode for? If you need it for the windows/gui (*.nib, *.xib files), then you should perhaps search for 'creating *.nib, *xib without Xcode'. That's only a search hint and no satisfying answer.

cl_progger
  • 413
  • 2
  • 6
  • 10
  • 1
    I found 2 approaches in this [intro](http://pyobjc.sourceforge.net/documentation/pyobjc-core/intro.html) and there was no word that you must use Xcode. On the bottom there is something with **py2app** and **IDE approach: Xcode**. I hope this helps you. How said, I am no expert. – cl_progger Feb 21 '11 at 22:25