Apple Script - Recursive Function

Hello there!


So, I'm struggling to code a recursive function that would go through every possible sub-folder, sub-sub-folder and so on of a parent folder (the one that's opened in the foreground), but I came to a point where the script gets an error message when it tries to make the recursion.


I suppose that this error is due to the class scope, but to be honest I don't know how to fix this issue.


Here's my apple script.


on openFolders(folderName)
	
	tell application "Finder"
		
		set theWindow to window 1
		set folderCount to count of folders of theWindow
		
		if folderCount is 0 then
			return
		end if
		
		set allFolders to every folder of theWindow
		
		repeat with i from 1 to folderCount - 1
			
			openFolders(item i in allFolders)
			
			delay 1 -- Optional delay to wait for the folder to open
			
		end repeat
		
		set target of theWindow to item folderCount in allFolders
		
		openFolders(item folderCount in allFolders)
		
	end tell
	
end openFolders

on run {input, parameters}
	
	tell application "System Events" to tell process "Finder"
		set frontmost to true
	end tell
	
	set theWindow to window 1
	
	openFolders(theWindow)
	
	return input
	
end run


P.S. I ended up doing apple scripts due to the fact that 'SetFolderViews' in Automator messes with the Finder backgrounds and now I'm trying to build an automation script that would revert the background of every folder to 'Default'. I'd be more than happy to share the final script with you and with everybody who needs it. Who knows, I might upload it to GitHub if I could get past this current issue.


Cheers,

Stefan-Daniel

MacBook Pro 13″, macOS 14.0

Posted on Oct 26, 2023 3:59 AM

Reply
Question marked as Best reply

Posted on Oct 26, 2023 8:08 AM

Here are some lessons in AppleScript recursive handlers. You may be able to adapt some of that information to your solution. Finder is a tree sloth with deep directory hierarchies and larger file counts.

4 replies

Oct 26, 2023 1:19 PM in response to VikingOSX

Or as MrHoffman suggests:


find $startfolder -type d -maxdepth 1 -print0 | xargs -0 -n 1 -P 4 -I{} zsh -c 'onlyfolders+=( ${0} );print -Dl $onlyfolders' {}


or to get a folder count:


find $startfolder -type d -maxdepth 1 -print0 | xargs -0 -n 1 -P 4 -I{} zsh -c 'onlyfolders+=( ${0} );print -Dl $onlyfolders' {} | wc -l


Tested on Sonoma 14.1.




Oct 26, 2023 2:09 PM in response to VikingOSX

Been staring at the screen too long today and made that more difficult than it needs to be. In the Terminal


onlyfolders=( $(find ./parentFolder -type d -print) ) && print -Dl $onlyfolders && print ${#onlyfolders}



This shoves the recursive folder-only contents found in the parentFolder hierarchy into the array onlyfolders. It then prints a list of those folders using a tilde path, and then finally the count of the array items.

Apple Script - Recursive Function

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.