2012-08-30 22 views
8

Tôi đang cố gắng gửi thông báo tới Mountain Lion từ tập lệnh python của tôi và phản hồi với các nhấp chuột trên thông báo. Gửi các thông báo hoạt động hoàn hảo tìm thấy ngay bây giờ. Nhưng tôi đã không thể có được Lion để gọi lại kịch bản của tôi khi nhấp chuột.Làm việc với Trung tâm thông báo của Mountain Lion bằng cách sử dụng PyObjC

Đây là những gì tôi làm. Tôi đã triển khai một lớp Thông báo. Mục đích duy nhất của một cá thể của lớp đó là cung cấp thông báo bằng cách gọi notify(). Trong cùng một phương pháp, tôi đặt đối tượng là ứng cử viên của đại biểu.

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"]]) 

Bây giờ tôi mong đợi applicationDidFinishLaunching_() được gọi khi nhấp vào thông báo. Thật không may là không bao giờ xảy ra. Tôi đang làm gì sai?

+0

Tôi đã cố thêm một trình trang trí '@ objc.signature (" v @:^@ ")' vào phương thức ủy nhiệm, mà không thành công. – koloman

+0

Bây giờ tôi cũng đã cố gắng thiết lập đối tượng 'MountainLionNotification' là đại diện của trung tâm thông báo mặc định và thực hiện phương thức' userNotificationCenter_didActivateNotification _() '. Stil không thành công! – koloman

+0

Xin chào, bạn có thể nhận thông báo hiển thị chỉ từ một tập lệnh/trình thông dịch python mà không cần bắt đầu vòng lặp sự kiện không? Tôi dường như không thể nhận được thông báo hiển thị bằng cách sử dụng mã ở trên – GP89

Trả lời

8

Ok, tìm thấy nó. Đã không chạy AppHelper.runEventLoop(). Rõ ràng là một sai lầm facepalm. Mã sau đây hoạt động:

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"]]) 
Các vấn đề liên quan