【发布时间】:2014-01-18 15:15:50
【问题描述】:
我正在尝试将预先存在的 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开销更大,这可能是完全没有根据的。
谢谢!
【问题讨论】:
-
当然,经过数小时的搜索,我发现了这个stackoverflow.com/questions/11712535/… 似乎 CLI 应用程序直接被禁止使用通知......对我来说似乎有点傻。
-
我相信这更多是因为缺少捆绑包而不是 cli,因为您可以从 cli 启动任何普通应用程序。
标签: objective-c macos perl foundation nsusernotificationcenter