I’ve made this script and saved it as an application and the saved the folder.icns to the contents of the application.
set folderIcon to (((path to me) as text) & "Contents:Resources:Icons:folder.icns") as alias
tell application "Finder"
make folder at (path to desktop) with properties {name:"hello", icon:folderIcon}
end tell
but it doesn’t add the icon to folder it just created, I’m not sure I am approaching it in the right way can anyone help.
Currently, I have this working, but I wondered if I can bypass this handler by having them as .icns as opposed to .png
Below does work on Snow Leopard, but not on OSX 10.8 Mountain Lion, even after xcode/developer tools are installed as there is no Develop folder.
Ideally I would like it to work on 10.6 to 10.8 (if possible)
set folderIcon to (((path to me) as text) & "Contents:Resources:Icons:folder.icns") as alias
tell application "Finder"
make folder at (path to desktop) with properties {name:"hello"}
my (setCustomIcon from folderIcon to the result)
end tell
to setCustomIcon from imageFile to destination
(*
Set the icon of a destination file item to the image contained in imageFile
parameters - imageFile [various]: an imagefile for the icon (Finder alias or POSIX text)
destination [various]: a destination item to set to the icon (Finder alias or POSIX text)
returns [boolean]: true if icon is set, false otherwise
*)
set imageFile to imageFile as text
if imageFile starts with "/" then -- check for POSIX file
set imageFile to imageFile as POSIX file as alias
else
set imageFile to imageFile as alias
end if
set destination to destination as text
if destination does not start with "/" then -- check for POSIX file
set destination to POSIX path of destination
end if
try
tell application "Finder" -- copy the icon image to a temporary file
(duplicate imageFile to (path to temporary items) with replacing) as alias
set tempImage to quoted form of POSIX path of the result
end tell
set tempResource to quoted form of (POSIX path of (((path to temporary items) as text) & "TempResource"))
do shell script "/usr/bin/sips -i " & tempImage -- add a Finder icon to the image
do shell script "/Developer/Tools/DeRez -only icns " & tempImage & " > " & tempResource -- get the icon resource
do shell script "/usr/bin/file " & quoted form of destination -- determine the destination file type
if the result contains "directory" then -- folder
set theTarget to quoted form of (destination & "/Icon" & return) -- create Icon\r file
set Command to "rm " & theTarget & "; " -- remove any existing custom icon
set Command to Command & "/Developer/Tools/Rez -a " & tempResource & " -o " & theTarget & "; " -- add resource file to a folder
set Command to Command & "/Developer/Tools/SetFile -a V " & theTarget & "; " -- make it invisible
else -- file
set Command to "/Developer/Tools/Rez -a " & tempResource & " -o " & quoted form of destination & "; " -- add resource to a file
end if
set Command to Command & "/Developer/Tools/SetFile -a C " & quoted form of destination -- set custom icon attribute
do shell script Command -- do it
on error errmess number errnum -- oops
log errmess
-- display alert "Error " & errnum message errmess buttons {"OK"}
return false
end try
try -- remove temporary files
do shell script "rm " & tempImage & space & tempResource
end try
tell application "Finder" to update (destination as POSIX file)
return true
end setCustomIcon