Customizing XCode File Templates

Sunday, September 18, 2005

Want to customize/organize your XCode 2.1 templates??

Here's a few things I do to help myself.

First off let's get an understanding how this stuff works.

The location of file templates used for XCode are stored in /Library/Application Support/Apple/Developer Tools/File Templates/

You'll find several folders in the file templates folder. Notice that the names of the folders coincide with the names of the headings in the sheet that appears when you add a new file to your project.

Also, within each of those folders are folders with names that end in .pbfiletemplate. (The 'pb' is a left over from the previously used Project Builder application.) Within each .pbfiletemplate folder there is a class.h, class.m template file, and a TemplateInfo.plist. Just as an example open the /Library/Application Support/Apple/Developer Tools/File Templates/Cocoa/Objective-C class.pbfiletemplate folder. In that folder you'll find a class.m, class.h, and a TemplateInfo.plist file.

Open the TemplateInfo.plist file, which should automatically open in the Property List Editor application. In the Root node you'll find the Description of the template you see when you select this file template in the New File dialog. You'll also see the MainTemplateFile and its CounterpartTemplateFile which are set to class.m, and class.h respectively. Close the file without saving any changes you may have made.

Now open the class.m file. You'll see the template of the standard .m file. Within this file you'll see several elements using a particular pattern like the one for the file name ¬'FILENAME¬". These elements are replaced when you create an instance of the file in your project. Most of these elements are one's you probably don't want to mess with. However, one such element is listed as ¬'ORGANIZATIONNAME¬" which normally defaults to "__MyCompanyName__". This particular listing can be set up within the defaults of your XCode application. I'll explain that later, though.

So let's set up you own custom listing for files...

First Quit XCode and the Property List Editor applications, without saving any changes.

Now go to the /Library/Application Support/Apple/Developer Tools/File Templates/ in the Finder and create a new sub folder named "- My Classes". Now go to the Cocoa folder and copy the 'Objective-C class.pbfiletemplate' to the "- My Classes" folder. Now open XCode and create a new Cocoa Application project. Now add a new file to the project. Notice you'll see a heading of "- My Classes" with an "Objective-C class" listing. Just cancel for now, as we'll go ahead an make some further changes.

Rename the 'Objective-C class.pbfiletemplate' to 'NSObject subclass.pbfiletemplate'. Then open the TemplateInfo.plist with that folder.

Edit the Description listing under the Root element to say 'An Objective-C class file which subclasses NSObject, with an optional header which includes the header.'

And finally, edit the .m file to use /* and */ to hold the comments rather than the normal // for each line.

Now close and save both the .h file and the TemplateInfo.plist file.

Now attempt to add a new file to your XCode project. You'll see the "- My Classes" heading with the "NSObject subclass" listing. Select the listing and you'll see the new description show up. Now click next, give the file and name and click finish. Notice the new commenting style on the .m file's comments.

The quick and easy way to take care of that "__MyCompanyName__" information is to use the Terminal.

Enter the following line in the Terminal, replacing the Company Name with whatever you prefer.
defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "Company Name";}'

Comments:
Better yet, specify both the Organization name *and* the Project name, as follows:

'{"ORGANIZATIONNAME" = "Intuit, Inc."; "PROJECTNAME" = "QuickBooks";}'
 
Thanks, very useful! I also like the fact that I can (finally) edit the code templates to use a sensible style instead of Cocoa's perverse letsrunallthewordstogethersoitsmuchlessreadable style.
 
Thanks a lot for this tip, I looked for it a long time before! Don't know why it's not in XCode preferences or project's info.
 
Post a Comment


<< Home

This page is powered by Blogger. Isn't yours?