Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

automatically setting folder icon

In one folder I have a lot of folders with pictures (without subfolders) which I'd like to organize like a catalogue.

What I want to do is:
For each folder
• Take the picture inside the folder named "cover.jpg" and set it as the Icon for the folder
• additionally change the view options of each Finder window to minimum grid size and maximum thumbnail size, arrange by name.

I tried a bit with Automator but unfortunately I couldn't get to an automation from the recorded steps.

Thanks for any help.

Mac OS X (10.5.5)

Posted on Jan 26, 2009 4:17 PM

Reply
16 replies

Jan 26, 2009 5:56 PM in response to Michael[0-9]-__

I am guessing that you are wanting to use a badge over a more-or-less normal folder icon - as mentioned in your other thread there isn't a handy way to change the folder icon like that. If you are wanting to just set an icon (without being able to tell if it is a folder), there is the seticon utility in the osxutils utilities - is that what you are wanting to do?

Jan 26, 2009 8:57 PM in response to Michael[0-9]-__

Hello

To set folder icon, you may try the following test script.
Please choose source jpg file in the first dialogue and target folder in the second dialogue and see whether it works or not.

IF it works, the rest would be relatively easy (with regard to setting folders' icon as you described).

H

--TEST SCRIPT
set srcpath to POSIX path of (choose file with prompt "Choose source image file.")
set dstpath to POSIX path of (choose folder with prompt "Choose target folder.")
--set dstpath to dstpath's text 1 thru -2 -- remove last slash if necessary.
setIcon(srcpath, dstpath)

on setIcon(srcpath, dstpath)
tell application "Automator"
set nsimg to call method "alloc" of class "NSImage"
set nsimg to call method "initWithContentsOfFile:" of nsimg with parameter srcpath
set nswks to call method "sharedWorkspace" of class "NSWorkspace"
set r to call method "setIcon:forFile:options:" of nswks with parameters {nsimg, dstpath, 0}
call method "release" of nsimg
end tell
return r
end setIcon
--END OF TEST SCRIPT

cf.
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/ObjC_cla ssic/

Jan 26, 2009 9:19 PM in response to Hiroto

Hello Hiroto

I tested your script, but got an error after the statement:

call method "initWithContentsOfFile:" of image id 1 with parameter "/path/to/test/file"
"Automator got an error: Can’t get image id 1."

Using the seticon utility, I was able to get most of the OP's functions, although the grid spacing is a Finder plist item. The following script sets the icon of the currently selected folder to the one used by an enclosed file named "cover.jpg":

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #FFEE80;
overflow: auto;"
title="this text can be pasted into the Script Editor">
tell application "Finder"
if (get windows) is not {} then
set Destination to (target of front Finder window) as text -- folder name
set Source to quoted form of POSIX path of (Destination & "cover.jpg")
set Destination to quoted form of POSIX path of Destination
do shell script "usr/local/bin/seticon " & Source & space & Destination -- change icon
set current view of front Finder window to icon view
set arrangement of icon view options of front Finder window to arranged by name
set icon size of icon view options of front Finder window to 128
end if
end tell</pre>

Jan 26, 2009 10:39 PM in response to red_menace

Hello red_menace,

Thank you for testing my untested code!
Since your code using 'seticon' command works, the rest is only for curiosity.

Probably I must use 'new' instead of 'alloc' here.
Revised test script follows. Incorrigibly. 😉

Hiroto

--TEST SCRIPT2
set srcpath to POSIX path of (choose file with prompt "Choose source image file.")
set dstpath to POSIX path of (choose folder with prompt "Choose target folder.")
--set dstpath to dstpath's text 1 thru -2 -- remove last slash if necessary.
setIcon(srcpath, dstpath)

on setIcon(srcpath, dstpath)
tell application "Automator"
set nsimg to call method "new" of class "NSImage"
set nsimg to call method "initWithContentsOfFile:" of nsimg with parameter srcpath
set nswks to call method "sharedWorkspace" of class "NSWorkspace"
set r to call method "setIcon:forFile:options:" of nswks with parameters {nsimg, dstpath, 0}
call method "release" of nsimg
end tell
return r
end setIcon
--END OF TEST SCRIPT2

cf.
NSObject
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic /

NSImage, NSWorkspace
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/ObjC_cla ssic/

Jan 27, 2009 5:46 AM in response to Hiroto

Your new code fails with the same error. Something is getting created though, since the image id (that it can't get) is incremented on each run - the created item isn't being released due to the script error.

I hadn't looked at Automator to hook into the Cocoa stuff, I figured something like Ruby or Python would need to be used. It would be interesting to get it working - it is a neat idea.

The seticon works, but it needs a file's icon or a .icns file - it would depend if the particular application used to save the file also sets a thumbnail for the icon, otherwise something else would be needed to convert a .jpg to an icon.

Jan 27, 2009 2:54 PM in response to Hiroto

thank you both for your efforts.

sadly both don't work.

After getting the utils I tried on command line:
setimage cover.jpg .
but this sets the icon of the folder to the standard jpeg icon.

so I looked at the manpage and it states a "-d" option for using the contained data instead.
but this doesn't do anything.

and Hiroto's script quits with some similar error as earlier (but something with "id 3")

Jan 27, 2009 4:20 PM in response to red_menace

Hello red_menace,

Thank you very much for testing.
Oh well, I may have missed something very basic here but now I give up.

Regarding Automator's 'call method' command, the Automator Programming Guide has brief description.

cf.
Automator Programming Gudie > Implement an AppleScript Action
pp.64, 66 (PDF version)
http://developer.apple.com/documentation/AppleApplications/Conceptual/AutomatorC oncepts/

It also states that (in p.66) -
"Sometimes, an AppleScript Studio script does not have access to a valid object for a call method command."

So I suspect this may be the reason why my attempt falis.

---
By the way, AppleScript Studio has 'load image' command for creating NSImage object from file.
E.g., my failed codes using Automator can be translated into something like the following one, which should work, I'd think.

--CODE (for AppleScript Studio script)
set srcpath to POSIX path of (choose file with prompt "Choose source image file.")
set dstpath to POSIX path of (choose folder with prompt "Choose target folder.")
--set dstpath to dstpath's text 1 thru -2 -- remove last slash if necessary.
setIcon(srcpath, dstpath)

on setIcon(srcpath, dstpath)
set nsimg to load image srcpath
set nswks to call method "sharedWorkspace" of class "NSWorkspace"
set r to call method "setIcon:forFile:options:" of nswks with parameters {nsimg, dstpath, 0}
delete nsimg
return r
end setIcon
--END OF CODE

cf.
AppleScript Studio Terminology Reference
http://developer.apple.com/documentation/AppleScript/Reference/StudioReference/

All the best,
Hiroto

Jan 27, 2009 7:05 PM in response to Michael[0-9]-__

Hello

Here's another attempt using sips(1) and seticon.
Hope this may work!
H

--SCRIPT (not tested)
set srcpath to POSIX path of (choose file with prompt "Choose source image file.")
set dstpath to POSIX path of (choose folder with prompt "Choose target folder.")
setIcon(srcpath, dstpath)

on setIcon(srcpath, dstpath)
(*
string srcpath, dstpath : POSIX path of source and destination
* NOT in quoted form
*)
set srcpath to quoted form of srcpath
set dstpath to quoted form of dstpath
set tmppath to quoted form of (POSIX path of (path to temporary items folder) & "image w_icontemp.jpg")

set sh1 to "sips -i " & srcpath & " --out " & tmppath -- # add icon to tmppath
set sh2 to "usr/local/bin/seticon " & tmppath & " " & dstpath -- # set icon of dstpath
set sh3 to "rm -f " & tmppath -- # delete tmppath

do shell script (sh1 & " && " & sh2 & " && " & sh3)
return true
end setIcon
--END OF SCRIPT

cf.
sips(1)
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/sips.1.h tml

seticon
http://sourceforge.net/projects/osxutils

Jan 27, 2009 9:06 PM in response to Michael[0-9]-__

OK, I think I got it. I finally found a python script that will set the icon to an image file (and yes, I tested it and it does work on my machine). You will need to chmod the execute permissions in order to run the script, and it also uses the utilities SetFile, Rez, and DeRez, in the Developer Tools (from the OS install disk). Using the python script, the AppleScript would be something like:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #FFEE80;
overflow: auto;"
title="this text can be pasted into the Script Editor">
set ScriptPath to quoted form of POSIX path of (choose file with prompt "Where is the python script?")
set Source to quoted form of POSIX path of (choose file with prompt "Select a source image:")
set Destination to (choose folder with prompt "Select a destination for the icon:")

do shell script ScriptPath & space & Source & space & quoted form of POSIX path of Destination
tell application "Finder" to update Destination
</pre>

Jan 28, 2009 4:41 AM in response to red_menace

Thanks a lot, I think this brought me close to it.

It works, but now I'm trying to adopt it for automation:

<pre>
on run {input, parameters}

repeat with i in input
set folder to (quoted form of POSIX path of i)
set SourceImage to folder & "/cover.jpg"
set Destination to folder

do shell script "/usr/local/bin/assignIcon " & space & SourceImage & space & quoted form of POSIX path of Destination
tell application "Finder" to update Destination
end repeat

return input
end run

</pre>
(sorry for the extra space)


Would be great if you could correct this, then I can run it through Automator and select the Folders.
Thanks!

Jan 28, 2009 1:12 PM in response to Michael[0-9]-__

The term "folder" is a reserved word, so something else needs to be used. Also, when you are dealing with shell scripts, it is important to keep track of the different forms that you are using for the paths. The Finder likes aliases, but you can also use text strings if they are used with the appropriate "file" or "folder" terms. The shell wants POSIX paths - these also need certain characters (such as spaces) escaped, or else the whole path can be quoted. In the following script, I changed the folder variable name and converted the original (Finder) path to text for use later in the script.

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #FFEE80;
overflow: auto;"
title="this text can be pasted into an Automator 'Run AppleScript' action">
on run {input, parameters} -- set the icons of the input folders

repeat with i in input
set TheFolder to (i as text) -- the original (Finder) path
set SourceImage to quoted form of POSIX path of (TheFolder & "cover.jpg") -- the icon
set Destination to (quoted form of POSIX path of TheFolder)

do shell script "/usr/local/bin/assignIcon" & space & SourceImage & space & Destination

tell application "Finder" to update folder TheFolder
end repeat

return input

end run
</pre>

Jan 28, 2009 2:47 PM in response to red_menace

Thanks, red_menace, I incorporated the lines from above and now It does everything I wanted except grid spacing which I guess isn't settable via AppleScript.

Some little downer is, that there is now an "Icon" file lying around in each of the folders.

I tried adding a colon to hide them in the assignIcon Script but this made it stop working.

But maybe I can live with them 🙂

--------
on run {input, parameters} -- set the icons of the input folders

repeat with i in input
set TheFolder to (i as text) -- the original (Finder) path
set SourceImage to quoted form of POSIX path of (TheFolder & "cover.jpg") -- the icon
set Destination to (quoted form of POSIX path of TheFolder)

do shell script "/usr/local/bin/assignIcon" & space & SourceImage & space & Destination

tell application "Finder"
activate
open folder i
select Finder window 1
set current view of front Finder window to icon view
set arrangement of icon view options of front Finder window to arranged by name
set icon size of icon view options of front Finder window to 128
update folder TheFolder
close Finder window 1
end tell

end repeat

return input

end run

Feb 8, 2009 4:35 PM in response to Hiroto

This one worked perfectly!


--CODE (for AppleScript Studio script)
set srcpath to POSIX path of (choose file with prompt "Choose source image file.")
set dstpath to POSIX path of (choose folder with prompt "Choose target folder.")
--set dstpath to dstpath's text 1 thru -2 -- remove last slash if necessary.
setIcon(srcpath, dstpath)

on setIcon(srcpath, dstpath)
set nsimg to load image srcpath
set nswks to call method "sharedWorkspace" of class "NSWorkspace"
set r to call method "setIcon:forFile:options:" of nswks with parameters {nsimg, dstpath, 0}
delete nsimg
return r
end setIcon
--END OF CODE

automatically setting folder icon

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