我在用
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
Run Code Online (Sandbox Code Playgroud)
在Mac中显示通知.但是,在单击通知时,我将被重定向到applescript编辑器.我是否可以将用户重定向到URL或单击生成的通知时打开目录?
我正在尝试从我的python脚本向Mountain Lion发送通知,并对通知的点击做出反应.现在可以完美地发送通知.但是,我无法让Lion在点击后回拨我的剧本.
这就是我的工作.我实现了一个Notification类.该类实例的唯一目的是通过调用提供通知notify().在同一方法中,我将对象设置为app的委托.
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"]])
Run Code Online (Sandbox Code Playgroud)
现在我希望applicationDidFinishLaunching_()在点击通知时被调用.不幸的是,从未发生过 我究竟做错了什么?
如何在mac上制作一个简单的弹出气球消息。我不想使用 NSUserNotification。使用 python-2.7 和 osx 10.8.5。POP-UP 不应该有任何按钮。弹出窗口应该来,显示消息并自动去。它也应该与 py2app 正确打包。
import objc
import Foundation
import AppKit
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
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)
def notificationBalloon(title,msg):
notify(title1, msg1,"", sound=False)
Run Code Online (Sandbox Code Playgroud)