Custom Xcode Templates

·

·

When adding new files to a project in Xcode, you are presented with a menu to select file types to choose from that often are pre-populated with text or code. As part of the Xcode/gfortran contest I created one that is included in the package for generating a generic FORTRAN file. Creating new categories and templates is pretty easy and I’ll show you how to do it. These instructions are for Xcode 2.4, but I’d imagine that the mechanism hasn’t changed much for some time, so will probably work with earlier versions.

This tutorial is pretty basic and there a lot of advanced things you can do. But I normally tend to work on a project that needs a custom header or that requires certain include files almost all of the time. So this is a quick way to generate new files in Xcode with just a little bit of editing.

Template Location
Master templates for Xcode are stored in:

/Library/Application Support/Apple/Developer Tools/File Templates

If you open that directory in Finder, you will see a bunch of subfolders. Each one of these subfolders is a category that will be presented in the main window of Xcode when asked to select a file type. Open the folder labeled Cocoa and you’ll see something like the image below.

Each of these entries is a directory with the extension “.pbfiletemplate” and corresponds to a single file type (or set of file types) that you can choose from within the Cocoa class of files. Select the folder “Objective-C class.pbfiletemplate” and you will see that there are three files in there.

The header and Objective-C files are the actual template files, while the TempalteInfo.plist file is that tells Xcode which files to use as the template, and can include additional information such as a description of the files. The files can be organized as show below or using XML.

{
	MainTemplateFile = "class.m";
	CounterpartTemplateFile = "class.h";
	Description = "An Objective-C class file, with an optional header which includes the  header.";
}

Creating a custom template

We’ll begin by creating a new category and then populate it with a file template for C.

1) In /Library/Application Support/Apple/Developer Tools/File Templates create a new folder called “MacResearch” (no quotes).

2) In that directory create another folder called “MacResearch and C Rock.pbfiletemplate” (again no quotes)

Note: If you fire up Xcode now and select File > New File you should see a category called MacResearch with a file type called “MacResearch and C Rock”. If Xcode is already started, you’ll need to restart it first.

Now we want to create the files that we are going to use as our templates. There are a few ways to do this. You can use Xcode to create a file and then rename it with the appropriate name and extension, or you can use a text editor and add then save the file there. We’ll take the path of least resistance and simply copy the files from another template directory.

3) Copy the .h and .c files from the template folder BSD > C File.pbfiletemplate into your “MacResearch and C Rock.pbfiletemplate” folder. Rename them to main.c and main.h.

Ok. Now that we have the files in place we need to let Xcode know what to do with them. We’ll create an XML based plist file and populate it with the right information.

4) Fire up your favorite text editor and create a new plain text document with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Description</key>
	<string>Have a MacResearch day!!!!</string>
	<key>MainTemplateFile</key>
	<string>main.c</string>
	<key>CounterpartTemplateFile</key>
	<string>main.h</string>
</dict>
</plist>

The important information above are the key and string values in dictionary. Save this file in the same directory as your main.c and main.h files and name it TemplateInfo.plist

5) If Xcode is open, close and start it back up. Choose File > New File and you should see something like this:

and this:

producing files that look like this:

It’s pretty simple. If you open up the template files for the .h and .c files, you’ll see some of the syntax that is used to automatically populate the files with some of the information you see in your newly created files.


Leave a Reply

Your email address will not be published. Required fields are marked *