set icon to file

hi, I’m looking for a way to set a specific icon to a specific files produced regularly.

I thought of placing an Automator Application on toolbar allowing a quick drag to set this custom icon…
The script would refer to a .icns file stored locally.

Your expertise would be much appreciated!
Cheers,

Hi,

I wrote you a small AppleScript droplet named Ionic Elf, which sets a defined image as the icon of dropped files. You can chose the image by opening the script with a double click. To set the chosen image as the icon for a bunch of files, just drop them onto the script afterwards.

The script is based on a simple command line tool I wrote today, you can study its source code here.

To download the ready for use droplet, just click here, its only a small download (ca. 50 KB).

Please note that the script requires Mac OS X 10.5 or higher.

Maybe you can make good use of it.

Best regards from snow Berlin,

Martin


-- author: Martin Michel
-- eMail: martin@joyofscripting.com
-- created: 27.01.2010
-- version: 1.0

-- This script sets the icon of dropped files to the defined image.
-- The image can be chosen by opening the script with a double click.

property mytitle : "Ionic Elf"
property imgfilepath : missing value

-- I am called when the user opens the script with a double click
on run
	set imgfilepath to (choose file with prompt "Please select an image to be used as a file icon:" without invisibles, multiple selections allowed and showing package contents) as text
end run

-- I am called when the user drops Finder items onto the script's icon
on open finderitems
	try
		-- did the user already chose an image to be used as a file icon?
		if imgfilepath is missing value then
			run
		end if
		
		-- if so, does it still exist?
		if not my itempathexists(imgfilepath) then
			run
		end if
		
		-- searching for files in the dropped Finder items
		set foundfiles to {}
		repeat with finderitem in finderitems
			set finderiteminfo to (info for finderitem)
			if not folder of finderiteminfo then
				set foundfiles to foundfiles & finderitem
			end if
		end repeat
		
		-- no files found :(
		if foundfiles is {} then
			set errmsg to "Please drop some files (not folders) onto the script to change their icon."
			my dsperrmsg(errmsg, "--")
			return
		end if
		
		-- processing each found file with the command line tool
		set qtdtoolpath to quoted form of POSIX path of (((path to me) as text) & "Contents:Resources:ionicelf")
		repeat with foundfile in foundfiles
			set command to qtdtoolpath & " -img " & quoted form of POSIX path of imgfilepath & " -file " & quoted form of POSIX path of foundfile
			try
				do shell script command
			on error errmsg number errnum
				my dsperrmsg(errmsg, errnum)
			end try
		end repeat
	end try
end open

-- I am indicating if a given item path exists
on itempathexists(itempath)
	try
		set itemalias to itempath as alias
		return true
	on error
		return false
	end try
end itempathexists

-- I am displaying error messages to the user
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occurred:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle
	end tell
end dsperrmsg

Hi Martin

Works perfectly, thank you!

The file type must be “.ico” though, not “.icns” (if someone else wants to try your brilliant script).

This is gonna fix my .tasks files in TextMate.

Thanks again… from hot Western Australia :wink:

Hi Psacal,

Oh, how I envy you for living in hot Western Australia :slight_smile: It’s currently freezing cold here in Germany, with a lots of snow and ice everywhere. My wife already suggested a trip to Tenerife to escape the cold, but we have both too many work projects…And of course, my little daughter loves the weather, she likes to go sledding with daddy :lol:

Actually the script should process every image format supported by NSImage:

‘PDF’", PDF, pdf, “‘PICT’”, PIC, pic, PCT, pct, PICT, pict, “‘EPSF’”, PS, ps, EPSI, epsi, EPSF, epsf, EPI, epi, EPS, eps, “‘BMPf’”, “‘PNTG’”, “‘TPIC’”, “'BMP '”, “‘8BPS’”, “‘icns’”, “‘TIFF’”, “'jp2 '”, “‘GIFf’”, “‘JPEG’”, “‘PNGf’”, HDR, hdr, EXR, exr, MAC, mac, PNT, pnt, PNTG, pntg, TARGA, targa, TGA, tga, CUR, cur, XBM, xbm, BMP, bmp, ICO, ico, PSD, psd, SRF, srf, ORF, orf, MRW, mrw, RWL, rwl, RAW, raw, RW2, rw2, RAF, raf, CRW, crw, CR2, cr2, TIF, tif, ICNS, icns, EFX, efx, JFAX, jfax, JFX, jfx, G3, g3, FAX, fax, TIFF, tiff, DCR, dcr, ERF, erf, ARW, arw, SR2, sr2, PEF, pef, NRW, nrw, NEF, nef, 3FR, 3fr, FFF, fff, MOS, mos, DNG, dng, JPF, jpf, JP2, jp2, GIF, gif, JPE, jpe, JPEG, jpeg, JPG, jpg, PNG, png

:smiley:

Best regards,

Martin

Hey Martin,
It could be my ico files…

Never mind, your script is on my toolbar fixing these .todo files from the Task bundle for TextMate.
Used a lot for writing down brief and minutes associated with a project, I’m glad to finally see a proper icon.

And I won’t tell you how great the Indian Ocean was today :wink:

Thanks again for sharing!!

Hi Martin,

Looking at the source code was very helpful in understanding how to write a command line tool and use options.
i.e -img

Thanks for sharing.