【问题标题】:Issue with PerlObjCBridge and the OS X notification centerPerlObjCBridge 和 OS X 通知中心的问题
【发布时间】: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


【解决方案1】:

我是这样解决这个问题的。

  • 在运行 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)
  • 如果劫持捆绑包,请在脚本输出的捆绑包目录中创建指向捆绑包内容目录的软链接。
  • 如果创建您自己的 Bundle,我没有说明。但我认为创建一个 Contents 目录和 Info.plist file with the required elements 也可以。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 2014-01-06
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多