Is this what you want?
import objc
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
import time
import sys
import os
myGrowlBundle = objc.loadBundle(
"GrowlApplicationBridge",
globals(),
bundle_path = objc.pathForFramework(
'/Library/Frameworks/Growl.framework'
)
)
class MenuMakerDelegate(NSObject):
"""
This is a delegate for Growl, a required element of using the Growl
service.
There isn't a requirement that delegates actually 'do' anything, but
in this case, it does. We'll make a little menu up on the status bar
which will be named 'pyGrr!'
Inside the menu will be two options, 'Send a Grr!', and 'Quit'.
Send a Grr! will emit a growl notification that when clicked calls back
to the Python code so you can take some sort of action - if you're that
type of person.
"""
statusbar = None
state = 'idle'
def applicationDidFinishLaunching_(self, notification):
"""
Setup the menu and our menu items. Getting excited yet?
"""
statusbar = NSStatusBar.systemStatusBar()
self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
self.statusitem.setHighlightMode_(1)
self.statusitem.setToolTip_('pyGrr!')
self.statusitem.setTitle_('pyGrr!')
self.menu = NSMenu.alloc().init()
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Send a Grr!',
'rcNotification:',
''
)
self.menu.addItem_(menuitem)
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Quit',
'terminate:',
''
)
self.menu.addItem_(menuitem)
self.statusitem.setMenu_(self.menu)
def rcNotification_(self,notification):
"""
This is run when you select the 'Send a Grr!' menu item. It
will lovingly bundle up a Grr and send it Growl way.
"""
print "Sending a growl notification at", time.time()
GrowlApplicationBridge.notifyWithTitle_description_notificationName_iconData_priority_isSticky_clickContext_(
"Grr! - I'm a title!",
"This is where you put notification information.",
"test1",
None,
0,
False,
"this ends up being the argument to your context callback"
)
class rcGrowl(NSObject):
"""
rcGrowl registers us with Growl to send out Grrs on behalf
of the user and do 'something' with the results when a
Grr has been clicked.
For additional information on what the what is going on
please refer to the growl dox @
http://growl.info/documentation/developer/implementing-growl.php
"""
def rcSetDelegate(self):
GrowlApplicationBridge.setGrowlDelegate_(self)
def registrationDictionaryForGrowl(self):
"""
http://growl.info/documentation/developer/implementing-growl.php#registration
"""
return {
u'ApplicationName' : 'rcGrowlMacTidy',
u'AllNotifications' : ['test1'],
u'DefaultNotifications' : ['test1'],
u'NotificationIcon' : None,
}
def applicationNameForGrowl(self):
"""
Identifies the application.
"""
return 'rcGrowlMacTidy'
"""
If you wish to include a custom icon with the Grr,
you can do so here. Disabled by default since I didn't
want to bloat up this up
"""
def growlNotificationWasClicked_(self, ctx):
"""
callback for onClick event
"""
print "we got a click! " + str(time.time()) + " >>> " + str(ctx) + " <<<\n"
def growlNotificationTimedOut_(self, ctx):
"""
callback for timing out
"""
print "We timed out" + str(ctx) + "\n"
def growlIsReady(self):
"""
Informs the delegate that GrowlHelperApp was launched
successfully. Presumably if it already running it
won't need to run it again?
"""
print "growl IS READY"
if __name__ == "__main__":
fp = os.open('/dev/null', os.O_RDWR|os.O_CREAT, 0o666)
dupped = os.dup(2)
os.dup2(fp, 2)
app = NSApplication.sharedApplication()
delegate = MenuMakerDelegate.alloc().init()
app.setDelegate_( delegate )
rcGrowlDelegate=rcGrowl.new()
rcGrowlDelegate.rcSetDelegate()
AppHelper.runEventLoop()
source
share