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

Automating folder icon change and using Jpg in folder you are changing.

Hi

Does any one know of a way of automating the change in folder icons and, using the icon (in most cases jpg) inside the folder you are changing thus, avoiding the "Get Info" and Command C method.

The reason I ask is that I have many music folders with an album cover that is a jpg icon and I want to change it as default. But with 1000's of these folder you can see why I would like to automate the process.

Any advise and help is appreciated.


Steve

Imac - Mac pro, Mac OS X (10.5)

Posted on Jun 22, 2009 8:48 AM

Reply
7 replies

Jun 23, 2009 4:42 AM in response to red_menace

Hi Red Menace

Thank you for your reply and I followed your link..

Excuse the the somewhat Dim approach but what I did is is copied the code:

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

And tried to compile, but I got an error: "Expected end of line but found identifier" on the word image.

Please can you advise or help o what code worked and finally how do I run it on the folders.

Steve

Jun 23, 2009 3:47 PM in response to Dark-Star

Since most of the default utilities deal with icon files, the main thing is to gather up all of the various tools - the script itself is just a few lines. The final solution is in this reply, which is a Run AppleScript action that sets the icon of the input folder items to the image that is in the file cover.jpg in each folder (it does other stuff that you may not need, such as setting the Finder view).

The action uses a Python script that calls some utilities that are included in Apple's Developer Tools (available as an optional installation on your OS install disk). You can download the script and set it's execute permissions, but if that sounds a bit scary I have it on my iDisk - you can download it here (it is a text file, so you can take a look at what is in there). In the topic it was named assignIcon and placed in the folder /usr/local/bin/ - it is named AssignIcon.py on my iDisk, but you can name it and put it wherever you want.

I don't know your scripting ability, but if you need some assistance in tweaking things once you get all of the pieces, post back with the details.

Jul 14, 2009 4:38 PM in response to l18807

Cover Flow should show whatever icon the item has - are you successful in changing the icon?

Since my last post, I have put everything together into a single AppleScript:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #FFEE80;
overflow: auto;"
title="this text can be pasted into the Script Editor">
-- assign an icon to a folder or file (requires Developer Tools)
-- any image file can be used for the icon


property FolderItem : true -- true will select folders, false will select files

try -- check for tools
get POSIX file "/Developer/Tools/" as text as alias
on error
error "Developer Tools not installed"
end try

set IconFIle to (choose file with prompt "choose an image for the icon:")
do shell script "/usr/bin/file " & quoted form of POSIX path of IconFIle -- determine the icon file type
if the result does not contain "image" then error "The icon file is not an image"
if FolderItem then
set Destination to POSIX path of (choose folder with prompt "choose a folder to assign the icon to:")
else
set Destination to POSIX path of (choose file with prompt "choose a file to assign the icon to:")
end if

try
tell application "Finder" -- copy the icon image to a temporary file
(duplicate IconFIle 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
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 the folder
set Command to Command & "/Developer/Tools/SetFile -a V " & TheTarget & "; " -- make it invisible
set Command to Command & "/Developer/Tools/SetFile -a C " & quoted form of Destination -- set custom icon attribute
else
set Command to "/Developer/Tools/Rez -a " & TempResource & " -o " & quoted form of Destination -- add resource to the file
end if
do shell script Command -- do it

on error errmess number errnum -- oops
log errmess
display alert (errnum as text) message errmess buttons {"OK"}
end try

try -- remove temporary files
do shell script "rm " & TempImage & space & TempResource
end try

tell application "Finder" to update (Destination as POSIX file)
</pre>

Automating folder icon change and using Jpg in folder you are changing.

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