Toggle all disclosure triangles of nested folders in list view

I’m writing a script for which I need to expand all folder disclosure triangles in a given Finder window in list view.
I’ve worked out how to expand and when done, collapse all nested folders in a window:

tell application "Finder"
	-- Open the relevant start folder.
	set baseFolder to choose folder
	open folder baseFolder
	-- Set the list view in case the window opens in another view.
	tell window 1 to set {current view} to {list view}
	
	-- Expand all disclosure triangles in window 1
	select every item of front window
	tell application "System Events"
		keystroke (ASCII character 29) using {option down}
	end tell
	
	-- deselect all
	delay 0.2
	set selection to {}
end tell

That works well expanding all nested folders to the deepest level. No stopping it at the level I need it.
So, I’m stuck now trying to work out how to limit the expansion to a specific subfolder level, say 3 down from window 1.
Please, any help how to do this will be greatly appreciated

For anybody interested, this is the reverse code:

tell application "Finder"
	activate
	-- Collapse all disclosure triangles in window 1
	select every item of front window
	tell application "System Events"
		keystroke (ASCII character 28) using {option down}
	end tell
	
	-- deselect all
	delay 0.2
	set selection to {}
end tell

Hi flex20.

I don’t know the answer to your specific query, but it occurs to me that since ‘ASCII character’ has been deprecated for some years now, you ought to be using ‘character id’ instead. The key codes would be better stlll here, since these relate directly to the keys rather than to the associated characters:

tell application "System Events"
	key code 124 using {option down} -- for keystroke (character id 29) using {option down}
	
	key code 123 using {option down} -- for keystroke (character id 28) using {option down}
end tell

Edit: This seems to work, but it’s only been tested on a specially created hierarchy containing just folders nested five deep:

tell application "Finder"
	-- Open the relevant start folder.
	set baseFolder to choose folder
	open baseFolder
	-- Set the list view in case the window opens in another view.
	tell window 1 to set current view to list view
end tell

repeat 3 times
	-- Expand all disclosure triangles in window 1 down to three levels.
	tell application "System Events"
		keystroke "a" using {command down}
		delay 0.2 -- This may need adjusting.
		key code 124
		delay 0.2 -- Ditto.
	end tell
end repeat

-- deselect all
delay 0.2
tell application "Finder" to set selection to {}

Hi Nigel,
Thank you for advice. I didn’t know that. I’m about to correct my posted code accordingly.
Cheers,
Chris

Hi Nigel,
I’ve seen your followup after my second post. Great idea using repeat to control the expansion. I run it quickly on my test folders and it works. I need to do some more testing tomorrow (it is 1:45 am here in Sydney). My real need can be either level related of specific folder position related. I think I can use a repeat until the specific folder name was processed. I will post the result.
Many thanks again,
Chris