How to change a folder icon ?

Hello,

I would like to write a script that fix an icon to a folder from a png image file.
I new to applescript and I’m not able to finish that I started.
My code :

[code]tell application “Finder”
set icon_image_file to file “my_icon.png” of folder “Users:Users:luc:Desktop”
set testdir to folder “test” of folder “Users:Users:luc:Desktop”

    -- save icon_image_file with itself as icon
    set icon_image_file_string to icon_image_file as string
    tell application "Image Events"
            launch
            set icon_image to open file icon_image_file_string
            save icon_image with icon
            close icon_image
    end tell
    -- here, the png icon_image_file has itself as icon

    -- Now I would like to fix the icon of testdir to the icon of icon_image_file
    -- BUT IT DON'T WORKS
    set icon of testdir to icon of icon_image_file 

end tell
end run[/code]
Any ideas to finish my script ?

Luc.

Model: powerbook G4
Browser: Firefox 2.0
Operating System: Mac OS X (10.4)

Hi Luc,

AFAIK it’s impossible to do it this way.
Though there is a property “icon” of an Finder item, it seems to be non accessible by AppleScript.

Two possible solutiona are QuicKeys http://www.cesoft.com/,
or to copy the resource fork which contains the icon with a shell command:

do shell script "ditto -rsrcFork path/to/source/ path/to/destination"

I found this information in the apple mailing lists

http://lists.apple.com/archives/Applescript-users/2002/Oct/msg01260.html

Model: G5 dual 2,5 GHz
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi StefanK,

Thanks for the ditto command. It partially solves the second part of my problem because I can copy the resource fork of one folder to another one.
ditto -rsrcFork dir_with_icon ~/Desktop/dir_without_icon

But, how can I generate a folder with png file as icon ?
Is there an infinite loop in the solution :wink:

Luc.

Hello

I uses this script daily:

--[SCRIPT colle_icone]

on run
	open ((choose file) as list)
end run

on open (sel)
	tell application "Finder" to set f to (sel's item 1) as alias
	-- grab the file's icon
	my CopyOrPaste(f, "c")
	-- now the icon is in the clipboard
	tell application "Finder" to set c to (container of f) as alias
	my CopyOrPaste(c, "v")
end open

on CopyOrPaste(i, cv)
	tell application "Finder"
		activate
		open information window of i
	end tell
	tell application "System Events" to tell process "Finder" to tell window 1
		keystroke tab -- select icon button
		keystroke (cv & "w") using command down (* (copy or paste) + close window *)
	end tell -- window 1 then  process Finder then System Events
end CopyOrPaste

--[/SCRIPT]

Yvan KOENIG (from FRANCE vendredi 27 octobre 2006 20:56:20)

wow, Yvan, this is pretty clever :D:D:D

Hi Yvan,

Yeah ! Perfect. It is exactly what I would like to to.
This method is very suprising but it works :smiley:

Thanks again,

Luc.

Re,

I post the finished script that set a folder icon from a png file.

[code]on run
set icon_image_file to file “Users:Users:luc:Desktop:mon_icon.png” as alias
set testdir to file “Users:Users:luc:Desktop:test” as alias

my saveImageWithItselfAsIcon(icon_image_file)
my copyIconOfTo(icon_image_file, testdir)

end run

on saveImageWithItselfAsIcon(icon_image_file)
– save icon_image_file with itself as icon
set icon_image_file_string to icon_image_file as string
tell application “Image Events”
launch
set icon_image to open file icon_image_file_string
save icon_image with icon
close icon_image
end tell
end saveImageWithItselfAsIcon

on copyIconOfTo(aFileOrFolderWithIcon, aFileOrFolder)
tell application “Finder” to set f to aFileOrFolderWithIcon as alias
– grab the file’s icon
my CopyOrPaste(f, “c”)
– now the icon is in the clipboard
tell application “Finder” to set c to aFileOrFolder as alias
my CopyOrPaste(result, “v”)
end copyIconOfTo

on CopyOrPaste(i, cv)
tell application “Finder”
activate
open information window of i
end tell
tell application “System Events” to tell process “Finder” to tell window 1
keystroke tab – select icon button
keystroke (cv & “w”) using command down (* (copy or paste) + close window *)
end tell – window 1 then process Finder then System Events
end CopyOrPaste[/code]
Luc.

Hello

I wish to say that I tried for a long time to automate this process which I was doing the same by hand.

If I remember well, I found or even got as a responce a script of this kind on one forum.

After the crash of my HD I have no track of the true origin bust feels that it is honest to say thank you to the original author.

Yvan KOENIG (from FRANCE samedi 28 octobre 2006 16:27:03)

I posted a similar script in this thread some time back. It’s certainly not as elegant as yours, though, Yvan.

j

hey everyone,

i have searched for an answer to my question but i am super new to this so please excuse me if i am asking the obvious.

i have used Yvan’s script to change the folder icon manually but i have a large music collection and the album art in each album folder. is there someway of automating the process of looking through a collection of folders and where there is a .jpg in there setting the folder icon to that?

pointers in the right direction would be most appreciated.

thanks in advance :slight_smile:

phill

Hello

I hope that this one will do the trick.

-- [SCRIPT iconizer]

set _ to choose folder
open (_ as list)

on open (sel)
	local F, cl
	set F to (sel's item 1) as Unicode text
	tell application "Finder"
		if class of item F is document file then
			my saveImageWithItselfAsIcon(F as alias)
		else
			if ((class of item F) is folder) then my iconizeFolder(F)
		end if
	end tell
end open

on iconizeFolder(F)
	local ao, i, lesJPEGs, fichier
	tell application "Finder" to tell folder F
		set lesJPEGs to every item whose kind contains "jpeg"
		if lesJPEGs is not {} then
			set fichier to (lesJPEGs's item 1) as alias
			my saveImageWithItselfAsIcon(fichier) (* I'm not sure that the file already has an icon *)
			-- grab the file's icon
			my CopyOrPaste(fichier, "c")
			-- paste the icon to the container
			my CopyOrPaste(F as alias, "v")
		end if
		repeat with i in (get folders)
			my iconizeFolder((i as alias))
		end repeat
	end tell -- to folder F then  to Finder
end iconizeFolder

on saveImageWithItselfAsIcon(F)
	tell application "Image Events"
		launch
		tell (open F)
			save with icon -- save icon_image_file with itself as icon 
			close
		end tell
	end tell
	tell application "Finder" to update F
end saveImageWithItselfAsIcon

on CopyOrPaste(i, cv)
	tell application "Finder"
		activate
		open information window of i
	end tell
	tell application "System Events" to tell process "Finder" to tell window 1
		keystroke tab -- select icon button
		keystroke (cv & "w") using command down (* (copy or paste) + close window *)
	end tell -- window 1 then  process Finder then System Events
end CopyOrPaste

-- [/SCRIPT]

I may do the task using osxutils’s seticon cmd but it doesn’t work if the file/folder’s pathname contains accented chars so, I dropped it.

Yvan KOENIG (from FRANCE vendredi 10 novembre 2006 14:05:56)

Wow, talk about quick service :slight_smile: Thanks so much.

I tried running the script and pointed it at the root folder of my music collection. it ran for a few seconds and then returned but nothing in the folder structure was changed.

am i running it correctly or is there something else i would need to do to get it to work correctly?

cheers,

phill

Hello

If you run the script on a folder containing subfolders containing jpeg files, the icon of the first jpeg file in a subfolder is given to this subfolder.

Isn’t it what was asked for ?

Yvan KOENIG (from FRANCE vendredi 10 novembre 2006 20:26:23)

Yvan,

A thousand apologies. I was running the script against a backup directory of my music which in fact didn’t have the album art in there either! Doh!

The scripts works perfectly.

Now to learn AppleScript…

Any recommendations for good websites as I am away travelling at the moment so getting books isn’t exactly feasible?

Cheers,

Phill

Just stick around here for a while. In my opinion this is the number 1 spot to learn appescript. Look at some peoples scripts at the macscripters section, and look at code people have posted here. I have learned almost all my techniques from here. Don’t forget to practice, practice practice! Do small little test exercises to learn different techniques, of doing things. Good luck:)

Hello

If you read French, you may look at:

http://didierdurandet.free.fr/applescript/

It offers a tutorial which give the basis.

Yvan KOENIG (from FRANCE samedi 11 novembre 2006 20:10:53)

But how can you apply the ditto command when moving an icon from a (source) folder to (destination) folder…?
(invisible icon resources)

Hello

Here is a new version using “osxutils”.

As this set of commands doesn’t work if one used pathname contains accented chars, I coded a workaround:
the folder containing the source file is moved into the Temporary Folder and renamed.
The iconize process is done then the folder is moved back to it’s original folder and it is renamed with its original name.
As is, the coded workaround assumes that the jpeg filename doesn’t contain accented and make the same assumption upon the boot volume.
It’s what I am working with :wink:

-- [SCRIPT iconizer]
(* uses osxutils 
with a workaround for accented chars

osxutils is available (free) from
http://osxutils.sourceforge.net/
*)

set _ to choose folder
open (_ as list)

on open (sel)
	local F, cl
	set F to (sel's item 1) as Unicode text
	tell application "Finder"
		if class of item F is document file then
			my saveImageWithItselfAsIcon(F as alias) (* I'm not sure that the file already has an icon *)
		else
			if ((class of item F) is folder) then my iconizeFolder(F)
		end if
	end tell
end open

on iconizeFolder(F)
	local ao, i, lesJPEGs, fichier
	tell application "Finder" to tell folder F
		set lesJPEGs to every item whose kind contains "jpeg"
		if lesJPEGs is not {} then
			set fichier to (lesJPEGs's item 1) as alias
			my saveImageWithItselfAsIcon(fichier) (* I'm not sure that the file already has an icon *)
			my copyIcon(fichier)
		end if
		repeat with i in (get folders)
			my iconizeFolder((i as alias))
		end repeat
	end tell -- to folder F then  to Finder
end iconizeFolder

on saveImageWithItselfAsIcon(F)
	tell application "Image Events"
		launch
		tell (open F)
			save with icon -- save icon_image_file with itself as icon 
			close
		end tell
	end tell
	tell application "Finder" to update F
end saveImageWithItselfAsIcon

on copyIcon(F) (* F is an alias *)
	local tName, F, coF, cocoF, nocoF, tempStorage, tempCof
	tell application "Finder"
		(* temporarily move and rename so the used pathname will not contain accented chars *)
		set coF to (container of F) as alias
		set {cocoF, nocoF, tName} to {(container of coF) as alias, name of coF, "osxutils_slituxso"}
		set tempStorage to (path to temporary items from user domain) as alias
		set coF to (move coF to tempStorage) as alias
		set name of coF to tName
	end tell -- to Finder 
	
	do shell script "/usr/local/bin/seticon " & quoted form of POSIX path of F & " " & quoted form of POSIX path of coF
	
	tell application "Finder"
		(* back to original storage and name *)
		set coF to (move coF to cocoF) as alias
		set name of coF to nocoF (* remet le vrai nom *)
		update coF
	end tell
	
end copyIcon

-- [/SCRIPT]

Yvan KOENIG (from FRANCE dimanche 12 novembre 2006 12:22:38)

hey

i applied the script using osxutils, but i get the error: sh: line 1: /usr/local/bin/seticon: No such file or directory

any idea

i first applied the first script, without the workaround, but not every folder containing an image jpg file got an icon

thx

Hello

SetIcon is a part of osxutils is available (free) from
http://osxutils.sourceforge.net/

You have to download it.
Then, install the Seticon file where it must be stored.

Yvan KOENIG