How to capture the icon preview of a Finder item?

So, I know that I can select an item in the Finder, “Copy”, and “Paste” it into Preview to get the finder icon. But this only gives me the standard icon, the one that tells you (for example) that it’s a Word doc or a PDF. What I want is a way to copy the icon preview, the one that the Finder shows you when you have “Show icon preview” checked in the Finder View Options.

An Applescript-based method would be ideal, but I’ll take anything.

When I copy a file in the Finder, it gives me the icon preview when I paste it into Preview. At least it does so for a picture file. What file type are you trying to get a preview from?

Word or Pages documents, Excel or Numbers spreadsheets, Powerpoint or Keynote presentations…

Since you said you’d take anything, I’ll pass this along. Look at Matt Gemmell’s website at http://mattgemmell.com/source. There’s obj-c source code for “NSImage+QuickLook”. Below is what his readme file says. It’s not something you can directly call from applescript, but you could build a command line tool to access it, and then call that tool from your applescript.

Wow. Well, I did say “anything”.

I’m afraid that’s beyond my abilities – I’m just a simple Applescript-user, obj-c is totally beyond me.

So I guess there’s no easy way to copy the icon preview to the clipboard, huh? Seems like it should be available from the Get Info window or something.

If you check out the Finder’s dictionary, there is an item that refers to a file’s icon, but it’s tagged as “not available yet.” I assume this means that it is planned, but when that might happen is anybody’s guess.

I had some time tonight so I put it together for you. I’ll get some use out of it too. I created a command line tool called qlpreview which you can run from applescript. You can download the command line tool from here. So download it and put it on your computer. Then here’s how you would run it from applescript. I hope that helps!

-- 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!
try
	do shell script cmd
on error theError number errorNumber
	if errorNumber is not -128 then
		tell me
			activate
			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

That… that is a thing of beauty.
Thank you so much. I am going to make awesome use of this.

No Problem! Glad you like it. :cool:

Just a small note:

Quicklook has a command line Server debug and management tool. qlmanage

You can use this to pump out a .png file of the Quicklook Thumbnail. It talks about scale factor and size. But I have not yet got that to work. **EDIT just worked out the scale. factors .

You can use floats or integers.
I found that you can use 8 bit multiples for thumbsizes.
So a factor of 0.5 is 64 × 48 pixels
Divide 0.5 by 2 you get 0.25 and 32 × 24 pixels.

I tested with a image of original size 2592 × 1944 pixels.

0.0625 8 × 6 pixels
0.125 16 × 12 pixels
0.25 32 × 24 pixels
0.5 64 × 48 pixels
1 : 128 × 96 pixels
2: 256 × 192 pixels
4 :512 × 384 pixels
***end edit

To get the Quicklook thumbnail.

set file_Path to POSIX path of (choose file)
set save_path to POSIX path of (choose folder)
do shell script ("/usr/bin/qlmanage -tf 2  " & quoted form of file_Path & space & " -o " & quoted form of save_path)

-t displays the Quick Look generated thumbnails (if
available) for the specified files.
-f factor Scale factor for the thumbnail

-o output result in dir (don’t display thumbnails or previews)

Well this is a much quicker way …

Say, where do the thumbnails go if they can"t be created or saved ?

I’m thinking I could actually use this to dispaly item icons in my app, and save the thumbs to a cache folder I create somewhere…

if qlmanage can not create a thumbnail of any kind then it will tell you with a [ERROR] or [WARNING] message in the stdout.

This also shows up in the Results in Applescript.
[i][WARNING] Plug-in at /Users/username/Library/QuickLook/QuickLook Script.qlgenerator/ is 32bit

  • No thumbnail created for /Documents/Scripts/add file addressbook TEST.scpt
    Done producing thumbnails"[/i]

The problem above is on my intel 10.6 Powerbook, the plugin that makes the quicklook thumbnails is 32bit, and the command line tool seems to not like that Although the normal quicklook view works.

So you should include a trap to capture the result and check if there was a problem.

I made a simple Service Action, with this.

And can choose from a size list.

I thought it would help if I passed on a simple method of only displaying the needed text in the choose list and getting the right Factor size without going through repeat loops etc…


set sizeList to {"32 × 24 pixels", "64 × 48 pixels", "128 × 96 pixels", "256 × 192 pixels", "512 × 384 pixels"}

(* dividing the first word i.e 256 divided by 128 gives the scale factor of 2.0 needed in the do shell  to set the thumbsize to "256 × 192 pixels" *)
	set the chosenSize to (word 1 of item 1 of (choose from list SizeList with prompt "Choose Thumbnail Size" ) as number) / 128 as real