2012-08-30 20 views
8

का उपयोग कर केंद्र के साथ कार्य करना। नोटिफिकेशन भेजना अभी तक पूरी तरह से काम करता है। लेकिन फिर भी मैं शेर को एक क्लिक पर अपनी स्क्रिप्ट वापस कॉल करने में सक्षम नहीं था।माउंटेन शेर की अधिसूचना मैं अपने अजगर स्क्रिप्ट से माउंटेन शेर पर सूचनाएं भेजने और सूचनाओं पर क्लिक करता है पर प्रतिक्रिया करने की कोशिश कर रहा हूँ PyObjC

यहाँ मैं क्या कर रहा है। मैंने एक अधिसूचना वर्ग लागू किया। उस वर्ग के उदाहरण का एकमात्र उद्देश्य notify() का आह्वान करके अधिसूचनाएं प्रदान करना है। उसी विधि में मैंने वस्तु को ऐप के प्रतिनिधि को सेट किया।

import Foundation 
import objc 
import AppKit 

class MountainLionNotification(Foundation.NSObject, Notification): 

    def notify(self, title, subtitle, text, url): 
     NSUserNotification = objc.lookUpClass('NSUserNotification') 
     NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter') 
     notification = NSUserNotification.alloc().init() 
     notification.setTitle_(str(title)) 
     notification.setSubtitle_(str(subtitle)) 
     notification.setInformativeText_(str(text)) 
     notification.setSoundName_("NSUserNotificationDefaultSoundName") 
     notification.setUserInfo_({"action":"open_url", "value":url}) 
     AppKit.NSApplication.sharedApplication().setDelegate_(self) 
     NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification) 

    def applicationDidFinishLaunching_(self, sender): 
     userInfo = sender.userInfo() 
     if userInfo["action"] == "open_url": 
      import subprocess 
      subprocess.Popen(['open', userInfo["value"]]) 

अब मैं उम्मीद applicationDidFinishLaunching_() अधिसूचना पर एक क्लिक पर कहा जाता है। दुर्भाग्य से ऐसा कभी नहीं होता है। मैं क्या गलत कर रहा हूं?

+0

मैंने बिना किसी सफलता के, प्रतिनिधि विधि को सजावटी '@ objc.signature ("v @:^@") जोड़ने की कोशिश की है। – koloman

+0

अब मैं भी _() 'विधि मेरी' MountainLionNotification' आपत्ति डिफ़ॉल्ट सूचना केंद्र के प्रतिनिधि सेट और प्रोटोकॉल 'लागू करने की कोशिश की userNotificationCenter_didActivateNotification। कोई सफलता नहीं है! – koloman

+0

अरे, आप घटना पाश प्रारंभ किए बिना सूचनाएं सिर्फ एक अजगर स्क्रिप्ट/दुभाषिया से दिखाने के लिए प्राप्त करने में सक्षम थे? मुझे – GP89

उत्तर

8

ठीक है, यह मिल गया। AppHelper.runEventLoop() नहीं चलाया। जाहिर है एक चेहरे की गलती। निम्नलिखित कोड काम करता है:

class MountainLionNotification(Foundation.NSObject, Notification): 

    def notify(self, title, subtitle, text, url): 
     NSUserNotification = objc.lookUpClass('NSUserNotification') 
     NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter') 
     notification = NSUserNotification.alloc().init() 
     notification.setTitle_(str(title)) 
     notification.setSubtitle_(str(subtitle)) 
     notification.setInformativeText_(str(text)) 
     notification.setSoundName_("NSUserNotificationDefaultSoundName") 
     notification.setHasActionButton_(True) 
     notification.setOtherButtonTitle_("View") 
     notification.setUserInfo_({"action":"open_url", "value":url}) 
     NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self) 
     NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification) 

    def userNotificationCenter_didActivateNotification_(self, center, notification): 
     userInfo = notification.userInfo() 
     if userInfo["action"] == "open_url": 
      import subprocess 
      subprocess.Popen(['open', userInfo["value"]])