Add a Favorites Section to Xcode File Templates

December 4th, 2005

Scenario

If you’re developing software with Xcode, you’re familiar with the typical process of adding a new source file to your project. You hit Cmd-N, and then either navigate or typeselect to the desired file type. Usually, if you’re a Cocoa programmer, this means getting past all the AppleScript, BSD, and Carbon templates you don’t give a rat’s ass about. Even if you use the disclosure triangle to “close” those sections, they’ll reopen the next time you open the dialog.

Wouldn’t it be great if all the file types *YOU* care about were at the top of the list, beckoning you to instantly select them and get on with your job, hobby, or ascetic pursuit of nirvana?

Here’s a trick I discovered that allows just that: place a custom category of templates at the top of the list, with symbolic links pointing to all the desired items in their proper home folders. The best news? Xcode chooses the name of the template for the dialog by the name of the file, so you also rename the link to accurately describe the template in question. For instance, there are templates called “C File” in both BSD and Carbon sections. By linking to the items but renaming the link, I end up with a clear and useful Favorites section that works best for my current workflow:

Note that because the templates in the “Design” section are not true templates, there is no way that I know of to include them in the Favorites section. These items must by dynamically injected by Xcode, because they are not represented by “.pbfiletemplate” files, and include a custom UI in the New File assistant dialog.

Step by Step…

Just so it’s exceedingly clear how this is done, I’ll describe the process as a series of simple steps:

  1. Create a Custom Favorites Folder
    To get your folder to show up above all the other categories, you’ll want to name it so that it sorts alphanumerically early. A trick I’ve used is to insert a space before the word, to make sure it sorts early. The following example shows how you might create the required folder from the Terminal:
    % cd ~
    % mkdir -p "Library/Application Support/Apple/Developer Tools/File Templates/ Favorites"
  2. Find Existing Templates to Link To
    Unless you have existing custom templates in your home directory, you’ll find that all the templates worth linking to are located in Xcode’s File Templates folder. Navigate in the Finder to this directory (Cmd-Shift-G): “/Library/Application Support/Apple/Developer Tools/File Templates/”. You should be looking at a a list of folders in the Finder, each containing a number template items. Items ending in “.pbfiletemplate” are templates folders. These folders are what you want to link directly to.

  3. Add Links to Favorites
    The easiest way to add links to the Favorite folder is to open up a Terminal window, change to the Favorites directory, and use Drag-and-Drop to easily drop in the full paths of templates you want to include. Type “ln -s “, then drag in the pbfiletemplate folder you wish to add, hitting backspace a few times to remove the trailing slash. After your link has been created, you can rename the link to avoid naming collisions with other templates, or to increase the clarity as you see fit. For example, here are the commands I used to add the “BSD C File” template to my list of favorites:

    % cd ~/Library/Application\ Support/Apple/Developer\ Tools/File\ Templates/\ Favorites
    % ln -s /Library/Application\ Support/Apple/Developer\ Tools/File\ Templates/BSD/C\ File.pbfiletemplate
    % mv C\ File.pbfiletemplate BSD\ C\ File.pbfiletemplate
    

    Note: Unfortunately, Mac OS Aliases do not work. You must use a link. I recommend symbolic links because a hard link would not allow you to rename the link as described above (without also renaming the original). (Update: 12/20/2005: Kyle Dean points out in the comments that this is not true. See the comments for discussion.End update.)

Summary

Adding a list of favorites to the Xcode file templates will only save you a few seconds here and there, but the clarity of focus and satisfaction that your tools are working for you and not against you can improve your productivity and increase your desire to work. Enjoy!

6 Responses to “Add a Favorites Section to Xcode File Templates”

  1. Corey Peterson Says:

    Wow – this is a great tip!
    Since I spend 99% of my time with Cocoa classes, I just made a link from the /Library version of the Cocoa folder to one in the same folder with a space before it (to put it at the top, like what you did for the Favorites one).

  2. Daniel Jalkut Says:

    Cool! Glad you got some use from the tip, and thanks for sharing that idea. You might still consider putting the link (with the space in it) in your home directory’s templates folder, so you can count on it still being there the next time you update your system (or log into somebody else’s computer, assuming you’re on Netinfo).

  3. Cynical Peak » Blog Archive » Favorite File Templates Says:

    […] Daniel over at Red Sweater gives a nice little tip for adding a favorites section at the top of Xcode’s new file window. Since 95% of the time I’m using 1 of 3 templates it should save a couple seconds here and there. Plus hunting through the list is a pain sometimes. […]

  4. Kyle Dean Says:

    In your article you recommend using a symbolic link because “a hard link would not allow you to rename the link … without also renaming the original”

    You don’t have to worry about this. A hard link is a completely separate name for the file, which points to the underlying inode. You can rename a hard link (or delete it) without harming the file under the original name. If you delete (or move) the original file name, the file will still exist under the new name. That is something symbolic links can’t do.

    The second column in an ‘ls -l’ gives the number of hard links to a file.

    Some drawbacks to hard links are: hard links cannot span filesystems, and a user normally can’t hardlink directories (you can mess up the filesystem if you hardlink directories)

    After all that, a symbolic link is usually better. A symbolic link looks different and you can see a reference to the “original”

    You can do the link and rename in one step, by giving a second argument to ln.
    ln -s /Library/Application\ Support/Apple/Developer\ Tools/File\ Templates/BSD/C\ File.pbfiletemplate BSD\ C\ File.pbfiletemplate

    (I guess you did it in two steps because the line was getting pretty long by that stage)

  5. Daniel Jalkut Says:

    Thanks Kyle – you’re right about the ability to have separate names. I was jumping to conclusions on that. However, in this case the file being linked to is actually a directory (it’s a “bundle”) , so I guess the symbolic link is still appropriate.

    Another benefit of using symbolic links is that when Apple updates the original, the link will not keep a stale reference to the original.

  6. Nothing to Say » Favorite File Templates Says:

    […] Daniel over at Red Sweater gives a nice little tip for adding a favorites section at the top of Xcode’s new file window.  Since 95% of the time I’m using 1 of 3 templates it should save a couple seconds here and there.  Plus hunting through the list is a pain sometimes. 9:18 pm […]

Comments are Closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.