Command-line Cocoa

It is possible to write command-line applications using Cocoa. However, you have to be careful. The only way to test such an app is by ssh’ing into your own box. You can’t just use the Terminal. Some APIs can only be called if they have access to the console or if root is running them. You won’t know that until you run your command-line app via ssh. if You get a message such as:
kCGErrorRangeCheck : Window Server communications from outside of session allowed for root and console user only
INIT_Processeses(), could not establish the default connection to the WindowServer.Abort
That is what’s going on.

The fix is simple, just don’t use the offending code. The offending code could be anything. I haven’t found a definitive list yet. Clearly anything related to the Window Server is out. Also verboten is anything related to the Process Manager.

To compensate, there is the little-known function “NSApplicationLoad” that will bootstrap enough of the Cocoa framework to run things that normally do not work until “awakeFromNib” in a GUI application. If you don’t want to call “NSApplicationMain”, go ahead and call “NSApplicationLoad”, create your own autorelease pool, and go forth on the command line.

Leave a Reply