Mac Mountain Lion从CLI应用程序发送通知 [英] Mac Mountain Lion send notification from CLI app

查看:158
本文介绍了Mac Mountain Lion从CLI应用程序发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过命令行应用程序向通知中心发送通知?



示例

  #import< Cocoa / Cocoa.h> 

int main(int argc,const char * argv []){
NSLog(@Running notifications);

NSUserNotification * note = [[NSUserNotification alloc] init];
[note setTitle:@Test];
[note setInformativeText:@Woot];

NSUserNotificationCenter * center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:note];

return 0;
}

然后编译如下:


$ b b

  clang -framework cocoa /tmp/Notes.m 

我获得

  2012-07-29 16:08:35.642 a.out [2430:707] b $ b  

作为输出,但没有通知:(



NSUserNotificationCenter 工作原理 [[NSBundle mainBundle] bundleIdentifier] 返回正确的标识符所以,我写了一些swizzling代码,你可以找到 https://github.com/norio-nomura/usernotification



它可以发送 NSUserNotification ,没有应用程序包。


How can I send a notification to the notification center from a command line app? My attemps so far compile and run, but don't succeed in notifying me.

Example

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
    NSLog(@"Running notifications");

    NSUserNotification *note = [[NSUserNotification alloc] init];
    [note setTitle:@"Test"];
    [note setInformativeText:@"Woot"];

    NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
    [center scheduleNotification: note];

    return 0;
}

I then compile like:

clang -framework cocoa /tmp/Notes.m

and I get

 2012-07-29 16:08:35.642 a.out[2430:707] Running notifications

as output, but no notification :(

Is codesigning a factor in this?

解决方案

I found that NSUserNotificationCenter works when [[NSBundle mainBundle] bundleIdentifier] returns the proper identifier. So, I wrote some swizzling code that you can find at https://github.com/norio-nomura/usernotification

It can send an NSUserNotification without an Application Bundle.

这篇关于Mac Mountain Lion从CLI应用程序发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆