Need simple script to change sort method in dialog box

Could someone please tell me how to make a script to change the sort method (by name, by date) in an open dialog box. For example, change it from sort by date to sort by name. In a way previous version of OS X there was a key-command to toggle between the different sorts but that is long gone and I still miss, I hate always have to mouse over and click.

Thanks,
~peter

Hi,

you can do this only with GUI scripting, here an example for Safari


tell application "System Events"
	tell process "Safari"
		click UI element "Date Modified" of group 1 of outline 1 of scroll area 2 of splitter group 1 of group 1 of window "Open"
	end tell
end tell

Thanks so much for your help. The script worked great for Safari. Is there a way to change it to a universal script for any Open dialog, or any Save As dialog, so that I don’t need to make a separate script for each app? I tried changing the script for Word by just changing the app name to “Word” but it didn’t work. Any idea why?

Also, could you maybe be so kind as to give me a script for change the sort, say “by date” for a window in the finder?

Thanks again,
~peter

As open and save dialogs are customizable, the UI elements can be different in each application.
But for MS Word the syntax is the same


tell application "System Events"
	tell process "Microsoft Word"
		click UI element "Name" of group 1 of outline 1 of scroll area 2 of splitter group 1 of group 2 of window "Open"
	end tell
end tell

Note: “Name” and “Open” is in the appropriate language of a localized version of MS Word

The Finder is directly scriptable. GUI scripting is not needed


tell application "Finder"
	activate
	set current view of Finder window 1 to list view
	tell list view options of Finder window 1
		set sort column to column id modification date column
	end tell
end tell

Here I added some instructions to take care of localizations.

Even with the correct strings, several applications fail.



--set theApp to "iDVD" (* OK *)
--set theApp to "Keynote" (* OK *)
--set theApp to "Numbers" (* OK *)
--set theApp to "Pages" (* OK *)
--set theApp to "Preview" (* OK *)
set theApp to "Safari" (* OK *)

(* French with problem *)
--set theApp to "Adobe Reader"
--set theApp to "EazyDraw"
--set theApp to "iTunes"

(* English Only with problem *)
--set theApp to "Firefox"
--set theApp to "Linotype FontExplorer X"
--set theApp to "NeoOffice"
--set theApp to "PaintBrush"
--set theApp to "Skitch"

if theApp is in "EasyDraw" then
	set OPEN_loc to my getLocale("EasyDraw", "Open", "StringTable_2006_A")
	if OPEN_loc is "Open" then
		set DATEMODIFIED_loc to "Date modified"
	else
		set DATEMODIFIED_loc to my getLocaleFromTable("Finder", "N221", "LocalizableCore")
	end if
	
else if theApp is in {"Firefox", "Linotype FontExplorer X", "NeoOffice", "PaintBrush", "Skitch"} then
	set DATEMODIFIED_loc to "Date modified"
	set OPEN_loc to "Open"
	
else
	set p2Fres to (path to application "Finder" as text) & "Contents:Resources"
	set DATEMODIFIED_loc to my getLocaleFromTable("Finder", "N221", "LocalizableCore")
	set OPEN_loc to my getLocale("Finder", "N151")
end if

tell application theApp to activate
tell application "System Events" to tell process theApp
	keystroke "o" using {command down}
	click UI element DATEMODIFIED_loc of group 1 of outline 1 of scroll area 2 of splitter group 1 of group 1 of window OPEN_loc
end tell

--=====

on getLocale(a, x)
	tell application a to return localized string x
end getLocale
--=====

on getLocaleFromTable(a, x, t)
	tell application a to return localized string x from table t
end getLocaleFromTable

--=====

Yvan KOENIG (from FRANCE vendredi 27 mars 2009 21:48:38)

Hi Yvan,

you did a lot of work, but I think this could do the same.
The date modification column has always index 2 and the script checks for the proper window


set theApp to "Safari" (* OK *)
activate application theApp
tell application "System Events" to tell process theApp
	keystroke "o" using {command down}
	set OPEN_window to (1st window whose outlines of scroll area 2 of splitter group 1 of group 1 is not {})
	click UI element 2 of group 1 of outline 1 of scroll area 2 of splitter group 1 of group 1 of OPEN_window
end tell

maybe there is still a more reliable way to identify the open window

Thanks again. Can’t get the sort in the Open dialog to open but sorting in the Finder works fine. Only thing is I can’t seem to get the keyboard shortcuts to work. I name the script to have a shortcut like this “finder_sort_date\mosD.scpt”.

Thanks,
~peter

To assign shortcuts to scripts you need a third party tool like FastScripts, QuicKeys, Butler, QuickSilver and a few more