Latest in Hacks

Image credit:

TUAW Tip: Add file extensions to Quick Look


TUAW reader David wrote us to ask how he could view .erb files (Rails development) in Quick Look. The fact is, there are a lot of plain-text files with extensions that Quick Look doesn't recognize. It's relatively straightforward to tell Quick Look to treat these files like any other text file and preview them as plain text; it does require diving into plist files and possibly breaking an application, so don't dive in unless you're comfortable and fully backed up. Read on for a short tour of Quick Look hacking basics ...

Reminder: back up the affected applications before editing any part of them!

The easiest way I've found to do this is to add a definition for the filetype extension to TextEdit, so QuickLook will recognize the files as plain text. This requires editing the Info.plist file for TextEdit.

Go to your Applications folder in Finder and locate the file, then right-click it and choose "Show Package Contents." Inside of the Contents folder, there's a file called Info.plist. Depending on what you have installed in your system, this file may default to opening in Property List Editor, but I find this little hack easier in plain text -- edit the file in TextMate, BBEdit or your backup copy of TextEdit itself.

Do a quick search for a key called UTExportedTypeDeclarations (just search for "Exported"). If it's already there, you can continue adding to the array which begins directly after the ExportedType... key. If you come up empty-handed, you'll need to add it yourself by copying in the block below. If you're inserting it yourself and you're not sure where to stick it, look for the CFBundleExecutable key and place the code below right before it.

If you're extending an existing array, leave off the key and array wrapper (first two lines and last line). For testing purposes, I used a made-up filetype (Aardvark file) with file extensions .ardv or .ards -- the equivalent of the .erb extension David asked about. You can quickly pick through the code and replace all of the aardvark nonsense (in italics) with details appropriate to the filetype you're adding.

The most vital parts of the code (to be left intact) are the bits which tell the system how to handle the file (UTTypeConformsTo and and the definitions for the extensions you want it to recognize (public.filename-extension). You can add or remove lines from the public.filename-extension section as needed to handle alternative forms of the extension.

<string>Aardvark file</string>

Once you've saved the Info.plist file and closed your text editor, the easiest way to make the changes take effect is to move the app you just edited (TextEdit) out of the Applications folder and then move it back (you can also use 'touch /Applications/' from Terminal). Launch it again. Now, find a file with the previously unrecognized extension in Finder and hit the ol' Spacebar. Hopefully, you're now looking at a text preview of your file in Quick Look.

If you have a specific application you use for the filetype in question, you'll probably want to edit that file's Info.plist rather than TextEdit's, just to avoid any file-association hiccups.

Viewing with code formatting/coloring is a little more complex ... if you're at a loss, I'd email your favorite code-viewing Quick Look plugin developer to see if they'll add the filetype.

This method should be able to be expanded to non-text files, such as movies which QuickTime can play but Quick Look doesn't recognize by default. I haven't actually had this problem, so I haven't explored this.

Credits go to a macosxhint which got me started on this a while back.


In response to Luigi193 in the comments, you can tell qlmanage (command line Quick Look utility) to read your file as a given filetype. Incorporating this into an AppleScript would let you grab the Finder selection and Quick Look it manually. Add it to your user scripts folder and have it available in your Script Menu or assign a hotkey with something like FastScripts:

tell application "Finder"
set _files to selection
repeat with _file in _files
do shell script "qlmanage -p -c public.plain-text " & POSIX path of (_file as alias) & " 2> /dev/null &"
end repeat
end tell

From around the web

ear iconeye icontext filevr