Using AppleScript, how can I loop through all folders and sub-folders within the selected parent folder of Finder?

I’m trying to collapse all Finder child folders/sub-folders so that navigating anywhere under the currently selected folder will not display any expanded folders that were remembered on previous and forgotten searches. I primarily navigate Finder in List view and find it disorienting when I’m presented with a screen of randomly expanded folders. I’d find it easier to see folders as they would be displayed from a “fresh start” - no expansions.

I’ve used Shortcuts & Automator to create simple apps (Quick Actions, Workflows and Services) that can be called using my function keys. The app I made to collapse Finder folders is very basic. When I press F12 in Finder, it does the following:

on run {input, parameters}   
         tell application "System Events"
                  keystroke "a" using command down
                  key code 123 using control down --left arrow
                  key code 126 using option down --up arrow
         end tell
         return input
end run 

This works fine but only takes care of the primary (right-side) view. Selecting any sub-folder will still potentially show an uncollapsed mess. I’d like to have it iterate through each sub-folder and collapse their display as well. I’ve gathered that AppleScript can hang/error out if iterating through a large number of sub-folders so it should probably stop the iterations at some point.

(Apologies if my terminology is unclear when referencing parent, child, sub-folders and the like. I’m just after a “fresh start” navigators view that is possibly constrained, if needed, to avoid scripting errors.)

I’ve found many examples of recursive folder navigation in a variety of scripting flavors but can’t understand how to apply any to my ends. There seem to be many different ways to do this and that tyranny of choice only adds to my confusion. Plus, after a certain age, recursion becomes incomprehensible and I’m well past that point.

Maybe there’s a system file I could edit/delete that keeps track of the last view state for each Finder folder? I’m open to other ideas. Thanks for any help you can provide.

Shouldn’t it be

key code 123 using option down

Don’t know and have not tried it that way. It works fine the way it is to collapse the view into something intelligible. The problem is that if I expand to a sub-folder and the last view of that sub-folder had expansions (down-arrows) then that’s what I’ll get. What I’d like to get is a view that did not have any expansions - just file names and folder names of the immediate contents of that sub-folder.

I could just run my script every time I drill down into an ‘open’ child folder but that slows down my navigation a bit. Instead I’d like to see a pristine view of everything under my selected folder. I.e., I’d like to see what I’d see if none of the sub-folders had ever been viewed before.

Finder is ‘helping’ me more than I’d like it to by remembering my last view of any folder I select. I’m looking for a way to tell it to forget.

You can toggle the open/closed status of a folder and its subfolders by option-clicking on what Apple calls the disclosure triangle just before a folder. This is a toggle and may need to be done twice. On my Sonoma computer, you can do the same thing with option-right arrow and option-left arrow.

The following collapses every folder in the user’s home folder and subfolders but appears to do more than you want and seems unnecessary. This script has to be run in the background to work properly (for example by way of the Script Menu).

set userFolder to (path to home folder)

tell application "Finder"
	close every Finder window
	select userFolder
	repeat 20 times
		if exists window 1 then exit repeat
		delay 0.1
	end repeat
end tell

tell application "System Events"
	key code 124 using option down -- right arrow
	delay 0.5 -- test different values
	key code 123 using option down -- left arrow
end tell

-- tell application "Finder" to close every Finder window -- if desired

Option clicking doesn’t really get it for me. I’m not sure how I’d script it but it wouldn’t do more than what I’ve already got - a first-level collapse. It leaves sub/child folders in whatever state they were in.

And you are correct about running that more comprehensive script in background. It’s either overkill or it would force me to always want to view the same tree in a pristine state and then remember to run it again after completing my navigations within it.

I received a different suggestion that seems to work well. I’m surprised I haven’t seen this mentioned in my earlier searches. It’s very simple:

When I find myself in a messy folder situation, the script does nearly exactly what it does now except that after the “select all” instead of Option-LeftArrow it does a Command-Option-LeftArrow.

This collapses more than I was getting but doesn’t go all the way either. It disposes of all of the “last folder states” for folders that display as opened under the current folder. (I’m not sure what the real name is for “last folder state” or where it might be stored but it is something the OS must be keeping track of somewhere.) The only shortcoming is that if a sub-folder is not displaying as open (triangle-down) but has child folders that have been previously expanded, those child folders remain expanded if/when they are selected.

Oh well, guess I can’t have everything.

RicBozz. I retested on a heavily-threaded folder on my Sonoma computer. I had several subfolders open several levels deep, and option-clicking on the disclosure triangle of the top-level folder collapsed every single subfolder. I don’t know why you are seeing different results.

I read the rest of your post multiple times, and it is not entirely clear to me what you want to accomplish. However, it sounds like you have found a solution, and that’s great news.

Try @robertfern’s suggestion. Option-right arrow (to expand) or option-left arrow (to contract) is what works in the Finder to expand or contract an entire folder structure.

I was incorrect in my description of the results of using Option-click to collapse. I’m using a Logitech K850 and frequently confuse the Apple “Option” key with the Microsoft “Ctrl” key. They’re labeled identically to the right of the space bar and perform as “Option” but are separate keys on the left side where they each do different things.

I think Option-click would work like the Cmd-Opt-LeftArrow if there was a way to Option-click on all the folders in the view pane. But there’s no way to “select all” for the Option-click. E.g., if I’ve selected “Documents” and the viewing pane has many expanded folders, I’d need to

  • navigate up to “Users”
  • expand the “Documents” folder if needed (because it would not necessarily be in an expanded state in that view)
  • Option-click
  • Navigate back to “Documents”

I could get the same result by:

  • Cmd-A
  • Cmd-Opt-LeftArrow

The latter being much easier to script.

RicBozz. Thanks for the explanation. I think I understand the issue now.

In the following circumstance, option-clicking on the disclosure triangle before Top Level Folder will close all subfolders. As an alternative, selecting Top Level Folder, followed by option-left arrow or by command-option-left arrow will close all subfolders.

In the following circumstance, the approach you suggest works, and it also works in the situation noted above. However, it would not work if all folders in the user’s home folder are shown in the view pane (because it would collapse folders other than in Top Level Folder.

So, the solution depends on what is displayed in the Finder view pane.

BTW, none of these keyboard shortcuts are documented by Apple, so this may or may not work on different versions of macOS (I’m on Sonoma).