Tôi đang cố gắng kết nối với trung tâm thông báo Mountain Lion thông qua python. Tôi đã cài đặt pyobjc và đang làm theo hướng dẫn here và here. Xem thêm: Working with Mountain Lion's Notification Center using PyObjCNSUserNotificationCenter.defaultUserNotificationCenter() trả về Không có trong python
Dưới đây là mã của tôi:
import Foundation, objc
import AppKit
import sys
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
""" Python method to show a desktop notification on Mountain Lion. Where:
title: Title of notification
subtitle: Subtitle of notification
info_text: Informative text of notification
delay: Delay (in seconds) before showing the notification
sound: Play the default notification sound
userInfo: a dictionary that can be used to handle clicks in your
app's applicationDidFinishLaunching:aNotification method
"""
notification = NSUserNotification.alloc().init()
notification.setTitle_(title)
notification.setSubtitle_(subtitle)
notification.setInformativeText_(info_text)
notification.setUserInfo_(userInfo)
if sound:
notification.setSoundName_("NSUserNotificationDefaultSoundName")
notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
Khi tôi gọi là thông báo chức năng với các đối số tôi nhận được một lỗi thuộc tính:
AttributeError: 'NoneType' object has no attribute 'scheduleNotification_'
Tôi không hiểu tại sao NSUserNotificationCenter.defaultUserNotificationCenter()
là trả về một đối tượng NoneType. Tôi đã không thể truy vấn bất cứ điều gì về vấn đề này trên internet hoặc SO.
Bạn không thể sao chép điều này chỉ với ba dòng mã ('import objc; NSUserNotificationCenter = objc.lookUpClass ('NSUserNotificationCenter'); in NSUserNotificationCenter.defaultUserNotificationCenter()') thay vì toàn bộ hàm? –
Ah! Tôi cho rằng tôi có thể có! – deepak
FWIW, thử nghiệm ba dòng hoạt động cho tôi bằng cách sử dụng MacPorts hiện tại 'python27' (@ 2.7.3_1) và' py27-pyobjc' (@ 2.5.1_0): nó trả về một thể hiện của '_NSConcreteUserNotificationCenter'. –