Assign preview image as file icon, using utilities by Hank McShane

For some software that I distribute on DMG disk images, I want the ReadMe file to appear on the DMG with its preview image as its file icon, and I sometimes want other files to display their QuickLook preview as their file icon.

Hank McShane at HamsoftEngineering.com provides two unix executables and some sample Applescript that make this task easy to perform, and I’ve combined them into a single Applescript application. Hank McShane told me it was all right with him if I posted it here. Except for very slight details, it consists entirely of his work.

Here’s how it works. Go to this page:

http://www.hamsoftengineering.com/codeSharing/codeSharing.html

and download SetFileIcon and qlpreview.

Save the following Applescript as an application:

-- QuickLookPreviewToFileIcon
-- uses two utility programs by HAMsoft Engineering (HAMSoftEngineering.com)
-- based on an AppleScript by Hank McShane of HAMsoft Engineering

set thisApp to path to me as text
set qlpreviewPath to thisApp & "Contents:Resources:Files:qlpreview"
set setfileiconPath to thisApp & "Contents:Resources:Files:SetFileIcon"

-- variables, Note: all variables need to be strings
set imageType to "png" --> jpg, png, or tif
set JPEGimageQuality to "90" --> only applicable when the "imageType" is jpg. Defines the amount of compression used. Must be a value between 0 and 100, 0-max compression and 100-no compression.
set maxWidth to "512" --> the maxWidth in pixels of the preview image
set maxHeight to "512" --> the maxHeight in pixels of the preview image
set asIcon to "yes" --> do you want a pretty icon format? -- changed to "yes" from "no" in the original HAMsoft script
set preferFileIcon to "no" -- when yes the file icon is returned even if the preview image exists
set generatePreviewOnly to "no" -- when yes then if a preview image can't be found then no image is generated

-- get the file you want the preview image of
set inPath to choose file with prompt "Choose the file to set its preview image as its icon:" without invisibles

set outPathName to inPath as text
set outPath to outPathName & "." & imageType
-- display dialog outPath
-- display dialog quoted form of POSIX path of outPath

-- setup the shell command
set cmd to quoted form of POSIX path of qlpreviewPath & space & "-imageType" & space & imageType & space & "-JPEGimageQuality" & space & JPEGimageQuality & space & "-maxWidth" & space & maxWidth & space & "-maxHeight" & space & maxHeight & 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 & space & "-generatePreviewOnly" & space & generatePreviewOnly

-- run it!
try
	do shell script cmd
on error theError number errorNumber
	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 try

-- set up command to set file icon to image file
set icnCmd to quoted form of POSIX path of setfileiconPath & space & "-image" & space & quoted form of POSIX path of outPath & space & "-file" & space & quoted form of POSIX path of inPath

-- set the file icon
try
	do shell script icnCmd
on error theError number errorNumber
	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 try

-- delete the image file?
activate
display dialog "Delete the image file?" buttons {"Yes", "No"} default button 1
if button returned of result is "Yes" then
	do shell script "rm" & space & quoted form of POSIX path of outPath
end if

-- show the result
set infileInfo to inPath as alias
tell application "Finder" to open information window of infileInfo

After saving this an application, right-click on it and show the contents of the application bundle. Create a folder named “Files” inside the “Contents/Resources” folder, and copy the two unix excutables SetFileIcon and qlpreview into the “Files” folder.

You can now run this application in order to assign the QuickLook preview of a file as its file icon. Obviously the script can be simplified, and it’s trivial to convert it into a droplet so that you can simply drop a file on to it in order to assign its QuickLook preview as its icon. (I simply haven’t taken the five minutes required to do that.)

I’m sure that the AppleScript can be improved in a dozen ways, but, meanwhile, I hope someone else will find this as useful as I do. Again, I want to emphasize that Hank McShane did 99.9 percent of the work on this; I merely did the 0.1 percent required to put it all together in one package.

Three (positive) things:
1- You’re way to polite because setting an image icon for a file is basically one method in Cocoa! QL is only a set of one methdo call depending on the parameters you give on the command line. That you can’t write Objective-C or don’t understand it doesn’t mean you don’t deserve credits. My point here is that Cocoa delivers us a one-liner to achieve the job, Apple is the one that did all the coding for us so in my opinion you’ve done half the job which is more than 0.1 percent!

2- Because of the great explanation I think this fits better in ‘code exchange’

3- :cool:

I agree about the politeness, but I think you’re overlooking qlpreview – not rocket science, perhaps, but certainly more than one method call, and nicely packaged.

I agree that this should be in Code Exchange, so I’m moving it there.