How Can I Open Subfolders In List View?

Hello MacScripters,

In a Finder window set to list view, some subfolder’s triangles are rotated to reveal their contents and some are not. How can I rotate the triangles of those I want to reveal?

Thanks,
ICTA

The only way I know is by using GUI Scripting, the actual code for which undoubtedly depends on the Mac OS version and possibly on individual Finder window configurations. This works for me in OS 10.6:

-- This script assumes that the front window in the Finder is a 'Finder window' in list view.

tell application "System Events"
	tell application process "Finder"
		set frontmost to true
		tell front window
			set subfolderNames to value of text field 1 of group 1 of (rows of outline 1 of scroll area -1 of splitter group 1 whose value of text field 1 of group 1 is not missing value)
			if (subfolderNames is {}) then
				-- No subfolders.
			else
				set theChoice to my chooseFromList(subfolderNames)
				-- Click the triangles in reverse order so that any sub-subfolders are handled first.
				repeat with subfolderName in (reverse of theChoice)
					click UI element 1 of group 1 of (first row of outline 1 of scroll area -1 of splitter group 1 whose value of text field 1 of group 1 is subfolderName)
				end repeat
			end if
		end tell
	end tell
end tell

on chooseFromList(thelist)
	tell application (path to frontmost application as text)
		set theChoice to (choose from list thelist with prompt "Toggle which disclosure triangles?" with multiple selections allowed)
	end tell
	if (theChoice is false) then error number -128
	
	return theChoice
end chooseFromList

My take at this.

tell application "Finder"
	activate
	tell front window
		set Item_List to {1, 3, 5} -- Have 5 or more folders to test
		-- Open drop downs
		repeat with This_Item in Item_List
			if kind of item This_Item = "Folder" then
				select item This_Item
				tell application "System Events"
					tell application process "Finder"
						key code 124 using option down -- Right Arrow
					end tell
				end tell
			end if
		end repeat
		delay 2
		-- Close drop downs
		repeat with This_Item in Item_List
			if kind of item This_Item = "Folder" then
				select item This_Item
				tell application "System Events"
					tell application process "Finder"
						key code 123 using option down -- Left Arrow
					end tell
				end tell
			end if
		end repeat
	end tell
end tell

Command down selecting then Option+RightArrow||LeftArrow works in the GUI

Hi Nigel,

It didn’t work for me in 10.4.11 but now that I know I must use GUI scripting, I will select the folders I want to reveal and use “Command + right arrow”.

Thank you!
ICTA

Thanks Mark67,
I didn’t see your post when I replied to Nigel.

My case is pretty simple, I don’t need to go into subfolders so I used this:


tell application "Finder"
	activate
	set the_folders to every folder of front window
	select the_folders
end tell

tell application "System Events"
	tell application process "Finder"
		key code 124 -- Right Arrow (works without any other keys)
	end tell
end tell


Hi, Darrell. Yeah. Good idea. The 10.4.11 version of my script would be very similar .

-- This script assumes that the Finder's front window is a Finder window in list view.

tell application "System Events"
	tell application process "Finder"
		set frontmost to true
		tell front window
			set subfolderNames to name of UI element -1 of group 1 of (rows of outline 1 of scroll area -1 of splitter group 1 whose name of UI elements contains "Folder")
			if (subfolderNames is {}) then
				-- No subfolders.
			else
				set theChoice to my chooseFromList(subfolderNames)
				-- Click the triangles in reverse order so that any sub-subfolders are handled first.
				repeat with subfolderName in (reverse of theChoice)
					click UI element 1 of group 1 of (first row of outline 1 of scroll area -1 of splitter group 1 whose name of UI element -1 of group 1 is subfolderName)
				end repeat
			end if
		end tell
	end tell
end tell

on chooseFromList(thelist)
	tell application (path to frontmost application as text)
		set theChoice to (choose from list thelist with prompt "Toggle which disclosure triangles?" with multiple selections allowed)
	end tell
	if (theChoice is false) then error number -128
	
	return theChoice
end chooseFromList

. but Mark67’s and your ‘key code’ approach works “as is” on both systems. And in one sense, it isn’t even GUI Scripting, because the ‘keystroke’ and ‘key code’ commands work even when GUI Scripting’s turned off. :slight_smile:

NG. That is good to Know. I had never tested just presumed it did.

Excellent. Thanks Nigel. Thanks Mark.

ICTA

Nigel,
How do I get ALL subfolders to open?

I’m trying to create a text file of all files and folders I have on my backup DVD’s. Doing this allows me to spotlight search for files that no longer reside on my hard drives. I then know which DVD to insert to get to the file I’m looking for.

Can you help?
Dan

Hi, Dan. Welcome to MacScripter.

Do you really need the folders to be open (as seen in list view), or do you just want a text file?

Hi Nigel.
That’s a good question. Will the text file you mention include the names of all folders, subfolders and files on the DVD? If so, I guess the text file would be perfect. I just save it in my documents folder and when searching by keyword in spotlight, it’ll tell me where the file is. (DVD name, folder, subfolder)

Opening all those folders and subfolders in list view just to create a text file (select all, copy then paste and match style in text edit) is time consuming, even with the script you were kind enough to supply.

On a different note, let me say that I’ve wanted to get into Applescript for years. This may be the door I’ve been looking for. Thank you for your help. I really do appreciate it.

Dan

Model: MacMini
AppleScript: 2.3 (118)
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

First of all, moving files (by removing them from source) to another data medium is no backup.
Second of all, a hard disk is
¢ cheaper
¢ faster
¢ bigger capacity
¢ easier to handle
¢ more reliable
¢ Spotlight compatible

and you don’t need that kind of script :wink:

Stefan,
I don’t care to have 15 years of Graphic Design files on spinning HD’s sucking up electricity that I access infrequently, if that’s okay with you. I’m not that important that I need to leave such a footprint on this planet. FYI, I have two (2) sets of cd/DVD backup disks. But you’re right, I don’t need a script…I would LIKE to have a script.

Dan

In that case, it’s outside the subject of this thread. I’ve started another thread with a possible solution here.

By the way, would you mind not top-posting (ie. putting replies above the associated quotes) in these fora? Cheers.

Thank you, Nigel. I’ll redirect.
And no, I don’t mind not top posting. Sorry.

Best regards,
Dan