How to test for custom icon?

This seems like a simple question, but I have been unable to find an answer. Please excuse me if it has been answered elsewhere.

How can I use AppleScript to test if a folder has a custom icon? At another website I saw a passing reference to a "uses custom icon" flag, but I can't get it to work, nor can I find it documented anywhere.

Thank you for any help.

iMac G3 400 MHz, Mac OS X (10.4.11)

Posted on Jan 23, 2009 10:08 PM

Reply
9 replies

Jan 24, 2009 3:47 PM in response to red_menace

Thank for your input red_menace, but I can't do what I want to yet. Could this be a feature implemented in Leopard but not Tiger?

Modifying your script slightly:

set TheFile to quoted form of POSIX path of (choose file)
do shell script "/usr/bin/mdls -name kMDItemFSHasCustomIcon " & TheFile
return result

yields:

"/Users/imacuser/Documents/ temp icon folder/tmp1.JPG -------------"

I don't know what if anything can be done with that result. In addition, I am looking for folders with a custom icon, not files. Changing "choose file" in the above to "choose folder" yields a similar result.

Since you pointed out what metadata I hope should be looking for, in Terminal I tried:

mdfind -onlyin "/Users/imacuser/Documents/ temp icon folder/" kMDItemFSHasCustomIcon == 1

It appears to find only files with custom icons not folders. At first mdfind seemed to find all the correct files, but now it only finds one. Do you think this could be part of Spotlight's general flakiness?

In the end, I want something with the logical structure:

if kMDItemFSHasCustomIcon of selectedFolder is 1
then
--copy icon to clipboard
--paste icon into a database thumbnail field
else
--skip
end if

Thanks for your help.

Jan 24, 2009 7:02 PM in response to Lucifer

It looks like that particular key is only in Leopard. In Tiger, mdls returns a path followed by several dashes if the particular metadata tag doesn't exist.

There also isn't a handy way in AppleScript (even in Leopard) to get the icon of an item - this would require using a scripting language that can access cocoa routines. A script that uses a foundation tool to access the standard system icons is available at macscripter.net - maybe you can modify that.

Jan 24, 2009 11:08 PM in response to Lucifer

Hello

You may try GetFileInfo(1) command in Developer Tools to check the attribute bit for custom icon. Something like CODE1.

--CODE1
set p to POSIX path of (choose folder)
do shell script ("/Developer/Tools/GetFileInfo -ac " & quoted form of p)
set hasCustomIcon to result as integer -- 0 or 1
return hasCustomIcon
--END OF CODE1

cf.
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/GetFileI nfo.1.html



Or you may check the presence of actual icon file in the target folder. Something like CODE2.

*Note the fact that there is "Icon\r" file does not necessarily mean the enclosing folder has custom icon (at least under OS9/Classic). So CODE2 should NOT be used if your system is configured for both OSX and OS9/Classic (unless additional code is introduced to check whether "Icon\r" file actually contains "icns" resource).

--CODE2 (for OSX-only system)
set d to choose folder
return hasCustomIcon(d) -- true or false

on hasCustomIcon(d)
(*
alias d : folder alias

* CAUTION: "Icon\r" file may exist even when it does not contain custom icon.
(Under OS9/Classic, this file may also contain custom column setting in list view,
which under OSX is stored in ".DS_Store" file.)
*)
set nn to {"Icon" & return, ".VolumeIcon.icns"} -- list of possible icon file names
set dp to d as Unicode text
if dp does not end with ":" then error "Given alias is not directory: " & dp number 9000
repeat with n in nn
try
(dp & n) as alias
return true
on error -- the node does not exist
end try
end repeat
return false
end hasCustomIcon
--END OF CODE2

Hope this may help,
H

Jan 24, 2009 11:16 PM in response to red_menace

Another approach occurs to me: As I understand it, if a folder has a custom icon, that icon is stored in an invisible file stored in the folder. Could AppleScript test for the existence of that file? I seem to recall that there is something tricky about the name of the invisible file.

I've already written and tested the code to get the custom icon; I just want to test for the custom icon's existence. My code opens the Get Info window and uses GUI scripting to copy the icon to the clipboard. Like most GUI scripting, it feels like a kludge -- but it works. If the folder does not have a custom icon, my code copies the generic icon. I would like to avoid pasting the generic icon into my database.

Jan 25, 2009 2:01 AM in response to Hiroto

Hiroto, I think you've answered my question.

CODE1 looks more elegant and versatile. As soon as I resolve the space issues on my startup volume, I'm going to install Developer Tools and test it. I've been intending to install them anyway, and this will give me the impetus to do it.

CODE2 works just as you described. It works correctly for newer folders, but not for folders that were created before the migration to OS X.

Thanks for your help. You seem to really know your stuff and put a lot of time into helping others. I'll let you know how CODE1 works.

Jan 25, 2009 7:21 PM in response to Hiroto

Hello Hiroto,

After putting it off for months, I finally installed Developer Tools. Your CODE1 now does exactly what I wanted. Thank you again.

Just as an exercise, I would be interested in the code required to check whether the "icon\r" file contains "icns" resource, if anyone has an explanation handy. I wouldn't, however, want anyone to invest a lot of time in figuring it out for me, because my real problem is solved.

Jan 25, 2009 10:35 PM in response to Lucifer

Hello Lucifer,

You're quite welcome!

As for the exercise, in order to look into resource fork we need some special tool, for AppleScript has no built-in function for that. One handy way is to use free scripting addition named 'Satimage osax' that is made and distributed by Satimage Software, France.

CODE2A below is a revised version of CODE2. It will check whether "Icon\r" file contains 'icns' resource by using 'list resources' command of Satimage osax, whose dowload links are given in script comment.



--CODE2A
set d to choose folder
return hasCustomIcon(d) -- true or false

on hasCustomIcon(d)
(*
alias d : folder alias

* using 'list resources' command of Satigmage.osax

Install Satimage osax in either of:
/Library/ScriptingAdditions/
~/Library/ScriptingAdditions/

Download links:
http://www.satimage.fr/software/en/downloads/downloadscompanionosaxen.html
http://www.satimage.fr/software/en/downloads/downloadsold_companionosaxen.html
http://www.satimage.fr/software/en/support/soft9.html
*)
set dp to d as Unicode text
if dp does not end with ":" then error "Given alias is not directory: " & dp number 9000
-- check "Icon\r" file
try
(dp & "Icon" & return) as alias
if (list resources "icns" from result) is not {} then return true
on error -- the node does not exist or has no resource fork
end try
-- check ".VolumeIcon.icns" file
try
(dp & ".VolumeIcon.icns") as alias
return true
on error -- the node does not exist
end try
return false
end hasCustomIcon
--END OF CODE2A



Kind regards,
Hiroto

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to test for custom icon?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.