我正在尝试将预先存在的 perl 脚本连接到 Mac OS 10.8/10.9 通知中心。
以下代码是我尝试过的,由于某种原因我无法让它工作:
#!/usr/bin/perl
use strict;
use warnings;
use Foundation;
my $notification = NSUserNotification->alloc()->init();
my $string = NSString->stringWithCString_("Test");
$notification->setTitle_($string);
$notification->setInformativeText_($string);
# the following line seems to be the issue. According to PerlObjCBridge->setTracing(1)
# the value returned from this is \0 and not an object, which is what it should be.
my $center = NSUserNotificationCenter->defaultUserNotificationCenter();
# this should be the line to display the notification, but it doesn't work because
# $center is \0 instead of an object
$center->deliverNotification_($notification);
NSUserNotificationCenter->defaultUserNotificationCenter()
关于为什么不返回对象的任何帮助?
另外,我意识到我可以打电话
system(osascript -e 'display notification "test" with title "test"');
但是,好吧,我鄙视 AppleScript。我最初的印象是调用 applescript 引擎比调用 objc 有更多开销,这可能完全没有根据。
谢谢!