Custom Icon question

Hello, am new here and not so savvy at scripting, so please pardon if i dont get it…

Is there a service or Script I can have, that pastes a custom icon onto a folder, that contains several more folders, that will do every single folder, inside that folder?

First of all: Welcome to MacScripter!

Well you’re not the first one who asked this. So I refer to this topic so you can continue there. It’s the best answer to your questions of all similar topics. If there is something you don’t understand in there don’t hesitate to ask but all information required is in there and on hank’s website (link can be found in that topic as well).

Hello, thank you for replying.
I actually read this before posting. My request differences in wondering if it is possible to have every subfolder folders inside be changed as well. Not just a single folder. Possibly as a service.
thank you!

I guess that this script may help.



--[SCRIPT put-icons]
(*
Yvan KOENIG (VALLAURIS, France)
2012/07/09
edited on 2012-07-11
*)

on run
	my commun(choose folder)
end run

--=====

on open sel
	local maybe
	set maybe to sel's item 1 as alias
	maybe as text
	tell application "System Events" to (class of disk item result) as text
	tell application "Finder"
		if result is in {"folder", "«class cfol»"} then
			my commun(maybe)
		else
			error """ & maybe & "" isn't a folder !"
		end if
	end tell -- Finder
end open -- Open

--=====

on commun(dossier) -- dossier is an alias
	
	tell application "Finder"
		files of folder dossier whose name extension is in {"jpeg", "jpg", "png", "tiff", "tif"}
		item 1 of result as alias
		open result using application file id "com.apple.Preview" as alias
	end tell
	set the clipboard to "" -- added
	my activateGUIscripting() -- moved
	activate application "Preview"
	tell application "System Events" to tell application process "Preview"
		keystroke "ac" using {command down} -- Select All, Copy
		keystroke "w" using {command down} -- Close Window
	end tell
	(*
Now, the clipboard contains the picture to use as icon *)
	my exploreTraite(dossier, "")
end commun

-- ===========

on exploreTraite(elem, ptree) (*
elem est un alias
elem is an alias *)
	local elem_
	set elem_ to elem as text
	tell application "System Events" to (class of disk item elem_) as text
	if result is in {"folder", "«class cfol»"} then (* CAUTION, script returns "folder" but App returns "«class cfol»" *)
		my ExploreUnDossier(elem_, ptree)
	end if -- laClasse is not .
end exploreTraite

--=====

on ExploreUnDossier(dossier, ptree) (* deux chaines *)
	local nomElement, cheminElement
	my CopyOrPaste(dossier as alias, "v")
	repeat with nomElement in list folder dossier without invisibles
		try
			nomElement as text
			set cheminElement to dossier & result
			tell application "System Events" to set c to name of disk item dossier
			my exploreTraite(cheminElement as alias, ptree & c & ":")
		on error
			set rapport to rapport & dossier & msg970 & return
		end try
	end repeat
end ExploreUnDossier

--=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

--=====

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]


It uses the first picture file stored in the master folder to build an icon applied to the master folder and its subfolders.

Yvan KOENIG (VALLAURIS, France) mardi 10 juillet 2012 11:50:52

Hi, thanks for replying!
In AppleScript, i get this error message:

AppleScript Error
“Can’t make item 1 of {} into type alias.”

Hello

I carefully wrote :

It uses the first picture file stored in the master folder to build an icon applied to the master folder and its subfolders.

I logically get the described warning when there is no picture file in the master folder.

I discovered a missed behavior. Sometimes, the GUIScripting code copying the picture from the Preview window is triggered when the picture file isn’t really open so the script paste what is stored in the clipboard before running the script.
To get rid of that, I added two instructions :
one which empty the clipboard
one which put a short delay before triggering the GUI Scripting code. To do that, I moved the instruction supposed to enable GUI Scripting.

Yvan KOENIG (VALLAURIS, France) mercredi 11 juillet 2012 15:00:52

I don’t know about services, but here’s how you might get Hank’s executable to work with the folder and every folder in it:

-- These quoted POSIX paths should be to the base folder of the hierarchy and to the image or icon file respectively.
set baseFolder to quoted form of POSIX path of (choose folder with prompt "Select the base folder of the hierarchy you want to iconify..")
set imagePath to quoted form of POSIX path of (choose file with prompt "Select the icon or image you want to use..")

-- This quoted POSIX path should be to wherever you've stored Hank's "SetFileIcon" executable.
set SetFileIcon to quoted form of ((POSIX path of (path to scripts folder)) & "Libraries/Unix executable/SetFileIcon")
-- Pre-construct the beginning of the shell script command
set commandLine to SetFileIcon & " -image " & imagePath & " -file "

-- Get POSIX paths to all the folders in the hierarchy.
set folderPaths to paragraphs of (do shell script ("find -f " & baseFolder & " \\( -type d \\)"))

-- Apply the icon to each folder.
repeat with thisPath in folderPaths
	do shell script (commandLine & quoted form of thisPath)
end repeat

I f i could show you guys the script that i have that works, maybe you can suggest what i need to change for this to work for more than one folder at a time?
thank you!

(*
Image - Duplicate as JPEG
*)

property done_foldername : “JPEG Images”
property originals_foldername : “Original Images”
property newimage_extension : “jpg”
– the list of file types which will be processed
– eg: {“PICT”, “JPEG”, “TIFF”, “GIFf”}
property type_list : {“TIFF”, “GIFf”, “PNGf”, “PICT”}
– since file types are optional in Mac OS X,
– check the name extension if there is no file type
– NOTE: do not use periods (.) with the items in the name extensions list
– eg: {“txt”, “text”, “jpg”, “jpeg”}, NOT: {“.txt”, “.text”, “.jpg”, “.jpeg”}
property extension_list : {“tif”, “tiff”, “gif”, “png”, “pict”, “pct”}

on adding folder items to this_folder after receiving these_items
tell application “Finder”
if not (exists folder done_foldername of this_folder) then
make new folder at this_folder with properties {name:done_foldername}
end if
set the results_folder to (folder done_foldername of this_folder) as alias
if not (exists folder originals_foldername of this_folder) then
make new folder at this_folder with properties {name:originals_foldername}
set current view of container window of this_folder to list view
end if
set the originals_folder to folder originals_foldername of this_folder
end tell
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
tell application “Finder”
my resolve_conflicts(this_item, originals_folder, “”)
set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
set the source_file to (move this_item to the originals_folder with replacing) as alias
end tell
process_item(source_file, new_name, results_folder)
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application “Finder”
activate
display dialog error_message buttons {“Cancel”} default button 1 giving up after 120
end tell
end if
end try
end adding folder items to

on resolve_conflicts(this_item, target_folder, new_extension)
tell application “Finder”
set the file_name to the name of this_item
set file_extension to the name extension of this_item
if the file_extension is “” then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
if the new_extension is “” then
set target_name to file_name
set target_extension to file_extension
else
set target_extension to new_extension
set target_name to (the trimmed_name & “.” & target_extension) as string
end if
if (exists document file target_name of target_folder) then
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & “.” & (name_increment as string) & “.” & target_extension) as string
if not (exists document file new_name of the target_folder) then
– rename to conflicting file
set the name of document file target_name of the target_folder to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
end repeat
end if
end tell
return the target_name
end resolve_conflicts

– this sub-routine processes files
on process_item(source_file, new_name, results_folder)
– NOTE that the variable this_item is a file reference in alias format
– FILE PROCESSING STATEMENTS GOES HERE
try
– the target path is the destination folder and the new file name
set the target_path to ((results_folder as string) & new_name) as string
with timeout of 900 seconds
tell application “Image Events”
launch – always use with Folder Actions
set this_image to open file (source_file as string)
save this_image as JPEG in file target_path with icon
close this_image
end tell
end timeout
on error error_message
tell application “Finder”
activate
display dialog error_message buttons {“Cancel”} default button 1 giving up after 120
end tell
end try
end process_item