简体   繁体   English

将CLI命令添加到Cocoa应用程序?

[英]Add CLI-commands to a Cocoa app?

I am looking for a way to send commands (with arguments) from a command line tool to a running Cocoa app. 我正在寻找一种方法将命令(带参数)从命令行工具发送到正在运行的Cocoa应用程序。 Is this possible? 这可能吗?

If my the Cocoa app is open and running I would like it to receive basic events from the terminal. 如果我的Cocoa应用程序已打开并运行,我希望它能够从终端接收基本事件。 How can I communicate between a CLI-target and Cocoa-target? 如何在CLI目标和Cocoa目标之间进行通信?

The only way this I have found is to use the AppleScript and add scriptability to the app. 我找到的唯一方法是使用AppleScript并为应用添加脚本性。 Is this the only way to go? 这是唯一的出路吗?

To send commands to your app from the terminal you could use URL Scheme. 要从终端向您的应用程序发送命令,您可以使用URL Scheme。

Register a scheme in info.plist ("yourScheme" is the scheme identifier in this example), then listen for events: 在info.plist中注册一个方案(“yourScheme”是本例中的方案标识符),然后监听事件:

NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(handleGetURLEvent(event:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))

and get the content of an event with 并获取事件的内容

@objc func handleGetURLEvent(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
    if let text = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)) {
        // you may want to filter the commands here
        if text.contains("yourScheme://doStuff") {

        } else if ...
    }
}

Example from the terminal: 终端示例:

$ open "yourScheme://doStuff"
$ open "yourScheme://doSomethingElse"

I recently created a command-line app for a Swift app that I've distributed on the Mac App Store. 我最近为我在Mac App Store上发布的Swift应用程序创建了一个命令行应用程序。 I was able to do this by: 我能够做到这一点:

  1. Making the App scriptable (see Implementing a Scriptable Application ) 使应用程序可编写脚本(请参阅实现可编写脚本的应用程序

  2. Generating a library to script the app (see Using Scripting Bridge ) 生成库以编写应用程序脚本(请参阅使用脚本桥

  3. Creating a separate CLI target using the library embedded from step 2 使用步骤2中嵌入的库创建单独的CLI目标

I've happend to make the code open source (aside from the app which was made scriptable). 我发生了使代码开源(除了可编写脚本的应用程序)。 The scripted library is called EmporterKit , which also includes a Makefile to generate the scripted headers, and the CLI is called emporter-cli , which was built with a library called YDCommandKit , which makes writing CLI apps way easier. 脚本库名为EmporterKit ,它还包含用于生成脚本头的Makefile ,CLI称为emporter-cli ,它是使用名为YDCommandKit的库构建的 ,这使得编写CLI应用程序变得更加容易。

This topic is extremely broad and the options are limited while keeping in step with modern security practices on macOS. 这个主题非常广泛,选项有限,同时与macOS上的现代安全实践保持同步。 Honestly, you should just stick to using URL schemes if possible, but if you want a full-featured command-line app, this is probably the only way to go. 老实说,如果可能的话,你应该坚持使用URL方案,但如果你想要一个功能齐全的命令行应用程序,这可能是唯一的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM