Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Another way to view any text file in Quick Look System 10.6
Snow Leopard only hintIn Leopard, for Quick Look to display the contents of a text file, its extension had to be 'registered' with its UTI in the Info.plist file of the application associated with the file extension (see this hint). For most file endings associated with applications, this already works out of the box (in short, for all files with a Finder icon). This still seems to apply to Snow Leopard, but there is another option which might be new to Snow Leopard.

To make the file Quick Look-able, one simply has to set an extended attribute identifying the file as a text file. The necessary attribute is com.apple.FinderInfo with the value TEXT!Rch. Setting extended attributes can be done via with the xattr command in the Terminal. Note that xattr is technically a script and not a shell command, and thus does not have a man page, but help is available with xattr -h.

In my testing, I was only able to set the value in hex format. For a file named foo.goat this looks like this:
xattr -wx com.apple.FinderInfo '54 45 58 54 21 52 63 68 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' foo.goat
Interestingly, TextWrangler already sets this attribute with the 'correct' value, SubEthaEdit and TextEdit do not.
    •    
  • Currently 1.93 / 5
  You rated: 5 / 5 (14 votes cast)
 
[13,306 views]  

10.6: Another way to view any text file in Quick Look | 10 comments | Create New Account
Click here to return to the '10.6: Another way to view any text file in Quick Look' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Another way to view any text file in Quick Look
Authored by: dillfest on Jan 14, '10 10:49:11AM

Could I use this for a whole extension? I have a series of text files with a custom extension, that don't get scanned for Spotlight or registered with Quicklook because of that, but there are thousands of them. The extension is .bww (it's actually for a Windows program I run under Parallels, but it would be really nice if OSX could play with them better.)



[ Reply to This | # ]
The hint I linked to should do this
Authored by: hamarkus on Jan 14, '10 01:08:10PM

The hint I linked in my hint ('this hint') to should do this.



[ Reply to This | # ]
These are the Type and Creator codes
Authored by: radiola on Jan 14, '10 10:57:16AM

What you're doing with xattr is setting the file's 4-character Type and Creator codes, in kind of a backwards way. The first four characters (TEXT) are the file type, the second four (!Rch) are the creator.

TEXT is the standard type code for a text file. In fact, on my Leopard system, all you need to do is set is the type, not the creator. I don't know if that can be done safely with xattr — I recommend SetFile from XCode Tools, or any other utility that is designed to manipulate type and creator info.

Interestingly, TextWrangler already sets this attribute with the 'correct' value, SubEthaEdit and TextEdit do not.

TextWranger sets it "correctly" because !Rch is TextWrangler's own creator code! Again, in my testing, only the TEXT type code must be set; the creator code doesn't matter, and can be missing.



[ Reply to This | # ]
These are the Type and Creator codes
Authored by: hamarkus on Jan 14, '10 01:19:39PM

Good to know. So the more general version of this hint would be to simply set the type code to TEXT, with the additional information that the type codeis stored as an extended attribute under com.apple.FinderInfo. Multiple tools exist for setting that that, one of that being xattr another setfile (not sure where both are available if Xcode is not installed).

In one sense, xattr is the more general method since the information is stored as an extended attribute. In another sense, setfile is more direct method since it calls this setting by its commonly known name (type).



[ Reply to This | # ]
10.6: Another way to view any text file in Quick Look
Authored by: asmeurer on Jan 14, '10 12:03:43PM
I got so tired of QuickLook showing my an icon instead of the text of a plain text file that doesn't end in .txt, that I wrote this AppleScript:

on run
	tell application "Finder"
		set theitems to the selection as alias
		showthefiles(theitems)
	end tell
end run
on open (opened)
	set theitems to opened as alias
	showthefiles(theitems)
end open
on showthefiles(theitems)
	set thescript to ("qlmanage -p -c public.plain-text " & the quoted form of the POSIX path of theitems)
	do shell script thescript
end showthefiles
Save it as a drop script and put it in the Finder toolbar. You can then drag a plaintext file onto it to open a QuickLook window that will show you the contents. The only down side is that you have to close it by hand. Also, this should work just by selecting the file and clicking on the script in the toolbar, but I get an error. Any AppleScript gurus out there know what I am doing wrong?

[ Reply to This | # ]
10.6: Another way to view any text file in Quick Look
Authored by: mr. applescript on Jan 14, '10 10:35:01PM
A couple things: 1) inside the Run handler: use "as alias list" to have the Finder return references to the currently selected items as a list of references in alias format 2) in the Open handler: you don't need to coerce the input to an alias list, they already are by default 3) in the sub-routine: you have to create a space-delimited string of posix references by iterating the list of alias references, like this:
on showthefiles(theitems)
	repeat with i from 1 to the count of theitems
		set this_path to the quoted form of the POSIX path of (item i of theitems)
		if i is 1 then
			set the POSIX_list to this_path
		else
			set the POSIX_list to the POSIX_list & space & this_path
		end if
	end repeat
	set thescript to ("qlmanage -p -c public.plain-text " & POSIX_list)
	do shell script thescript
end showthefiles


[ Reply to This | # ]
10.6: Another way to view any text file in Quick Look
Authored by: mr. applescript on Jan 14, '10 11:32:16PM
FYI, a QuickLook Droplet is already available and posted on MACOSXAUTOMATION.COM. http://macosxautomation.com/applescript/quickviewer/index.html

[ Reply to This | # ]
10.6: Another way to view any text file in Quick Look
Authored by: asmeurer on Jan 14, '10 01:46:37PM

If you are going to edit the file anyway, as this hint has you do, why don't you just change the extension to .txt?

For the most part, text files that I make already have this, so this is mostly for things that are from something else, when I usually don't want to edit the file, extension or creator code. In that case, I use the script that I posted below.



[ Reply to This | # ]
10.6: Another way to view any text file in Quick Look
Authored by: hamarkus on Jan 14, '10 03:26:04PM

I run a lot of numerical simulations. It is quite useful to give the files these simulations output meaningful extensions, eg, .log, .dat, .param or something even more specific. It is simply my way of already sorting these files into categories. I also use partly code written by others which had their own conventions.
Most of simulations are run on Linux workstations, with files created by Fortran, C++ or Perl. I essentially was just curious what possibilities there to make them quicklookable.

Edited on Jan 14, '10 03:29:24PM by hamarkus



[ Reply to This | # ]
10.6: Another way to view any text file in Quick Look
Authored by: Sesquipedalian on Jan 15, '10 12:29:04PM

Rather than xattr or SetFile, I would just use this one line AppleScript:

tell application "Finder" to set file type of ((POSIX file "/path/to/file") as alias) to "TEXT"

You could call it from the command line via osascript, or via pretty much any other way you can use AppleScript.



[ Reply to This | # ]