Script: Create colorized icns file (e.g. for usage in toolbar scripts)

This script

  • prompts for a png or icns file,
  • displays a color picker,
  • creates a colorized icns file.

This file can then be dragged to any file or folder’s Finder info window to use it as icon.

Especially wrote it to match the color of toolbar script icons with those built into DEVONthink (no overkill here as I resued large parts from a previous script (and it’s worth the effort)). Of course you can choose any color you want and do whatever you like with the resulting icon, it’s not bound to DEVONthink. I’ve set the color picker’s default color to the one that I think fits best.

The script needs ImageMagick, you can get it via Homebrew.

If you’ve installed ImageMagick already or plan to do so you might be interested in another script which lets you colorize DEVONthink groups, smart groups, records etc. on the fly.

-- Colorize *.png or *.icns files for usage in Toolbar Scripts

property openDEVONthinkToolbarFolder : false
property theIconFolderPath : "" -- e.g. "/Users/username/Pictures/Toolbar Script Icons/" -- if empty a folder is created on desktop
property theTempPath : POSIX path of (path to temporary items)

try
	tell application "SystemUIServer" -- würde Finder choose file ausführen könnten keine Dateien auf choose from list gezogen werden
		activate
		set chooseFile to choose file of type {"png", "icns"} with multiple selections allowed without invisibles
		set chooseColor to choose color default color {51657, 51657, 51657} -- Farbe der DEVONthink Toolbar Icons
		set R to round ((item 1 of chooseColor) / 257) rounding to nearest
		set G to round ((item 2 of chooseColor) / 257) rounding to nearest
		set B to round ((item 3 of chooseColor) / 257) rounding to nearest
		set theColor to "(" & R & "," & G & "," & B & ")"
	end tell
	
	set colorizedIconFiles to {}
	
	repeat with thisFile in chooseFile
		tell application "Finder"
			set theFile to file thisFile
			set theName to name of theFile
			set theExtension to name extension of theFile
			set theFile_Path to POSIX path of (theFile as text)
			if theExtension = "png" then
				set theFile_Name to characters 1 thru -5 in theName as string
				set inputPNG_Path to theFile_Path
			else
				set theFile_Name to characters 1 thru -6 in theName as string
				do shell script "sips -s format png " & quoted form of theFile_Path & " --out " & quoted form of (theTempPath & theFile_Name & ".png" as string)
				set inputPNG_Path to (theTempPath & "/" & theFile_Name & ".png" as string)
			end if
		end tell
		
		set colorizedPNG_Name to (theFile_Name & "_" & R & "." & G & "." & B & ".png") as string
		set colorizedPNG_Path to ((theTempPath & colorizedPNG_Name) as string)
		do shell script "/usr/local/bin/magick convert " & quoted form of inputPNG_Path & " -fill 'rgb" & theColor & "' -colorize 100 " & quoted form of colorizedPNG_Path
		
		set iconSetName to (theFile_Name & "_" & R & "." & G & "." & B & ".iconset") as string
		
		set theCommands to {"mkdir " & quoted form of iconSetName, "sips -z 16 16 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_16x16.png") as string, "sips -z 32 32 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_16x16@2x.png") as string, "sips -z 32 32 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_32x32.png") as string, "sips -z 64 64 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_32x32@2x.png") as string, "sips -z 128 128 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_128x128.png") as string, "sips -z 256 256 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_128x128@2x.png") as string, "sips -z 256 256 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_256x256.png") as string, "sips -z 512 512 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_256x256@2x.png") as string, "sips -z 512 512 " & quoted form of colorizedPNG_Name & " --out " & quoted form of (iconSetName & "/icon_512x512.png") as string, "cp " & quoted form of colorizedPNG_Name & " " & quoted form of (iconSetName & "/icon_512x512@2x.png") as string, "iconutil -c icns " & quoted form of iconSetName, "rm -R " & quoted form of iconSetName} -- https://stackoverflow.com/a/20703594/13213957
		
		repeat with thisCommand in theCommands
			set theCommand to "cd " & quoted form of theTempPath & " && " & thisCommand
			do shell script theCommand
		end repeat
		
		tell application "Finder"
			if theExtension = "icns" then move (POSIX file inputPNG_Path as alias) to trash
			move (POSIX file colorizedPNG_Path as alias) to trash
			if theIconFolderPath = "" then
				set theIconFolderPath to (POSIX path of (path to desktop) & "Colorized Icons/") as string
				try
					exists (POSIX file theIconFolderPath) as alias
				on error
					make new folder in desktop with properties {name:"Colorized Icons"}
				end try
			end if
			move (POSIX file ((theTempPath & "/" & theFile_Name & "_" & R & "." & G & "." & B & ".icns") as string) as alias) to POSIX file theIconFolderPath as alias
			if openDEVONthinkToolbarFolder = true then open POSIX file ((POSIX path of (path to library folder from user domain) & "Application Scripts/com.devon-technologies.think3/Toolbar/") as string) as alias
			set end of colorizedIconFiles to (POSIX file (theIconFolderPath & "/" & theFile_Name & "_" & R & "." & G & "." & B & ".icns") as string) as alias
		end tell
	end repeat
	
	tell application "Finder"
		reveal colorizedIconFiles
		activate
	end tell
	
on error error_message number error_number
	if the error_number is not -128 then display alert "Hoppla!" message error_message as warning
	return
end try

Update: Script now supports multiple files per run