2010-06-27 17 views
5

मैं एक छोटे से आवेदन लिखने के लिए ग्रोथ पायथन बाइंडिंग (ग्रोल्व रिपोजिटरी से Growl.py v0.7) का उपयोग करने की कोशिश कर रहा हूं। वर्तमान में अनुपलब्ध सुविधाओं में से एक है पायथन पर भेजे गए क्लिक अधिसूचना।ग्रोथ पायथन क्लिक फीडबैक के साथ बाध्यकारी?

मैं उद्देश्य-सी में जानता हूं, जब कोई उपयोगकर्ता अधिसूचना पर क्लिक करता है, तो यह चल रहे एप्लिकेशन को ट्रिगर भेज देगा। मैं पाइथन बाइंडिंग के साथ एक समान काम करना चाहता हूं। जब कोई उपयोगकर्ता अधिसूचना पर क्लिक करता है, तो मैं पाइथन प्रोग्राम ब्राउज़र में एक यूआरएल खोलना चाहता हूं (या घटना को किसी अन्य तरीके से संभालता हूं)।

कोई विचार है कि मैं इसे कैसे पूरा कर सकता हूं?

अद्यतन: सिंथेसाइज़रपेटेल के लिए धन्यवाद जो एक आशाजनक समाधान प्रदान करता है और मैं अपने शब्दों को शेर पर काम करता हूं। दुर्भाग्यवश, मैक से बाहर निकलना शुरू कर रहा हूं, इसलिए मैं अब मैक प्रोग्रामिंग नहीं करता हूं।

Discussion Growl PyObjC not working with PyObjC 2.2b3

Source code

+0

कोई भाग्य यहाँ? मुझे भी जानना अच्छा लगेगा। –

+0

यह अभी भी एक खुला प्रश्न है ~ – Patrick

+0

क्या आप इसे सभी उगने वाली अधिसूचनाओं को पकड़ने के लिए देख रहे हैं या केवल एक पायथन ऐप जिसमें फ्लाईल अधिसूचनाएं भेजने की क्षमता है और इसके लिए माउस को पकड़ने में सक्षम होने के लिए जब उपयोगकर्ता इसे खारिज कर देता है? – synthesizerpatel

उत्तर

1

आप यही चाहते है: हालांकि, मैं कुछ डिबगिंग के रूप में यह अभी भी बर्फ तेंदुए पर काम नहीं है, और यहाँ क्यों है क्या किया?

#!/usr/bin/env python 
# 
# pyGrr! 
# 
# This code was originally found @ 
# http://www.cocoaforge.com/viewtopic.php?f=6&t=13359&p=91992&hilit=pyobjc+growl 
# It is (to the best of our knowledge) the work of user 'tooru' on the same 
# website. 
# 
# I make no claim to this code, all I did was get it working with PyObjC on Lion 
# reformatted it a bit and added some more verbose explanations of what the script 
# does. To be honest, I haven't touched pyobjc in a couple years and I was amazed 
# that it still works! Even more amazed that I was able to get this example working 
# in about 20 minutes. 
# 
# Great job tooru! 
# 
# I have verified this code works with the following combination of 
# packages/versions 
# 
# * OSX Lion 10.7.3 
# * Python 2.7 
# * Growl 1.3 
# * Growl SDK 1.3.1 
# 
# 
# - Nathan Ramella [email protected] (http://www.remix.net) 
################################################################################## 

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() 
    # Create the statusbar item 
    self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength) 

    self.statusitem.setHighlightMode_(1) # Let it highlight upon clicking 
    self.statusitem.setToolTip_('pyGrr!') # Set a tooltip 
    self.statusitem.setTitle_('pyGrr!') # Set an initial title 

    # Build a very simple menu 
    self.menu = NSMenu.alloc().init() 
    menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
     'Send a Grr!', 
     'rcNotification:', 
     '' 
    ) 
    self.menu.addItem_(menuitem) 

    # Default event 
    menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
     'Quit', 
     'terminate:', 
     '' 
    ) 
    self.menu.addItem_(menuitem) 

    # Bind it to the status item 
    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's 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, 
    } 

    # don't know if it is working or not 
    def applicationNameForGrowl(self): 
    """ 
    Identifies the application. 
    """ 
    return 'rcGrowlMacTidy' 

    #def applicationIconDataForGrowl(self): 
    """ 
    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 
    """ 
    #icon = NSImage.alloc().init() 
    #icon = icon.initWithContentsOfFile_(u'remix_icon.tiff') 
    #return icon 

    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's already running it 
    won't need to run it again? 
    """ 
    print "growl IS READY" 


if __name__ == "__main__": 

    # Both 'growlnotify' and this script seem to have the following 
    # error after emitting a Grr! 
    # 
    # Error Domain=GCDAsyncSocketErrorDomain Code=4 "Read operation timed out" 
    # UserInfo=0x7fa444e00070 {NSLocalizedDescription=Read operation timed out} 
    # 
    # So, we redirect stderr to /dev/null so that it doesn't muck up 
    # the output of this script. Some folks say upgrading Growl fixes it, 
    # others still have the problem. Doesn't seem to make much of a difference 
    # one way or another, things still seem to work regardless. 

    fp = os.open('/dev/null', os.O_RDWR|os.O_CREAT, 0o666) 
    dupped = os.dup(2) 
    os.dup2(fp, 2) 

    # set up system statusbar GUI 
    app = NSApplication.sharedApplication() 
    delegate = MenuMakerDelegate.alloc().init() 
    app.setDelegate_(delegate) 

    # set up growl delegate 
    rcGrowlDelegate=rcGrowl.new() 
    rcGrowlDelegate.rcSetDelegate() 
    AppHelper.runEventLoop() 
+0

हाय, लिपि के लिए धन्यवाद। दुर्भाग्य से, यह मेरे लिए काम नहीं करता क्योंकि मेरे पास उगने के साथ बर्फ तेंदुए है 1.2.2 – Patrick

+0

यदि किसी भी मामले में आपको उस मामले में कम समस्याएं होनी चाहिए। : डी लेकिन, जैसा कि यह है, यह वही है जो आप खोज रहे हैं। मुझे यह जानकर उत्सुकता होगी कि आपको कौन से त्रुटि संदेश मिल रहे हैं। – synthesizerpatel

+0

हाँ, मुझे और विस्तार प्रदान करना चाहिए था ... स्क्रिप्ट ने जो कुछ भी चाहते हैं उसके बारे में वादा किया था। कार्यक्रम चलाने के बाद, उसने दो विकल्पों के साथ स्थिति मेनू बनाया। हालांकि, मैंने "ग्रोअर भेजें" पर क्लिक करने के बाद, मुझे वास्तविक ग्रोथ अधिसूचना के बिना "केवल एक उगाना अधिसूचना भेजना ..." के साथ कंसोल में एक संदेश मिला। – Patrick