I’ve been spending a large amount of time in Xcode and Cocoa lately. I found I would like to customize the file templates created by Xcode for new classes and the like. After some research, I found it is reasonably easy to add your own set of file templates. Here is how you do it.
Apple’s Default Templates
First, take a look at the default template files Apple ships. They are located in /Library/Application Support/Apple/Developer Tools/File Templates. The structure is very simple. When you create a new file in Xcode, the templates are listed as starting points. Each directory creates a new category. A template is composed of a plist file and a set of template files. For example, the Objective-C class template is three files – class.m, class.h, and TemplateInfo.plist.
Create a Template Tree
I’d prefer to leave Apple’s tree alone so I tried creating my own template tree. It turns out that Xcode does the right thing and integrates any templates you have in your home directory’s Library folder with the ones in /Library.
Create a new directory structure ${HOME}/Library/Application Support/Apple/Developer Tools/File Templates. Note that it is likely that only the File Templates directory is missing if you have been running Xcode.
Next, create a category for your new file templates. Either create your own or use the same structure Apple uses. For example, to add another Objective-C class template for Cocoa, create a Cocoa folder and put your template in there. It will show up in the Cocoa section along with Apple’s templates.
Name the template directory with the name you want to show up in the assistant. Edit the plist file to reflect the descriptive text and any of the other properties you want. There appear to a main and supporting template file structure which allows you to create up to two files.
The template files themselves use a simple macro markup >. You should be able to add your own macro definitions by the following:
defaults write com.apple.Xcode PBXCustomTemplateMacroDefintions ‘{ “MACRO1″=”value”; “MACRO2″=”value2″; }’
ORGANIZATIONNAME is used in the Apple templates. You might want to create a EULA or copyright macro for example.
A couple other points:
- If you use the same name and location as an Apple template, yours will override it.
- Empty directories are ignored
- Nested subdirectories create subcategories. You can create a multi-level hierarchy.



One Comment
Great HOWTO, thanks for sharing it.
ODY