Setting file icons based on same named image files

I have a folder of files and I have another folder of images that have the same names as the files. What I would like to do is apply an icon of each image to it’s corresponding file. I have found this thread,http://macscripter.net/viewtopic.php?id=18973 and I tried to adapt it, but I just don’t have the knowledge to do so. Any help would be greatly appreciated.

Ian

Hello!

Please post what you have got so far! :slight_smile:

This is it. I know I’m probably not constructing the filename of the icon file right, among other problems.


set NakedFiles to choose folder with prompt "Choose a folder of naked files"
set IconFiles to choose folder with prompt "Choose a folder of image files"

on open (NakedFiles)
	local F, cl
	set F to (NakedFiles's item 1) as Unicode text
	tell application "Finder"
		if ((class of item F) is file) then my iconizeFile(F)
	end tell
end open

on iconizeFile(F)
	local ao, i, IconFile
	tell application "Finder" to tell file F
		set IconFile to "IconFiles & F" as alias
		-- grab the file's icon
		my CopyOrPaste(IconFile, "c")
		-- paste the icon to the container
		my CopyOrPaste(F as alias, "v")
		repeat with i in (get files)
			my iconizeFile((i as alias))
		end repeat
	end tell -- to folder F then to Finder
end iconizeFile

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

Hello

My understanding is that you made several errors.

(1)

the variables NakedFiles and IconFiles are defined only if you RUN the script but in this case the script do only that.

(2)

The open handler is called when you drop a folder icon on it.
In this case, from the handler point of view, NakedFiles is a list containing the path to the folder and the pats to embedded items.

IconFiles is not defined.

As the first item of the passed list is a folder, this open handler does nothing.

(3)

in the iconizeFile handler, the file to iconize is defined (by the passed pathname F) but :
(a) the folder containing the pict files would not be visible if the handler was caller from the implicit Run handler, would not be defined (and would not be visible too) when the handler was called from the open handler

(b) even if the variable IconFiles was visible from the handler point of view, it would not be used because you wrote a wrong instruction :

set IconFile to “IconFiles & F” as alias

Here is an edited version :



on run
	local NakedFiles, IconFiles
	set NakedFiles to choose folder with prompt "Choose a folder of naked files"
	set IconFiles to choose folder with prompt "Choose a folder of image files"
	my main(NakedFiles, IconFiles)
end run

on open (sel)
	local NakedFiles, IconFiles
	set NakedFiles to sel's item 1
	set IconFiles to choose folder with prompt "Choose a folder of image files"
	my main(NakedFiles, IconFiles)
end open

on main(NF, picturesFolder)
	local listeItems, FF
	my iconizeItem(NF as text, picturesFolder)
	tell application "Finder"
		set listeItems to entire contents of NF as alias list
	end tell
	repeat with FF in listeItems
		my iconizeItem(FF as text, picturesFolder)
	end repeat
end main

(*
I renamed the handler because the passed item may be a file or a folder.
*)
on iconizeItem(F, P)
	local ao, pictPath
	(*
I encapsulate the code in a try block because it's not guaranteed that there is a picture file for every item.
*)
	try
	tell application "Finder"
		get name of alias F
	end tell -- to folder F then to Finder
	set pictPath to ((P as text) & result & ".png") as alias
	
	-- grab the file's icon
	my CopyOrPaste(pictPath, "c")
	my CopyOrPaste(F as alias, "v")
	
	end try
end iconizeItem

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 *)
		--keystroke "w" using command down
	end tell -- window 1 then process Finder then System Events
end CopyOrPaste


CAUTION:
I assumed that every picture files are png ones.
It’s easy to change the type of every picture files.
If you plan to use picture files of different type, it would be more complicated.

Yvan KOENIG (VALLAURIS, France) dimanche 29 juillet 2012 20:07:27

See my command line tool SetFileIcon. There’s an example applescript to show you how to use it. I wouldn’t take much to modify the applescript to work how you want. I hope that helps. :smiley:

Thank you guys. I’m obviously in over my head here, but I don’t know any better way to learn. I’ll dig in with what you told me and report back.

Yvan, Why would it be more difficult with another image type?

Ian

Assuming that you have a picture file named “for file 1.png” and an other one named “for file 2.jpeg”, comparing the picture name to the file name can’t be done using brute force as I did in the “old” script.

Here is a new one accepting different kind of picture files.
It would be easy to edit it to use the Regulus’s SetFileIcon (which I use daily).



on run
   local NakedFiles, IconFiles
   set NakedFiles to choose folder with prompt "Choose a folder of naked files"
   set IconFiles to choose folder with prompt "Choose a folder of image files"
   my main(NakedFiles, IconFiles)
end run

on open (sel)
   local NakedFiles, IconFiles
   set NakedFiles to sel's item 1
   set IconFiles to choose folder with prompt "Choose a folder of image files"
   my main(NakedFiles, IconFiles)
end open

on main(NF, picturesFolder)
	local listeItems, listePictures, pictDescriptors, aPict, pName, pExt, FF, fName, P
	
	tell application "Finder"
		set listeItems to entire contents of NF as alias list
		set listePictures to entire contents of picturesFolder as alias list
		set pictDescriptors to {}
		repeat with aPict in listePictures
			set {pName, pExt} to {name of aPict, name extension of aPict}
			set end of pictDescriptors to {text 1 thru -(2 + (count pExt)) of pName, pExt}
		end repeat
	end tell
	set end of listeItems to NF
	repeat with FF in listeItems
		tell application "Finder"
			set fName to name of FF (* here, FF is an alias *)
		end tell
		repeat with P from 1 to count of pictDescriptors
			if item 1 of item P of pictDescriptors is fName then
				my iconizeItem(FF as alias, ((picturesFolder as text) & fName & "." & item 2 of item P of pictDescriptors) as alias)
			end if
		end repeat
	end repeat
end main

(*
I renamed the handler because the passed item may be a file or a folder.
*)
on iconizeItem(F, P)
	(*
Now, there is no need to encapsulate the code in a try block because we tested the availability of the file in the main handler.
*)
	my CopyOrPaste(P, "c")
	my CopyOrPaste(F, "v")
end iconizeItem

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 *)
		--keystroke "w" using command down
	end tell -- window 1 then process Finder then System Events
end CopyOrPaste


You may enhance the script using Shane STANLEY’s fine gem entitled ASObjC Runner.app.
It offers the command :

look in list‚v : Get the indexes of an object or list of objects in a list, or if the sublists parameter is true, in the sublists of a list.
look in list list of any : A list of items, or a list of sublists of items.
[matching any] : An item to look for.
[matching items list of any] : A list of items to look for.
[sublists boolean] : If true, operates on the list’s sublists rather than the list itself. Default is false.
[inverting boolean] : If true, returns indexes of items that don’t match. Default is false.
[only first boolean] : If true, only the index of the first match will be returned. Default is false.
→ any : The list of indexes, or a list of them.

allowing us to drop one of the loops embedded in the posted script.

Yvan KOENIG (VALLAURIS, France) mardi 31 juillet 2012 11:31:59