Folder Window Flustration...

I confess that I can’t stand the sidebar in Finder folder windows.

That said, I’ve been trying to write a script that will create a new folder on the desktop, turn off the sidebar, size it uniformly (for a 1024 x 768 display) and have it retain the size (bounds) I’ve given it.

(The following script is a work in progress… and doesn’t actually resolve some of the errors encountered with renaming, but at the moment, I’m frustrated and far less concerned with that than…)

I find that manually clicking on the window’s title bar or ‘grow area’, then closing the window, will retain the window bounds specified in the script… but I can’t find a way to script the same result.

I have UI Browser (on trial), but I can’t find a UI element to click that replicates what I can do manually.

Any help?

Thanks.

Peter B.



tell application "Finder"
	activate
	
	display dialog "Name The Folder..." & return & return & "	And Place It:" default answer "???" buttons {"Cancel", "In This Folder", "On The Desktop"} default button 3 with title "New Folder"
	copy the result as list to {text_returned, button_pressed}
	
	set folder_name to text_returned
	
	if button_pressed is "In This Folder" then
		
		if exists window 1 then
			tell application "System Events"
				tell process "Finder"
					keystroke "N" using (command down & shift down)
				end tell
			end tell
			
			try
				set name of folder "untitled folder" of window 1 to folder_name
			on error
				display dialog ¬
					"A folder with this name already exists in this location..." & return & return & "Please choose another folder name." & return & return default answer text_returned buttons {"Cancel", "Rename"} default button 2 with title "Rename Folder"
			end try
			
			tell application "System Events"
				tell process "Finder"
					keystroke (ASCII character 3)
				end tell
			end tell
			
		else
			
			tell application "Finder"
				activate
				set new_folder to make new folder at desktop
				open folder "untitled folder"
				try
					set name of folder "untitled folder" to folder_name
				on error
					display dialog ¬
						"A folder with this name already exists in this location..." & return & return & "Please choose another folder name." & return & return default answer text_returned buttons {"Cancel", "Rename"} default button 2 with title "Rename Folder"
				end try
			end tell
			
			tell application "System Events"
				tell process "Finder"
					click button 1 of window 1
				end tell
			end tell
			
			delay 0.4
			
			tell application "Finder"
				activate
				set bounds of window 1 to {194, 84, 836, 468}
				close window 1
			end tell
			
		end if
		
	else
		
		tell application "Finder"
			activate
			set new_folder to make new folder at desktop
			open folder "untitled folder"
			try
				set name of folder "untitled folder" to folder_name
			on error
				display dialog ¬
					"A folder with this name already exists in this location..." & return & return & "Please choose another folder name." & return & return default answer text_returned buttons {"Cancel", "Rename"} default button 2 with title "Rename Folder"
			end try
		end tell
		
		tell application "System Events"
			tell process "Finder"
				click button 1 of window 1
			end tell
		end tell
		
		delay 0.4
		
		tell application "Finder"
			activate
			set bounds of window 1 to {194, 84, 836, 468}
			close window 1
		end tell
		
	end if
	
end tell

see how this works.
I wrote it out quickly so I could go to beeed…
But it seems to work ok.

global new_folder
tell application "Finder"
	activate
	
	display dialog "Name The Folder..." & return & return & "	And Place It:" default answer "???" buttons {"Cancel", "In This Folder", "On The Desktop"} default button 3 with title "New Folder"
	copy the result as list to {text_returned, button_pressed}
	set folder_name to text_returned
	if button_pressed is "In This Folder" then
		repeat until not (exists folder text_returned of window 1)
			
			display dialog ¬
				"A folder with this name already exists in this location..." & return & return & "Please choose another folder name." & return & return default answer text_returned buttons {"Cancel", "Rename"} default button 2 with title "Rename Folder"
			copy the result as list to {text_returned}
		end repeat
		set folder_name to text_returned
		my the_window(text_returned, folder_name)
	else
		repeat until not (exists folder text_returned of desktop)
			
			display dialog ¬
				"A folder with this name already exists in this location..." & return & return & "Please choose another folder name." & return & return default answer text_returned buttons {"Cancel", "Rename"} default button 2 with title "Rename Folder"
			copy the result as list to {text_returned}
			set folder_name to text_returned
		end repeat
		my the_dektop(text_returned, folder_name)
	end if
end tell
on the_dektop(text_returned, folder_name)
	
	tell application "Finder"
		activate
		
		set new_folder to make new folder at desktop with properties {name:text_returned}
		return new_folder
		
	end tell
	
end the_dektop

on the_window(text_returned, folder_name)
	
	if exists window 1 then
		
		tell application "Finder"
			set toolbar visible of window 1 to false
			set sidebar width of window 1 to 0
			set new_folder to make new folder at window 1 with properties {name:text_returned}
			return new_folder
		end tell
		
		
	end if
end the_window

tell application "Finder"
	activate
	open new_folder
	set toolbar visible of window 1 to false
	set sidebar width of window 1 to 0
	set bounds of window 1 to {194, 84, 836, 468}
	close window 1
end tell




Thanks for the effort Mark, but I get the same result… when the folder is reopened manually, the window position set in the script hasn’t been retained.

I gave it another shot this morning and still haven’t got it.

Oh, well.

Peter B.


ok got it sort of
in the part of the script

tell application "Finder"
   activate
   open new_folder
   set toolbar visible of window 1 to false
   set sidebar width of window 1 to 0
   set bounds of window 1 to {194, 84, 836, 468}
   close window 1
end tell

The bounds is the last command.
But for some reason it will not get written to the finder prefs for that window.
But putting it first gets it included when the other settings write to the pref.
One problem with it running before the other two settings is the bounds get set first then the window shrinks.
Thus throwing your bonds out of whack.
So you have to compensate by setting a bound that will shrink into the correct position.
Yours system may differ slightly
The bound below will set my window to your {194, 84, 836, 468} when shrunk

tell application "Finder"
	activate
	open new_folder
	set bounds of window 1 to {1, 84, 841, 527} --> shrinks to {194, 84, 836, 468} when toolbar and sidebar are set
	set toolbar visible of window 1 to false
	set sidebar width of window 1 to 0
	
	
	close window 1
	
end tell

Mark:

Thanks again… I’m getting pretty consistent results from your revised snippet…

I found a different kludge that gives reliable result from my own script (flawed as it may be). Though I used your ‘toolbar visible’ line, I still call on System Events to click the zoom box twice. The window then holds the scripted position when closed.

Six of one, half a dozen of the other.

Peter B.


tell application "Finder"
	activate
	set new_folder to make new folder at desktop
	open folder "untitled folder"
	set toolbar visible of window 1 to false
	set bounds of window 1 to {194, 84, 836, 468}
	
	delay 0.2
	
	tell application "System Events"
		tell process "Finder"
			click button 2 of window 1
			click button 2 of window 1
		end tell
	end tell
	
	close window 1
	
end tell


A variation on Mark’s workround that seems to work is to flip the toolbar visibility again before closing the window. This avoids having to second-guess the bounds:

tell application "Finder"
	activate
	open new_folder
	set toolbar visible of window 1 to false
	set sidebar width of window 1 to 0
	set bounds of window 1 to {194, 84, 836, 468}
	set toolbar visible of window 1 to true
	set toolbar visible of window 1 to false
	close window 1
end tell

Or, in code-saving mode:

tell application "Finder"
	activate
	tell (make new Finder window to new_folder)
		set {toolbar visible, sidebar width, bounds, toolbar visible, toolbar visible} to {false, 0, {194, 84, 836, 468}, true, false}
		close
	end tell
end tell

Strange bug, though. :confused:

That works.

Now if only we can sort out the issue of once set.
If you do open the folder from a window that has toolbar and sidebar
the setting of the window are imposed on the folder. I suspect this is another bug.

Nigel Garvey wrote:

Strange bug, though.

mark hunte wrote:

I suspect this is another bug.

A bit off topic, but somewhat relevant to this thread:

On at least one other Mac-oriented forum there is an almost a perpetual thread of how flawed and buggy the OS X Finder is in general… or in design.

My main gripe (other than today’s post topic) is that folders sometimes ‘revert to form’, showing the sidebar/toolbar when they have previously been turned off manually… for no apparent reason other than that they weren’t double clicked directly… sometimes when they’re opened by scripts or applications.

I haven’t done much experimenting with folder ‘View Options’ manually… and when confronted with the choice of “This window only” or “All windows” I invariably choose the former, scared to death what effect the latter - within or without the particular folder - might be. Anyone know what that ‘hierarchy’ involves?

Thanks.

Peter B.


I find the result of this script rather odd too…

Even knowing the Desktop is only a representation of the desktop folder, why would this be the desired effect?

PB



tell application "Finder"
	activate
	make new folder at desktop
	select folder "untitled folder"
end tell