I have a bullet proof scheme for toggling:
You save a script with the window id and you try to read that window id, and if the id is different, then you may add it to a list, and expand. If the window id is there, then you remove the window id, and retract.
(It is not totally bullet-proof since a window can be “reborn with the same id” if it then were in here, in the list over expanded window, the script will try to retract it for starters, then you’ll have to re-execute. The upside should however be greater.)
I’m sorry, couldn’t help it.
-- http://macscripter.net/viewtopic.php?pid=161306#p161306
-- 2013 © McUsr after idea by kel, and put in public domain, in sofar that you may not post this elsewhere.
global outlineStates
on run
tell my runner to run
end run
script runner
on run
local FinderIcon, scriptpath
set scriptpath to (path to temporary items folder from user domain as text) & "net.mcusr.treevals"
set FinderIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:Finder.app:Contents:Resources:Finder.icns")
if finderBusy() then
tell application "SystemUIServer"
activate
display dialog "Finder is too busy at the moment, please try again later..." with title "Tree" giving up after 1 buttons {"Ok"} default button 1 with icon FinderIcon
end tell
return
end if
set mustExpand to false
set L to {0, ""} -- initial values since we are filling a list
tell application id "MACS"
activate
tell first Finder window to set L to {its id, its target as text}
end tell
try
set outlineStates to load script alias scriptpath
on error
script treeWinList
property WindowIDList : {}
end script
set outlineStates to treeWinList
end try
-- if we find the id of the window, but different target,
-- then we'll change the target, but keep the id.
-- if we don't find the id, we'll keep it!
set ind to posInList((item 1 of L), outlineStates's WindowIDList)
if ind = 0 then
set end of outlineStates's WindowIDList to L
set mustExpand to true
else if item 2 of item ind of outlineStates's WindowIDList ≠item 2 of L then
set item 2 of item ind of outlineStates's WindowIDList to item 2 of L
set mustExpand to true
end if
if mustExpand then
my expand_window()
else
my revert_window()
if ind = 1 then
set my outlineStates's WindowIDList to rest of my outlineStates's WindowIDList
else if ind = length of my outlineStates's WindowIDList then
set outlineStates's WindowIDList to items 1 thru -2 of outlineStates's WindowIDList
else
set outlineStates's WindowIDList to items 1 thru (ind - 1) of outlineStates's WindowIDList & items (ind + 1) thru -1 of outlineStates's WindowIDList
end if
end if
store script outlineStates in scriptpath replacing yes
end run
on expand_window()
tell application id "MACS"
activate
tell front window
select
if current view is not list view then
set current view to list view
end if
select every folder
tell application id "sevs"
key down command
key down option
key down shift
key code 124
key up shift
key up option
key up command
end tell
select its first item
end tell
end tell
end expand_window
on revert_window()
tell application id "MACS"
activate
tell front window
select
if current view is not list view then
set current view to list view
end if
select every folder
tell application id "sevs"
key down command
key down option
key down shift
key code 123
key up shift
key up option
key up command
end tell
select its first item
end tell
end tell
end revert_window
on posInList(this_item, this_list)
script o
property L : this_list
end script
repeat with i from 1 to the count of this_list
if item 1 of item i of o's L is this_item then return i
end repeat
return 0
end posInList
on finderBusy()
tell application "System Events"
set a to unix id of application process "Finder"
end tell
set finderCpu to (do shell script "top -a -pid " & a & " -o cpu -l 2 2>&1 | grep " & a & " |tail -1 |tr -s ' ' |cut -d ' ' -f 3")
set decSep to text 2 of ((1 / 2) as text)
if decSep ≠"." then
set sepos to offset of "." in finderCpu
if sepos ≠0 then
set finderCpu to (text 1 thru (sepos - 1) of finderCpu & decSep & text (sepos + 1) thru -1 of finderCpu) as number
end if
end if
if finderCpu > 1 then
return true
else
return false
end if
end finderBusy
end script
Edit
I saved it as an app, then, after seeing that it worked as a toolbar-app, I went looking for an icon, as I didn’t have anything resembling a stylized tree, or a symbol for a tree-control, and I found an icon “newsvine_128.jpg” from the batch with free for noncomercial use Social Icons from here.
I opened the jpg file in preview, copied it, and just pasted it into the icon of the file-information dialog in Finder for the app.
You can also call that app from a service, to get a keyboard shortcut for it.