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](https://github.com/michaeltyson/Commandline-Cocoa)
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:@"http://atastypixel.com/favicon.ico"]]]; [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 #import
s with the -include
parameter.
Grab the script from the [Commandline-Cocoa GitHub project](https://github.com/michaeltyson/Commandline-Cocoa) — make it executable (chmod +x runcocoa.sh
), and move it to /usr/local/bin/runcocoa
.
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.
Thank you for sharing your shell script (runcocoa.sh)! 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.
Please GitHub this!
Very cool, and very useful! Thanks for sharing, and yeah, you should put on github.
Thanks guys! I’ve created a GitHub project, now: https://github.com/michaeltyson/Commandline-Cocoa
Great, I made a Homebrew formula: https://github.com/mxcl/homebrew/commit/dbbe308842
Sweet! Cheers =)
I have translate your blog into Chinese.
Is that ok? The post
That’s great, Kebot, cheers!
The -ios parameter at least doesn’t seem to work with Lion. I get
error: UIKit/UIKit.h: No such file or directory
etc.