Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Sort by columns in the Finder from keyboard Desktop
There have been some recent hints about controlling windows and selecting similar files using AppleScripts, so I thought I would share one that toggles the sort column (kind, date modified etc.) and sort order (a-z or z-a) for a Finder window.

I wanted to do this from the keyboard, so I wrote this script and substituted the appropriate column name as the title, and then assigned it a keyboard shortcut in Quicksilver. Now I can change sort columns and order from the keyboard.

tell application "Finder"
	
	-- could take out the activate if you want	
	activate
	
	-- In case the column you want to sort by is not visible, turn it on. This example uses the Size column.
	if visible of column id size column of list view options of window 1 is false then
		set visible of column id size column of list view options of window 1 to true
	end if
	
	-- in case that column you want is not currently the one that is selected for sorting, this changes it.
	if sort column of list view options of window 1 is not size column then
		set sort column of list view options of window 1 to column id size column of list view options of window 1
	end if
	-- toggles the sort direction, repeatedly invoking the script again has the effect of changing the sort order.
	if sort direction of column id size column of list view options of window 1 is normal then
		set sort direction of column id size column of list view options of window 1 to reversed
	else
		set sort direction of column id size column of list view options of window 1 to normal
	end if
end tell

-- switch the name of the column using any of the following names, name column, modification date column, size column, kind, column, label column, comment column to change what you will sort on.

Figuring out how to access columns and manipulate them is, well, non-intuitive to say the least, even with the UI Inspector. I hope this script helps others who want to add similar functionality to their Macs.

[kirkmc adds: As explained at the end of the script, change the column named in the script as necessary to act on the column you want. But you can save a script for each column you want to use.]
    •    
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[9,937 views]  

Sort by columns in the Finder from keyboard | 9 comments | Create New Account
Click here to return to the 'Sort by columns in the Finder from keyboard' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Keyboard shortcuts used to be built-in
Authored by: hargreae on Apr 28, '06 09:05:23AM

The OS X Finder used to have keyboard shortcuts for switching the sort-by column in List View windows: Control-Tab and Shift-Control-Tab. This worked in Panther, but was removed in Tiger. Join me in asking for it back via Apple's OS X Feedback page.



[ Reply to This | # ]
Keyboard shortcuts used to be built-in
Authored by: galaher on Apr 28, '06 12:03:07PM

Using this code, would it be possible to create a list of visible columns and have the script shift focus to the next in the list, essentially recreating the functionality removed in Tiger?



[ Reply to This | # ]
Applescript to switch between name, date & size columns in list view
Authored by: RacerOSX on May 06, '06 10:38:22PM

Here's an Applescript to switch columns in list view. Based on the previous one, but modified to switch between columns with default sort order. Added the sortColumn variable to shorten the length of the statements, but they're still pretty long. Do NOT shorten the if sortColumn is statements "column id name column of list view options of Finder window 1" parts to the "name column" version as in the original script. It doesn't work (at least on my system - OS X 10.4.5). Save it as a script and use QuicKeys or whatever to invoke it.


tell application "Finder"
	
	-- Bail if current window view is not List view
	if current view of window 1 is list view then
		
		-- Could take out the activate if you want (don't need it if you're already in the finder)
		activate
		
		-- In case the columns you want to sort by are not visible, turn them on. (add more here or modify as needed)
		if visible of column id name column of list view options of window 1 is false then
			set visible of column id name column of list view options of window 1 to true
		end if
		if visible of column id modification date column of list view options of window 1 is false then
			set visible of column id modification date column of list view options of window 1 to true
		end if
		if visible of column id size column of list view options of window 1 is false then
			set visible of column id size column of list view options of window 1 to true
		end if
		
		-- Switches between the name column (normal - a to z), modification date column (reversed - most recent first)
		-- and size column (reversed - largest first). this is how I normally use the columns, so modify as needed
		-- check for current sort column and change to next one
		set sortColumn to ""
		set sortColumn to sort column of list view options of window 1
		if sortColumn is column id name column of list view options of Finder window 1 then -- if name, set to date (reversed)
			set sort column of list view options of window 1 to column id modification date column of list view options of window 1
			set sort direction of column id modification date column of list view options of window 1 to reversed
		else
			if sortColumn is column id modification date column of list view options of Finder window 1 then -- if date, set to size (reversed)
				set sort column of list view options of window 1 to column id size column of list view options of window 1
				set sort direction of column id size column of list view options of window 1 to reversed
			else
				if sortColumn is column id size column of list view options of Finder window 1 then -- if size, set to name (normal)
					set sort column of list view options of window 1 to column id name column of list view options of window 1
					set sort direction of column id name column of list view options of window 1 to normal
				end if
			end if
		end if
	end if
end tell

-- 	switch the name of the column using any of the following names, name column, modification date column, size column,
--	kind column, label column, or comment column.

Note that this only switches one way (not like the Finder in Panther), where shift-control-tab reversed the order. However, the script is easily modified to switch the order by rearranging the if-then-else statement order. Save this as another script and you're good to go.
You can also change which columns or add/subtract columns by modifying the same if-then-else statement.



[ Reply to This | # ]
Sort by columns in the Finder Ascii??
Authored by: kissedsmiley on Apr 28, '06 09:08:32AM

hi! thank you for this! I haven't tried it yet; I wonder, is there an easy way to get it to sort by ascii, not by unicode? For example, if my file is 050401* I want it to occur before all 050402* no matter what is '*'. Under unicode, some combos, I think *=null vs *=letter vs *=number sort unexpectedly. thank you!! I am so excited about the potential for reverse sort, ie most recent file, when labeled yymmdd, at top!



[ Reply to This | # ]
Sort by columns in the Finder from keyboard
Authored by: pawliger on Apr 28, '06 10:41:31AM

Great hint, thanks.

Is there a way to toggle the left-hand panel of a Finder window, the one that shows Network, your hard drives, the Applications folder, etc, in this same way? Normally I double-click on the divider between that section and the window containing the file list or icons. It would be great to have this also tied to a keyboard schortcut that invokes a script...



[ Reply to This | # ]
Sort by columns in the Finder from keyboard
Authored by: dzurn on Apr 28, '06 11:47:20AM

No script needed!

To toggle on/off the sidebar from a Finder window, type Cmd-Opt-Control-T.

Then you can remove/restore the metal appearance with Cmd-Opt-T

Enjoy.




---
Madness takes its toll.
Please have exact change.



[ Reply to This | # ]
Sort by columns in the Finder from keyboard
Authored by: pawliger on Apr 29, '06 10:14:49AM

Rockin', thanks. Where did you discover that one, since it's not in a menu?



[ Reply to This | # ]
Finder Window Manager
Authored by: jonn8n on Apr 28, '06 12:17:26PM
For more Finder window control, check out Finder Window Manager (recently updated to a Universal Binary). Using the included "Watcher" utility, you can have the app automatically adjust windows to your specification every time a new window is opened. It also offers Window Sets and more.

Jon

[ Reply to This | # ]
Sort by columns in the Finder from keyboard
Authored by: dzg on Aug 10, '12 10:32:23AM

This no longer works in 10.7; does anyone know a way to toggle sort order with Applescript in Lion??



[ Reply to This | # ]