Get a file icon or QuickLook preview image for a file

I wrote a command line tool which can get either the file icon or the QuickLook preview image of a file. It’s 10.5 or later only. You can download it from here:

I wrote this in response to helping someone in these forums. You can see that thread here. The command line tool can be run from applescript. Here’s an example script…

-- Example usuage of qlpreview to get a QuickLook preview of a file
-- qlpreview will output an image file of the preview as seen in QuickLook
-- if the QuickLook preview cannot be found then the normal file icon is output instead

-- variables
-- Note: all variables need to be strings
set exePath to (path to desktop folder as text) & "qlpreview" --> path to the unix executable qlpreview
set imageType to "jpg" --> jpg, png, or tif
set width to "1000" --> the width in pixels of the preview image
set height to "1000" --> the height in pixels of the preview image
set asIcon to "yes" --> do you want a pretty icon format?
set outPath to (path to desktop folder as text) & "test" -- where you want the image saved
set preferFileIcon to "no" -- when yes the file icon is returned even if the preview image exists

-- get the file you want the preview image of
set inPath to choose file without invisibles

-- setup the shell command
set cmd to quoted form of POSIX path of exePath & space & "-imageType" & space & imageType & space & "-width" & space & width & space & "-height" & space & height & space & "-asIcon" & space & asIcon & space & "-inPath" & space & quoted form of POSIX path of inPath & space & "-outPath" & space & quoted form of POSIX path of outPath & space & "-preferFileIcon" & space & preferFileIcon

-- run it!
	do shell script cmd
on error theError number errorNumber
	if errorNumber is not -128 then
		tell me
			display dialog "There was an error:" & return & return & theError & return & return & "Error Number: " & errorNumber as text buttons {"OK"} default button 1 with icon stop
		end tell
	end if
end try

Hi Hank,

thanks for sharing the source code. :slight_smile:
I didn’t realize yet that you can parse the command line arguments with user defaults

Glad you found it useful Stefan. I learned this only last week while looking through some of Martin Michel’s code and thought that was cool too!

I keep getting an error :

What can I do about it ? I already CMD+I 'ed it and set permissions to writing/reading… still does not work

Any help ?

“/path/to/me/” is not a path. You need the correct path to the executable “qlpreview.”

To get the path:

set thePath to POSIX path of (choose file)

Of course Craig, I know that !

That was just an example … since the message says that qlpreview denies me permission, that means the script gets to it :wink:

I used the code as it is posted here and put qlpreview (compiled already) on my desktop. But I still get this error… :frowning:

OK whatever, I was using the pre-compiled version.
Downloaded the source, compiled it myself in Xcode and it seems to work now… all I need to figure out is how to use it to display icon previews next to files in a table view … if that’s possible at all.
It may not be possible, since it saves the files somewhere… :frowning:

Anyway I’ll try it out, let’s get down to work… :slight_smile:

Ok, If all you want to do is get an icon for a file or application then use this.

That’s good to know Craig, but I’m writing an old school ASS application right now, I’m not using Obj-C or anything.

Plus I just want to get the preview icon for the files dropped on a table view (and starting to doubt it’s even possible), like so :

Create an Objective-C class and use call method.

Wow… uhhhh… yeah … I got as far as adding an Objective-C class called “Controller” ! But I know nothing about Obj-C to be honest.

We’re abusing Hanks post so I will email you off line.