Run a Finder script in all sub folders?

I cobbled together a script that sets the view of the current selected Finder window:

tell application "Finder"
	activate
	
	tell icon view options of the front Finder window
		set arrangement to arranged by name
		set icon size to 64
		set shows item info to true
		set shows icon preview to true
	end tell
	
	tell the front Finder window
		set toolbar visible to true
		set current view to icon view
		set the sidebar width to 160
	end tell
	
end tell

I’d like it run on all sub folders of the current window if possible to set the view (icon size, arranged by etc.)

Any help would be appreciated.

Google helped…

I did Google it but was looking for a general AppleScript to run on sub folders (not one that specifically for changing views). This looks just what I need, thanks!

Hello

Maybe this skeleton will be a good starting point :


--[SCRIPT skeleton]
(* 

Yvan KOENIG (VALLAURIS, France)
2012/11/13
*)

property msg99 : "" -- globale

#=====

(* deux lignes exécutées si on double clique 
sur l'icône du script application
¢ two lines executed if one double click 
the application script's icon *)
if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
	set msg00 to "Choisir un dossier ."
else
	set msg00 to "Choose a folder ."
end if
tell application "Finder"
	choose folder with prompt msg00
	set listeFichiers to every item in result as alias list
end tell
if (count listeFichiers) = 0 then
	return
else if (count listeFichiers) = 1 then
	open {listeFichiers}
else
	open listeFichiers
end if

#=====

on open (sel) (* sel contient une liste d'alias des éléments
qu'on a déposés sur l'icône du script (la sélection)
¢ sel contains a list of aliases of the items   
dropped on the script's icon (the selection) *)
	
	try
		if msg99 is "" then my prepareMessages()
		
		repeat with elem in sel
			my exploreTraite(elem as text, "")
		end repeat
		
		
	on error MsgErr number NroErr
		if NroErr is not -128 then
			beep 2
			tell application (path to frontmost application as string)
				display dialog "" & NroErr & " : " & MsgErr with icon 0 buttons {msg99} giving up after 20
			end tell
		end if -- NroErr is.
		return
	end try
end open

#===== Routines 

on exploreTraite(elem, ptree)
	# elem is a string
	tell application "System Events" to tell disk item elem
		set laClasse to class as text
		try
			set leTypeId to type identifier
		on error
			set leTypeId to "maybe a folder"
		end try
	end tell
	if laClasse is "folder" then
		
		my ExploreUnDossier(elem, ptree)
		
	else if laClasse is "file package" then
		# do what you need with a package
		if leTypeId is "com.apple.iwork.pages.sffpages" then
			my TraiteUnFichier(elem, ptree)
		end if
	else
		if leTypeId is "com.apple.iwork.pages.pages" then
			my TraiteUnFichier(elem, ptree)
		else
			# do what you want when the item doesn't match your requirements
		end if -- file type.
	end if
end exploreTraite

#=====

on ExploreUnDossier(dossier, ptree)
	tell application "System Events"
		set listFolder to path of every disk item of (folder dossier) whose visible is true
	end tell
	repeat with cheminElement in listFolder
		tell application "System Events" to set C to name of folder dossier
		my exploreTraite(cheminElement, ptree & C & ":")
	end repeat
end ExploreUnDossier

#=====

on TraiteUnFichier(leFichier, ptree)
	
	tell application "System Events" to set leVraiNom to (name of disk item leFichier)
	
	# Do what you want with the file
	
end TraiteUnFichier

#===== 

on prepareMessages()
	
	if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
		set msg99 to " Vu "
	else
		set msg99 to "Oops"
	end if
	
end prepareMessages

--[/SCRIPT]

Yvan KOENIG (VALLAURIS, France) mardi 13 novembre 2012 17:10:15

Thanks, I’m going to see if I can get this working for me this evening.

I got the script (linked to previously) to work how I wanted, but it’s a droplet saved as an app. What I’d like to do is run it as a ‘regular’ script which I’d activate from Alfred or Keyboard Maestro to work on the current selected Finder window and all of it’s sub folders. How would I do this?

Here’s my adapted version of the script:


property counter : 0

on open theFolders
	
	tell application "Finder"
		tell the front Finder window
			set toolbar visible to true
			set the sidebar width to 160
		end tell
	end tell
	
	set t to (current date)
	set counter to (count theFolders)
	tell application "Finder" to activate
	-- Do the business with each dropped folder
	repeat with thisFolder in theFolders
		setViews(thisFolder)
	end repeat
	set t1 to (current date) - t
	tell application "Finder" to display dialog (t1 as string) & " seconds for " & counter & " folders." with icon note
end open

on setViews(thisFolder)
	tell application "Finder"
		tell (get thisFolder's window)
			set its current view to icon view
			tell its icon view options
				set arrangement to arranged by name
				set icon size to 64
				set shows item info to true
				set shows icon preview to true
			end tell
		end tell
		try
			set theSubfolders to every folder of thisFolder as alias list
		on error
			set theSubfolders to first folder of thisFolder as alias as list
		end try
	end tell
	set subfolderCount to (count theSubfolders)
	if subfolderCount > 0 then
		set counter to counter + subfolderCount
		repeat with i from 1 to subfolderCount
			setViews(item i of theSubfolders)
		end repeat
	end if
end setViews

Also, what is the first line of the script for? (property counter : 0)