Does anyone have an applescript to turn the arrows down (expand) of every folder and sub folders of the frontmost finder window?
Any info would be greatly appreciated,
CarbonQuark
Does anyone have an applescript to turn the arrows down (expand) of every folder and sub folders of the frontmost finder window?
Any info would be greatly appreciated,
CarbonQuark
From Finder’s dictionary:
I’m afraid the easy way is not available yet (there’s no code behind that dictionary entry). (Maybe some ugly GUI scripting could do it.)
Something like this should work once “completely expanded” is available:
tell application "Finder"
tell front Finder window to set completely expanded to true
end tell
Bruce,
Thanks for the info!
CarbonQuark
Hey guys
Not wishing to poke my nose in to much on this one.
you guys have a lot more applescript knowledge,
but the mac keyboard shortcut for opening up all your triangles and subfolders etc…
is alt/option - right arrow.
guess you guys new that,
yes it would be ugly Gui scripting, but where not talking Quazimodo ugly surely
tell application "Finder"
activate
select every item of front window
tell application "System Events" to keystroke (ASCII character 29) using {option down}
end tell
Pidge1’s method is quite nice IF you have the window in question in List View (not Icon or Column). ASCII character 28 will close them all for you as well. Now if there was only a way to tell which way they were, we could toggle them with a little script…
It’s not pretty, but it works…
-- toggle for expanded folders in list view
-- most useful if assigned to a mouse key
on run
try
tell application "Finder" to activate
-- Selects list view and expands folders
tell application "System Events"
keystroke "2" using command down
tell application "System Events"
keystroke "a" using command down
end tell
tell application "Finder"
set selection_one to selection
set mycount1 to count selection_one
end tell
tell application "System Events"
keystroke (ASCII character 28) using option down
keystroke "a" using command down
tell application "Finder" to set selection_two to selection
set mycount2 to count selection_two
end tell
end tell
-- collapses folders if already expanded
if mycount1 is equal to mycount2 then
tell application "Finder" to activate
tell application "System Events"
keystroke "a" using command down
keystroke (ASCII character 29) using option down
end tell
else
quit
end if
on error -- do nothing
end try
end run
Not too bad for a newbie, eh?
BTW, I don’t recommend trying this on say, your entire hard drive. It can take a LONG time to expand all those folders…
Thanks pidge1 & Torajima,
You guys rock. That does the trick.
CarbonQuark
Thank Adam too… I took his comment “Now if there was only a way to tell which way they were, we could toggle them with a little script” to be a challenge!
Very effective workaround, pidge1 - and an inventive refinement, Torajima (for any scripting level).
Just one small point. The quit statement keeps trying to cause my Script Editor to quit. It isn’t really necessary, anyway - and your script works just fine here without it. (If you wanted to, you could also remove the ‘on error’ without doing any damage.)
I had a look at this discussion a little earlier, and came up with a routine that does something very similar. So - just to throw in a little variation:
tell application "Finder"
activate
tell front Finder window
if not (exists) or (count containers) is 0 then return beep (* or invoke some kind of dialog/error *)
set l to current view is not list view
select (* in case some other type of window is frontmost *)
end tell
end tell
tell application "System Events" to tell window 1 of application process "Finder"
set t to it
tell splitter group 1 to if exists then set t to it
if l then key code 19 using command down
key code 0 using command down
key code 124 - (value of UI element 1 of group 1 of (first row of outline 1 of t's ¬
scroll area -1 where value of UI element 1 of group 1 > -1)) using option down
end tell
tell application "Finder"
reveal front Finder window's item 1 (* forces a scroll-up after contracting a long list *)
select {} (* deselect *)
end tell
I found a problem with my original script… it didn’t always expand folders nested within other folders. It seemed to open more and more levels each time it was run (first run, expand 1st level of folders; second run, collapse folders; third run, expand 2nd level of folders, etc.). Now, this isn’t necessarily a bad thing, but it’s not what I originally intended. I also simplified some of the keystroke commands.
Here is the repaired script, it will continue running until every nested folder in the finder window is expanded:
-- toggle for expanded folders in list view
-- most useful if assigned to a mouse key
on run
try
set maxnumber to 1000 -- maximum number of items in expanded view (crash protection - change as needed)
-- note that comparisons are made after the current level of nested folders is open, so number of items may greatly exceed the "maximum"
tell application "Finder" to activate
-- Selects list view and counts number of files and folders
tell application "System Events"
keystroke "2" using command down
tell application "System Events"
keystroke "a" using command down
end tell
tell application "Finder"
set selection_one to selection
set mycount1 to count selection_one
end tell
-- Does initial expansion if number of files and folders is less than maxnumber
if mycount1 is less than (maxnumber + 1) then
tell application "System Events"
keystroke "a" using command down
keystroke (key code 124)
delay 0.5 -- added delay to make script more reliable (increase or decrease as needed)
keystroke "a" using command down
tell application "Finder" to set selection_two to selection
set mycount2 to count selection_two
end tell
-- collapses folders if already expanded
if mycount1 is equal to mycount2 then
tell application "Finder" to activate
tell application "System Events"
keystroke "a" using command down
keystroke (key code 123)
end tell
else
-- continues to expand nested folders until all are expanded or number of items in folder is greater than maxnumber
set newcount to 0
set oldcount to 1
repeat until newcount = oldcount or newcount is greater than maxnumber
tell application "Finder" to activate
tell application "System Events"
keystroke "a" using command down
keystroke (key code 124)
tell application "Finder"
set selection_three to selection
set oldcount to newcount
set newcount to count selection_three
end tell
end tell
end repeat
end if
else
-- collapses folders if file count greater than maxnumber
tell application "Finder" to activate
delay 0.5 -- added delay to make script more reliable (increase or decrease as needed)
tell application "System Events"
keystroke "a" using command down
delay 0.2 -- ditto
keystroke (key code 123)
end tell
end if
-- deselect all (Thanks Adam!)
delay 0.1
tell application "Finder" to set selection to {}
end tell
on error -- do nothing
end try
end run
Okay, this is my final edit (for tonight at least!)…
I added Adam’s deselection bit, added some delays to make the script run better when saved as an app, and the biggie… stopped the finder crashes by having the script stop when there are too many items in the finder window.
I gotta say, I like writing scripts better than debugging them!
I’m impressed, Torajima. I had to remove the “quit” from the collapse section or it tries to quit the editor while the script is logically still running (10.4.6). In the Script Editor it tells you that, but it hangs Script Debugger.
I made some minor changes, the principal one being to deselect the files and folders after expansion and collapse.
[EDIT: I was doing this while you were changing yours - this doesn’t incorporate those changes.]
-- toggle for expanded folders in list view
-- most useful if assigned to a mouse key
tell application "Finder" to activate
-- Selects list view and expands folders
tell application "System Events" to keystroke "2a" using command down
tell application "Finder" to set mycount1 to count (get selection)
tell application "System Events"
keystroke (ASCII character 28) using option down
keystroke "a" using command down
end tell
tell application "Finder"
set mycount2 to count (get selection)
delay 0.1
set selection to {}
end tell
-- collapses folders if already expanded
if mycount1 is equal to mycount2 then
tell application "Finder" to activate
tell application "System Events"
keystroke "a" using command down
keystroke (ASCII character 29) using option down
end tell
delay 0.1
tell application "Finder" to set selection to {}
end if
Thanks Adam. I’ve tweaked the script quite a bit over the past few hours.
I originally had a line to deselect all files and folders by keystroking the ESC key, but it didn’t work without a huge delay. Your solution works much better…