5

Sto provando a connettermi al centro di notifica Mountain Lion tramite python. Ho installato pyobjc e sto seguendo le istruzioni here e here. Vedi anche: Working with Mountain Lion's Notification Center using PyObjCNSUserNotificationCenter.defaultUserNotificationCenter() restituisce None in python

Ecco il mio codice:

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) 

Quando chiamo la funzione di comunicare con argomenti ottengo un errore di attributo:

AttributeError: 'NoneType' object has no attribute 'scheduleNotification_'

non capisco il motivo per cui NSUserNotificationCenter.defaultUserNotificationCenter() è restituire un oggetto NoneType. Non sono stato in grado di eseguire query su questo argomento su Internet o SO.

+1

Non è possibile riprodurre questo con solo tre righe di codice ('import objc; NSUserNotificationCenter = objc.lookUpClass ('NSUserNotificationCenter'); stampare NSUserNotificationCenter.defaultUserNotificationCenter()') invece dell'intera funzione? –

+0

Ah! Suppongo che potrei avere! – deepak

+0

FWIW, il test a tre righe funziona per me utilizzando gli attuali MacPorts 'python27' (@ 2.7.3_1) e' py27-pyobjc' (@ 2.5.1_0): restituisce un'istanza di '_NSConcreteUserNotificationCenter'. –

risposta

2

Quindi, questo funziona perfettamente usando il consiglio di Ned sull'uso di python predefinito. Ha funzionato anche quando ho installato pyobjc sulla distribuzione enthought a 32 bit. Mi sembra che pyobjc funzioni solo su distribuzioni python a 32 bit (non posso confermarlo, ma finora sembra che sia così).

(Nota: quando ho postato la domanda, avevo installato pyobjc sulla distribuzione con enthought a 64 bit).

+1

pyobjc funziona perfettamente con una distribuzione python a 64 bit. –

+0

Tuttavia, un numero di framework Apple non funzionerà correttamente a meno che il binario non sia in un bundle (applicazione o plugin). Enthought installa un framework o solo una semplice distribuzione unix? –

+0

Enthought installa come framework. – deepak