Objective-C + Cocoa on the Command Line

Sometimes there’s just one tiny snippet of Cocoa code that you want to test — maybe to find out the output of NSDateFormatter for various cases, testing out some text replacement routine, or testing out some image drawing code.

It’s often too much trouble to create a new XCode project and set up the framework to do one simple test, which is why I put together this little shell script that lets you run Cocoa code from the command line:

$ runcocoa 'NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"d MMM, h:mm a"]; NSLog(@"%@", [formatter stringFromDate:[NSDate date]]);'

2011-02-23 20:02:10.313 runcocoa-output[28025:903] 23 Feb, 8:02 PM

You have full access to all Cocoa libraries, and in iOS mode, access to most iOS stuff too, straight from the command line.

Update: This is now available as a GitHub project

You can invoke it either by specifying the code to execute on the command line as a parameter, or through standard input, so you can pipe stuff to it. This is particularly convenient for use with TextMate: Type some code, hit Cmd-Option-R, type “runcocoa”, hit enter, and the result appears as a tooltip.

Awesome, no?

You can include other frameworks (use “-include AudioToolbox/AudioToolbox.h -framework AudioToolbox" as command line arguments, for example), and run the code in gdb (with -gdb as a command-line argument).

You can also run it as iOS code by supplying the -ios commandline parameter — try this:

runcocoa -ios 'UIGraphicsBeginImageContext(CGSizeMake(100,100)); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(ctx,[[UIColor whiteColor] CGColor]); CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, 50, 0); CGContextAddArc(ctx, 50, 50, 50, M_PI/2.0, M_PI/2.0 + (2*M_PI), 0); CGContextFillPath(ctx); UIImage *icon = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@""]]]; [icon drawAtPoint:CGPointMake((100-[icon size].width)/2.0,(100-[icon size].height)/2.0)]; UIImage *i = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [UIImagePNGRepresentation(i) writeToFile:@"output.png" atomically:NO];'; open output.png;

Any other libraries to link in can be specified — command line arguments will be passed on unmolested to GCC. Add #imports with the -include parameter.

Grab the script from the Commandline-Cocoa GitHub project — make it executable (chmod +x, and move it to /usr/local/bin/runcocoa.

, , . Bookmark the permalink. Both comments and trackbacks are currently closed.


  1. Dave
    Posted February 23, 2011 at 11:16 pm | Permalink

    I have several empty projects (“EmptyFoundation”, “EmptyAppKit”, “EmptyiPhone”) that I keep around for explicitly this purpose. My EmptyFoundation project I keep open, and just throw stuff into the main() function to test. It’s really handy.

  2. Tom S.
    Posted February 24, 2011 at 3:19 am | Permalink

    Thank you for sharing your shell script (! As an Objective-C/Cocoa newbie, sometimes I just want to experiment with the API (to see how things work) rather than having to go through the effort of setting up an Xcode command line project. Your script will be used often.

  3. Posted April 19, 2011 at 7:00 pm | Permalink

    Please GitHub this!

  4. Posted April 27, 2011 at 5:29 pm | Permalink

    Very cool, and very useful! Thanks for sharing, and yeah, you should put on github.

  5. Posted April 28, 2011 at 10:40 am | Permalink

    Thanks guys! I’ve created a GitHub project, now:

  6. Posted June 30, 2011 at 6:40 am | Permalink

    I have translate your blog into Chinese.

    Is that ok? The post

  7. Posted July 22, 2011 at 3:50 am | Permalink

    The -ios parameter at least doesn’t seem to work with Lion. I get error: UIKit/UIKit.h: No such file or directory etc.