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 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.  It'd be 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.  So now 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 first level down.  I'd like to have it iterate down through all the sub-folders - or at least more than 1 level.  From what I've read, it should probably stop the iterations at some point to prevent erroring out or hanging.  In practice, I'd be content to go no more than say 5 sub-folders down under each folder of the currently selected parent.

 

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.  Ironically, the multitude of approaches only adds to my confusion Plus, after a certain age, recursion becomes incomprehensible and I'm well past that point.

iMac 27″, macOS 14.2

Posted on Feb 13, 2024 8:21 AM

Reply
8 replies

Feb 16, 2024 10:12 AM in response to Luis Sequeira1



Luis Sequeira1 wrote:

What is wrong with Command-Option-RightArrow and Command-Option-LeftArrow?

They should recursively twirl open and close everything.

There's really no recursion in doing that and it is basically what I'm doing now. I select everything and collapse it and get a comprehensible view. But that does not carry down to all the sub-folders and their child folders within the currently selected "top" folder.

Feb 13, 2024 11:07 AM in response to Tired_Rabbit

Suppose we have a folder on the Desktop named Test. Within Test, we have this folder hierarchy in List view:


And, the following AppleScript:


use scripting additions

tell application "Finder"
	
	tell target of Finder window 1
		-- show list of main folders in list view
		reveal (every folder of it)
		tell application "System Events"
			tell application process "Finder"
				set frontmost to true
				key code 123 using command down
			end tell
		end tell
	end tell
end tell


and the result of the script running on the open Finder WIndow:




Feb 16, 2024 5:47 AM in response to VikingOSX

Thanks for that suggestion - it gives me another way of collapsing at the first level. But it doesn't do anything more than my current script. Yes, I get the collapsed window nice & tidy. But if I click on any of the folders that are presented, I'll wind up in an un-collapsed view of that folder (if it was left un-collapsed the last time it was navigated into). I.e., if I click on folder "A" after running your script or mine, it's likely that I'll see "A" with a bunch of down-arrows under which are displayed file names of files in the sub-folders of "A".


What I'd like to get if I clicked on "A" is to see the names of files within "A" and the child folder names of "A" without any expansions. To do this, the initial script would have had to have navigated the sub-folders within "A" and collapsed them (as well as any child folders within those sub-folders).


I will return in a bit and post some images of what I'm trying to get. All the levels make it difficult to describe with only words.

Feb 16, 2024 9:41 AM in response to Tired_Rabbit

Here are some screenshots that help better explain what I'm trying to accomplish:


When I navigate to Documents what I see is:



Running the script that I currently use, I get what I'd like to see:


And if I expand one of those folders, "_Scripting_Apps_", everything still looks good because I get only the files and folders in that sub-folder:


But that's just a coincidence because the last view of that sub-folder had no expansions. If instead I choose a different sub-folder to expand, I see:


And that's a mess. It also shows how I last viewed that folder. But my last view had a couple expansions. I'd like to have that folder displayed as if it had never been viewed. I.e., no down-arrows would be present.

Feb 17, 2024 3:47 AM in response to Tired_Rabbit

Tired_Rabbit wrote:




Luis Sequeira1 wrote:

What is wrong with Command-Option-RightArrow and Command-Option-LeftArrow?

They should recursively twirl open and close everything.
There's really no recursion in doing that and it is basically what I'm doing now. I select everything and collapse it and get a comprehensible view. But that does not carry down to all the sub-folders and their child folders within the currently selected "top" folder.


I'm thinking that you may not have actually tried it...


Command-Option-RightArrow opens recursively through all the folders and subfolders


Here are three folders, each with a number of enclosed subfolders:



Pressing Command-Option-RightArrow twirls them open recursively through all the levels of subfolders:



And Command-Option-LeftArrow does the opposite.


It does not matter what the open or closed state of intermediate folders.


For example, if A2 and C3 are twirled closed, then Command-Option-Left followed by Command-Option-Right will get it all open again.


Feb 17, 2024 5:25 AM in response to Luis Sequeira1

Luis Sequeira1 wrote:
I'm thinking that you may not have actually tried it...
Command-Option-RightArrow opens recursively through all the folders and subfolders
...

Well, the results when doing this are interesting. I navigate with 2 panes, left and right. If I select "Documents" in the left pane then I see the mess of files & folders in the right pane. Cmd-Opt-Left and Cmd-Opt-Right don't do anything from there. If I start at a higher level in the left pane and select "Documents" by double-clicking on it in the right pane, I see my mess in the right pane and those button combos still do nothing. Alternatively, if I just expand the "Documents" in the right pane instead of double-clicking, I see the expanded mess and the buttons still do nothing.


The buttons work only on what is highlighted (selected) in the right pane. But your solution works great for what I am trying to do which is collapsing everything in a single button press that runs an AppleScript. All I need to do before pressing the buttons that you suggested is to cmd-A (select all). I.e., I can navigate in the left pane and doing a cmd-A selects everything in the right pane without me needing to do an additional click to get there. With everything selected, the cmd-opt combos work great!


Thanks so much for pointing this out and then nudging me to experiment with it. I don't even need to add to my original and simple script. All I have to do is change the "key code 123 using control down" to include both keys (cmd & opt) instead of just the one. Wonderful. I sure hope this continues to work in the next major release - you never know...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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

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