Hack 6 Fiddling with Type/Creator Codes and File Extensions


Mac OS X uses a combination of type and creator codes and file extensions to determine the application with which a file is associated.

Every file in OS X and earlier versions of the Mac OS usually have both a type and creator attribute that help determine which application should open them. For example, a .html file may have a type and creator that say it should be opened in Microsoft's Internet Explorer, while a .mov file would have different attributes that suggest it should be opened by QuickTime. Files also have other attributes, like stationary, locked or unlocked, and timely information like creation and modification dates.

Unlike Windows, however, a Mac file doesn't need an extension to determine association with an application. A file named webcam could be a JPEG image, a text file to be opened by BBEdit, or even an HTML file associated with Internet Explorer. The type and creator codes rise above petty naming distinctions.

Normally, you'd need special software to set or change these types and creators, making the task more difficult (or expensive) than you'd hope. Longtime users of the Mac OS would often use the venerable ResEdit to perform the dirty deed, perhaps also tweaking other application strings in a fit of mirth and because they're there.

Thankfully, if you've installed Apple's Developer Tools, you can do this easily with the Terminal [Hack #48].

The easiest way to change a file's type and creator codes is to find a file you want to mimic and use its type and creator codes as a guide for your changes. For example, if you had a copy of Apple's home page (http://www.apple.com) saved as an HTML file named Apple Home.html, you'd enter the following into a Terminal [Hack #48] window to find its type and creator:

% /Developer/Tools/GetFileInfo "Apple Home.html"

The output would look something like this:

file: "Apple Home.html"
type: "TEXT"
creator: "MSIE"
attributes: avbstclinmed
created: 08/08/2002 19:12:46
modified: 08/08/2002 19:12:46

With this, we know that a file associated with Internet Explorer needs to have a type of TEXT and a creator of MSIE. If you check other files, you'll notice that types and creators are always four letters in length, often creating ungodly combinations of whimsy, like DBSE and FTCH, which have nothing to do with what you might think they do (an Extensis Portfolio database).

To associate some other file with Internet Explorer, you'd enter the following:

% /Developer/Tools/SetFile -c MSIE -t TEXT some_other_file

Because Mac OS X has a Unix underlayer, some files utilized by the Unix side of things may have no type and creator. GetFileInfo, in those cases, would show blank values for those fields:

% /Developer/Tools/GetFileInfo /usr/bin/vi
file: "/usr/bin/vi"
type: ""
creator: ""
attributes: avbstclinmed
created: 11/11/2002 18:28:56
modified: 11/11/2002 18:28:56

What's interesting is how a file with no associated type or creator codes might still be associated and opened with a particular application. Take, for example, an archived copy of the source code for Perl 5.8, freshly downloaded off the Net:

% /Developer/Tools/GetFileInfo perl-5.8.0.tar.gz
file: "perl-5.8.0.tar.gz"
type: ""
creator: ""

With no file type and creator, you may be confounded by the fact that tar.gz is nonetheless associated with Stuffit Expander when you look at it from the Finder's point of view, as shown in Figure 1-15.

Figure 1-15. tar.gz is associated with Stuffit Expander

The reasoning is simple: if a file has type and creator codes, they're used. Otherwise, Mac OS X takes a look at the file extension ? much like Windows does ? with an eye to figuring out what it belongs with. Rename perl-5.8.0.tar.gz to just perl-5.8.0 and Mac OS X will be lost; the Stuffit Expander icon changes immediately to a blank sheet.

Armed with an understanding of setting types and creators, you can easily fix this. A gzipped file has a type of Gzip and a creator of SITx; alter the codes on the command line and all's as expected:

% /Developer/Tools/SetFile -c SITx -t Gzip perl-5.8.0

With type and creator set, you can rename that file to your heart's content, appending whatever extension you like, and it'll remain associated with Stuffit Expander.

6.1 See Also

  • For details on GetFileInfo and SetFile, at the Terminal [Hack #48], type man GetFileInfo and man SetFile, respectively.