Something I’ve been meaning to do for a long, long time, is set up some custom project templates in Xcode. The problem is, it doesn’t really take that long to tweak all the little things in a new project, so when I’m in the mood to start something new, I usually just go through all the grunt steps.
Today I decided to put together a template that should cover at least 90% of the grunt work for typical applications I start work on. I started with a great example from Jon Wight of Toxic Software. His “CocoaApp” template did a lot of the things I wanted to do, but lacked a few whistles and bells. I thought I’d just work from his and it would be easy.
Instead it was amazingly, unbelievably difficult. The major complication comes from persuading Xcode’s template manager to actually do the substitutions it promises to do. See, at the root of the template system is this behavior where Xcode will replace certain tags in your files with appropriate values for the new project. The tags are surrounded by French quotes, chevrons, whatever … you know: «these». That’s fine, but depending on whether the file is in MacRoman, UTF-8, or UTF-16 format, the chevrons are a different value. Ugh, it’s infuriating the way they would sneak back and forth. I still don’t quite understand the logic applied by Xcode in deciding which encoding to look for, when. To make matters more complicated, I strived for (and accomplished) something truly amazing: an end to the “NewApplication” text in new project nib files. To achieve this I had to save the nib as XML format, but even then it was difficult to get the files into a suitable format that was still parseable as XML, yet would satisfy Xcode’s template system. This is so fragile that the nib files in the template are not even openable in Interface Builder – so unfortunately you won’t be able to tweak them without some serious hacking.
But the end result is nothing short of amazing, I’m proud to say. Download the Red Sweater Cocoa Application template and install it on your system. Then, when you go to create your next project, enjoy all the following frills right from the start:
- Automatically names all menu items in MainMenu.nib. No more “NewApplication”!
- Automatically instantiates a delegate class named [AppName]Delegate, connected as the delegate from NSApplication, and creates the supporting source files.
- Automatically creates an AppleScript scripting definition, pointed at by the Info.plist. The scripting definition contains a skeletal dictionary with a “[AppName] Suite”, ready for you to expose your model through.
- Automatically sets the CFBundleVersion to the subversion build number through a script build phase.
- Centralizes the marketing version in one build setting called “APPLICATION_VERSION.” Just change this build setting to easily update your release version.
- Presets the application icon name to [AppName]. Just pop in a [AppName].icns file.
I think that covers it. I have kindly left out the private framework references to my Red Sweater frameworks. You should be able to edit the “Cocoa Application.pbproj” to fine-tune things to your liking, but be sure to remove the “acct.pbuser” files from within it after you do so, these have a habit of bringing some nasty values into the template which won’t get replaced by the template system.