【问题标题】:How to get custom url in a Mac command line application?如何在 Mac 命令行应用程序中获取自定义 url?
【发布时间】:2018-01-31 02:09:33
【问题描述】:

现在我想编写一个小 CLI 应用程序,它可以获取自定义方案 url 来执行诸如启动其他应用程序之类的操作。 我找到了一个方法, 首先,编辑“info.plist”,在“URL 类型”下添加一个“URL Schemes”,例如我添加一个“mySchemes”项。 其次,在“AppDelegate”中安装一个事件处理程序,

-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:)
                     forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
-(void) handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSLog(@"%@",url);
    // Do something next   
}

但我想在命令行应用程序中实现它,我如何在没有 cocoa 框架的情况下获取此事件?我不需要用户界面

【问题讨论】:

  • 或者在没有 UI 的情况下使用 cocoa
  • 您可以拥有一个没有菜单或窗口的可可应用程序...您可以将其作为服务或作为单次应用程序运行...

标签: objective-c macos cocoa


【解决方案1】:

在 Info.plist 中设置 LSUIElement 将允许您在没有 UI 的情况下运行您的应用程序。来自 Apple 的Information Property List Key Reference

如果此键设置为 YES,Launch Services 会将应用程序作为代理应用程序运行。代理应用程序不会出现在 Dock 或强制退出窗口中。尽管它们通常作为后台应用程序运行,但如果需要,它们可以进入前台以呈现用户界面。单击属于代理应用程序的窗口会使该应用程序前进以处理事件。

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 2010-11-06
    • 2012-05-31
    • 1970-01-01
    • 2011-06-04
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多