November 17th, 2011
This post contains the material on custom file templates I had prepared for the book. I decided not to put this material in the book because it wasn’t strong enough for me to support. But information on creating custom file templates in Xcode 4 isn’t easily available so I’m making it available here. I hope it helps you create your own file templates.
File Template Contents
At a minimum a file template has the following items:
File templates can also include additional files, such as xib files and icon files.
The .xctemplate extension defines the folder as a template folder. The name of the folder is the name that appears in the list of templates for the selected category in the New File Assistant. The name of the folder is the name of the template.
The extension for the ___FILEBASENAME___ file depends on the language. If you create a C, C++, or Objective-C template, there may be a second file for the header file.
The easiest way to create a custom file template is to copy one of Apple’s templates and modify it to suit your needs. Apple’s file templates also provide good examples that you can study to help you build your own templates. Apple’s iOS file templates are in the following location:
The rest of Apple’s file templates are in the following location:
Developer is where you installed Xcode. For Xcode 4.3 and later you can find Apple’s file templates in the Xcode application bundle. Select the Xcode application in the Finder, right-click, and choose Show Package Contents to access the files in the application bundle.
After creating a copy of one of Apple’s templates, you must modify the files to customize them. Open the ___FILEBASENAME___ file and the TemplateInfo.plist file in Xcode to make your changes. I’m going to cover the keys in the TemplateInfo.plist file later in this post.
For Xcode to find your file template you must move the template folder to the user templates folder, which is in the following location:
You may need to manually create some of the folders in the path to the user templates folder. GroupName is the name of the category on the left side of the New File Assistant. You can create your own group name or use one of the built-in names. Your file template will appear in the GroupName category.
The TemplateInfo.plist file has the following keys (this may not be an exhaustive list) for file templates:
Your template does not have to use all these keys. The data type for the keys is usually one of the following: Array, Boolean, Dictionary, or String.
The AllowedTypes key is an array that contains allowed file types. Source code file templates use this key. The following are examples of common allowed types:
Use the public.source-code type for languages other than the ones I listed.
Only languages that have header files use the CounterpartTemplateFile key. Supply the name of the header file.
The DefaultCompletionName key contains the initial name for the file. When you save the file for the first time, this name appears as the file name in the Save panel.
The Description key describes the file template. The contents of the Description key appear at the bottom of the New File Assistant when you select the file template.
Text files have the following Kind key value:
The value of MainTemplateFile should be the name of the file in the .xctemplate folder. It generally has the following value:
Where Extension depends on the type of file you’re creating. For C, C++, and Objective-C files, the main template file is the implementation file, not the header file. The extension for an Objective-C file is .m.
The MacOSXVersionMin key is the earliest version of Mac OS X that can use the template.
The Options key is an array that lets you add controls to the New File Assistant. You can see an example of a custom control if you add an Objective-C class to a project. When you add an Objective-C class, there is a combo box that lets you choose the subclass. The combo box is an example of a custom control.
Create a dictionary key for each control you want to add. Add keys to the dictionary key. The following are common keys for controls:
The Platforms key is an array of strings that identifies the platforms the template works on. A Mac file template has the following value:
An iOS file template has the following value:
I’m not exactly sure what the SortOrder key does. I haven’t seen a sort order value other than 1 in Apple’s templates.
The Summary key summarizes what the file template does. The summary is a shorter form of the file template’s description.
Tags: xcode 4