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)
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
--[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)
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.
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.
-- [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)
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?
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)
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:)
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
-- [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)