Command line tool interacts with Cocoa application

I have a Cocoa Document-Based Application (text editor) and I want to be able to interact with it from the command line.

For example, I would like to install it as an editor for entering git / svn commit messages on the command line.

Assuming I'm creating a command line tool using Foundation, what is the best way for my command line tool to interact with the GUI?

Obviously, I can use standard open events so that my application opens a specific file, but I also need a command line application to wait for the graphical interface to work with the document (the user closes the editor window) before exiting (similar to mate -w file.txt in TextMate command line tool or equivalent in other other text file editors).

TextMate 2 uses a socket file . Is this a better approach? If possible, I would like to use something higher, perhaps the NSDistributedNotificationCenter .

+6
source share
3 answers

NSDistributedNotificationCenter is likely to work fine if this is the level of abstraction you prefer. Its interfaces are similar to NSNotificationCenter . Regarding NSDistributedNotificationCenter :

  • it is limited to plist types
  • reduction notifications allowed.
  • it is expensive"
  • the delay can be unpredictable.
  • Sandboxes cannot use the userInfo: parameter userInfo:

You can find sockets preferable if you want to convey a lot of information or want something more reliable / predictable.

+2
source

If you look at the source code for iTerm:

svn co https://iterm.svn.sourceforge.net/svnroot/iterm iterm

You can see how they did it. They used Growl notifications. Growl is a comprehensive notification system that will allow you to really control what is happening and properly respond to everything. You can find more information about Growl here:

http://growl.info/

+1
source

You can add a URL scheme to your application in the project details panel.

When someone tries to go somewhere to url yourscheme://info/for/your/app , he will launch your application if it has not been launched and passes arguments.

Here is the code in your application for handling URLs

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // ... [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; } - (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; // .. process your url } 
0
source

Source: https://habr.com/ru/post/927531/


All Articles