1

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

谢谢!

4

1 回答 1

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,我没有说明。但我认为创建一个 Contents 目录和一个包含所需元素的 Info.plist 文件也可以。

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

于 2014-02-20T16:43:19.963 回答