Finder 'completely expanded'

Hi,

I wonder why Apple hasn’t worked on this:

Has anyone made scripts to replace the NOT AVAILABLE YET completely expanded. I guess you need to use ui scripting.

Model: MBP
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Hi,

I thought I had a way with selection, but it doesn’t work. No, it might work. Select every folder, then open. etc.

?

Hello.

I can’t make the short cuts work with UI Scripting in a finder window for some odd reason.

(I did try to address application process Finder first.)

Edit
This works for in Snow Leopard! I had to add an “activate” statement, and it was all good. :slight_smile:

Removed unecessary delay statements.

Added shell script which performs on the current directory: makes a new finder window in tree-view
(expanded list view).

Changed the shell script opendir so only the front Finder window, gets to be over the Terminal window.


” http://macscripter.net/edit.php?id=161266
” 2013 © McUsr and put in public domain, in sofar that  you may not post this elsewhere.
tell application "Finder"
    activate
    tell its front window
        select every folder of it
        set current view of it to list view
        tell application "System Events"
            key down command
            key down option
            key down shift
            key code 124
            key up shift
            key up option
            key up command
        end tell
    end tell
end tell

This is occasionally useful from the commandline, and the tree-view is even interactive! :slight_smile:
This shell script must be saved somewhere in your path, and issued chmod u+x to it.
I have called it opendir.

[code]#!/bin/bash

http://macscripter.net/edit.php?id=161266

2013 © McUsr and put in public domain, in sofar that you may not post this elsewhere.

if test $PWD = $HOME ; then exit 0 ; fi
open -g .
osascript <<EOF 2>&1 >/dev/null
tell application “System Events”
set nm to name of first application process whose visible is true and frontmost is true
end tell
tell application “Finder”
activate
tell its front window
select every folder of it
set current view of it to list view
tell application “System Events”
key down command
key down option
key down shift
key code 124
key up shift
key up option
key up command
end tell
end tell
end tell
tell application nm to activate
EOF
open -a “Finder”
osascript <<EOF 2>&1 >/dev/null
tell application “Finder”
set toolbar visible of its first window to false
end tell
EOF[/code]

Hello.

The shell script in post #3 is revised for the last time, it is almost exactly how I want it.

When done with it, you just close it, and it should open exactly like it was the last time, with regards to size and position.

Hi McUsrII,

I just got up and checking out your scripts. They look good.

I was thinking about using ‘select’ and ‘selection’ and the arrow keys, but am a bit rusty with ui scripting. Also, was thinking about how to reclose all the folders. Eager to check out that shell script also.

Editted: I just found out that update OS 10.8.3 is out. Alright! Maybe they have something with AookeScript. :slight_smile:

Editted: in my keyboard shortcuts list, I can’t find:
shift + option + command + right (or left) arrow
The list must be incomplete. Thanks again.

Editted: found what looks like a complete set in the Developer Library:

https://developer.apple.com/library/mac/#documentation/IDEs/Conceptual/xcode_help-command_shortcuts/MenuCommands/MenuCommands014.html

Fold Methods & Functions
⌥⇧⌘â‡
Option-Shift-Command-Left Arrow
Unfold Methods & Functions
⌥⇧⌘⇢
Option-Shift-Command-Right Arrow

Thanks a lot,

Hello Kel.

I don’t know where I picked it up. I think some of the keyboard shortcuts for outline view also should work in Finder.
That is: it would be nice to stand on a folder and type command opt shift 9 and create a new subfolder.

Here is the counter part to the expand tree script. (When I use those, I’ll use them from Quicksilver. But I am also thinking of making a couple of services, so you can use the mouse to achieve the same, through the Services button on the Finder toolbar.

Let me know if you are interested, because it is more fun to make stuff that other needs.


” http://macscripter.net/edit.php?id=161266
” 2013 © McUsr and put in public domain, in sofar that  you may not post this elsewhere.
tell application "Finder"
	activate
	tell its front window
		select every folder of it
		set current view of it to list view
		tell application "System Events"
			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

Hi McUsrII,

Both scripts work great. I accidentally opened up my home folder. Guess I need to change directory first in Terminal.

It would be easier to go to the Finder window’s toolbar. I don’t know how to do that yet. :slight_smile:

Thanks again,

Hello.

The easiest way, is to:

  1. start Automator
  2. choose make new service
  3. Choose that the service should take marked files from finder as input.
  4. select an Applescript proceure action from the Automator Library.
  5. Save quit
  6. Go to System preferences, Keyboard, find the service, and select keyboard shortcut.

You should now be able to find the two services under the gear icon, under the services menu, or directly from the services menu when you right click in the Finder window.

(I hope this makes sense.)

I’ll be back later, with a clause in the shell script, so you can’t give it $HOME as parameter. You may then add similiar clauses should you need to.

Edit
I think you can just save the scripts as applications and drag them onto the toolbar, if that is how you prefer it, then you can also type the name of the apps in Spotlight, and execute them from there.

By the way, the size of the icons on the toolbars in Mac apps are customizable in size, you hold down command and click the bevel in the upper right corner of the toolbar.

Updated the shell script in post #3 to just exit if the current directory is the home folder.

I can get the scripts to show up in the Finder services menu, but I can’t get it to show up with Control click in a Finder window. If I control click on a folder in the Finder window, then the workflow shows up in the contextual menu.

I need to experiment more with Automator and think about what is the best way to do this. We could just add a keyboard shortcut to the service to take place of the select all (command A) and Option + Shift + Command + right/left arrow. I thought that I had read about a way to add an Applescript to the toolbar, but all the information I got is starting to get jumbled together. :o

Editted: got it now. Had to save the script as an app. Then added it to the right side of the Finder window toolbar. I might change the app to be background only, so it doesn’t show in the dock when run.

Editted: I just saw your edit. Great minds think alike. :slight_smile:

Thanks,

Hello kel.

Saving the apps as faceless background apps, is certainly something I am going to try. :slight_smile:

A couple of things worth mentioning:

The Automator thing worked for me, when I made a service, not workflow.

It is services that pop up, and that you can assign keyboard shortcuts to in the keyboard preferences pane.

Hi McUsrII,

It’s working great.

Saved the two apps and added them to the toolbar. I’m testing it out for a while before changing the info.plist for uielement something. Thinking about making the two apps one toggle app and changing the icon.

Thanks a lot for all your ideas and expertise.

gl,

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. :slight_smile:


-- 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. :slight_smile:

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.

Hi McUsrII,

The script is working good. I couldn’t find news vine, but I took a snapshot of a windows list view, completely expanded, and used that for an icon.

BTW, I managed to click on one of the disclosure triangles in the window. With my computer setup, something like this:


tell application "Finder"
	activate
	set folder_ref to target of front window
end tell

tell application "System Events"
	
	tell process "Finder"
		tell window "RunTester"
			tell splitter group 1
				tell scroll area 2
					tell outline 1
						tell row 1
							tell UI element 1
								
								--count (every row)
								--every UI element
								--role description of every UI element
								tell UI element 2
									click
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
	
end tell

Thanks for everything,

Hello kel.

I should thank you equally, -great minds think alike. :slight_smile:

As for icons, I think a three control icon from Interface Builder or the cocoa framework in XCode may be the nicest. :slight_smile:

I have thought of using UI Script, for collapsing everything but the curreent sub-tree with selected items, but I came to the conclusion that it was to much ado for little really. I am very happy to try your script! :slight_smile:

Hello.

Added a missing select its first item statement in the expand_window handler in post #12.

Hello.

I have updated the script in post #12 for at least feeling faster, though I think I have made the applet slightly faster by using the run script trick to get a new script environment, I also address applications by their application id for the hope that it leeds to less lookup. The real bottleneck is however selecting the folders, and ther isn’t much I can do about that (have tried), so I put up a dialog for telling you that the applet is indeed working. The next edition will use Stefan’s progress bar app.

Do yourself a favor and make it faceless by entering <key>LSUIElement</key> <integer>1</integer
into its plist.file.

Edit
Implemented my own very cheap progressbar for now.

Hello.

It doesn’t help much to ignore application responses in the current script, it blocks and postpones what is coming after. So the progress bar is replaced by a dialog again, telling it is working.

Edit

I have realized, that since I am sending keystrokes to select stuff, that a progress bar is probably a bad idea, at least to an applescript dialog, since that is the window that receives the keys. The alternative, is to use growl. I wonder how you remove a growl notification. :slight_smile:

Hi McUsrII,

I copied the latest version of your script. It’s a lot faster than the simpler one and works very well.

Thanks a lot,

Hello kel.

I am glad that the run script trick works on Mountain Lion still.

I tested the applet with a folder with 150 subfolders, so I didn’t notice much of the improvement, but it seemed to work a little faster.

There may be two reasons for this. One is the find command that finds folders very fast, and at the same time drags necessary resources into memory, concerning the disk.

Then there is the run script trick:

I want to add that this trick also works on scripts run, from the scripts menu, and may improve some scripts, preferably those that talks with applications with approximatly something in the range 20-40%.

Well, I did think out how to be lazy, and code the next version of tree yesterday, so that it keeps track of the window state, and different targets, so it doesn’t stumble, should you use that finder window as a “browser window”.

So, there are coming a new version later, hopefully this day, I have figured out everything. Even that you’ll have to delete the old cache file for running it, as the new cache consists of two values. (winID and Target folder as text).

Hi McUsrII,

Yes, that run script trick really speeds things up. I haven’t seen the filling the list with dummy items in the script yet. I have to look at it tomorrow morning.

I didn’t think that the window had caches, but coming to think of it if you use the back and forward buttons to browse. One thing when I use the back button to get the collapsed window, then go forward to the expanded, the list is no longer expanded. Amazing how you found the cache. :cool:

Thanks again,