PerlObjCBridge和OS X通知中心的问题

时间:2013-12-31 18:05:38

标签: objective-c macos perl foundation nsusernotificationcenter

我正在尝试将预先存在的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有更多的开销,这可能是完全没有根据的。

谢谢!

1 个答案:

答案 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.";
    }
    
  • 找一个劫持Info.plist(或创建自己的Bundle)的应用程序
  • 如果劫持捆绑包,请在脚本目录输出中创建指向捆绑包的Contents目录的软链接。
  • 如果创建自己的Bundle,我没有说明。但我认为创建一个目录目录,Info.plist file with the required elements也可以正常工作。

我劫持了Microsoft Outlook的捆绑包,它显示了一条通知,好像它来自Outlook。