Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Created June 2, 2014 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharoonthomas/6f5af82e347e94451d2e to your computer and use it in GitHub Desktop.
Save sharoonthomas/6f5af82e347e94451d2e to your computer and use it in GitHub Desktop.
Handling the GURL event triggered by OSX when it handles a URL scheme registered to an application.
import objc
NSAppleEventManager = objc.lookUpClass('NSAppleEventManager')
NSObject = objc.lookUpClass('NSObject')
class GURLHandler(NSObject):
def handleEvent_withReplyEvent_(self, event, reply_event):
url = event.descriptorForKeyword_(fourCharToInt('----')).stringValue()
# Do something with the url
# A helper to make struct since cocoa headers seem to make
# it impossible to use kAE*
fourCharToInt = lambda code: struct.unpack('>l', code)[0]
manager = NSAppleEventManager.sharedAppleEventManager()
manager.setEventHandler_andSelector_forEventClass_andEventID_(
GURLHandler.alloc(), 'handleEvent:withReplyEvent:',
fourCharToInt('GURL'), fourCharToInt('GURL')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment