我正在尝试将预先存在的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有更多的开销,这可能是完全没有根据的。
谢谢!
答案 0 :(得分:0)
以下是我解决这个问题的方法。
运行perl脚本时找到“捆绑目录”。
#!/usr/bin/perl
use Foundation;
my $bundle = NSBundle->mainBundle();
if ($bundle) {
print $bundle->bundlePath()->cString() . "\n";
print $bundle->bundleIdentifier()->cString() . "\n";
} else {
print "Bundle is not an object.";
}
我劫持了Microsoft Outlook的捆绑包,它显示了一条通知,好像它来自Outlook。