Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

sort window by name

I just want to sort the front finder window by name, the code below does nothing. OS 10.10.3 any ideas?


tell application "Finder"

set sort column of list view options of front window to name column

delay 2

set sort column of list view options of front window to size column

end tell

Posted on Jul 8, 2015 6:05 PM

Reply
18 replies

Jul 9, 2015 2:41 AM in response to VikingOSX

I think my OS is corrputed. typing a replay or pasting my code below chops off the front half of the text.

Script editor is still NOT executing some commands.

tell application "Finder"

--open folder "2010" of the folder "Mechanicals" of the folder "Outdoor" of disk "Marketing"

tell front Finder window to set the current view to list view

tell list view options of front Finder window

set text size to 16

set uses relative dates to false-- DOES NOT WORK

set sort column to columnname column-- DOES NOT WORK

set sort direction of sort column to normal-- DOES NOT WORK

set sort direction of sort column to reversed-- DOES NOT WORK

end tell

end tell

Jul 9, 2015 3:28 AM in response to FCPeditor

The MacScripter responses at that link are five years old, and were probably used on Leopard or Snow Leopard. Both the Finder and AppleScript have changed since then, and though the code compiles and runs, I very much doubt that your OS is corrupted. I have 10.10.4 and the script does not produce visual results here either.


The scripting dictionary for the Finder sort verb is almost as cryptic as Apple can make it. Unless someone else in the community has a workable solution, or your searches return functional examples, I will not be able to help further.

Jul 9, 2015 5:27 PM in response to FCPeditor

FCPeditor wrote:


I just want to sort the front finder window by name, the code below does nothing. OS 10.10.3 any ideas?


The following solution is admittedly not the most elegant one, but it seems to do exactly what you are asking for:


tell application "Finder"

if not (existsFinder window 1) then return

activate

repeat until frontmost is true

end repeat

set current view of Finder window 1 to list view

repeat until current view of Finder window 1 is list view

end repeat

end tell


tell application "System Events" to tell process "Finder"

if existsmenu item "Show View Options" of menu 1 of menu bar item "View" of menu bar 1 then

keystroke "j" usingcommand down

end if

repeat until exists (window 1 whose subrole is "AXSystemFloatingWindow")

end repeat

tell (window 1 whose subrole is "AXSystemFloatingWindow")

clickpop up button "Sort By:"

clickmenu item "Name" of menu 1 of pop up button "Sort By:"

end tell

keystroke "j" usingcommand down

end tell

Jul 11, 2015 4:39 AM in response to FCPeditor

Hello


As far as I can tell under OS X 10.6.8, the following script works.



tell application "Finder" set sort_column to name column set sort_direction to reversed -- normal | reversed activate tell Finder window 1 set current view to list view tell its list view options set text size to 12 set uses relative dates to false repeat 2 times -- [1] set sort column to its column 1 whose name is not sort_column -- [2] set sort column to its column sort_column tell sort column set sort direction to sort_direction end tell end repeat (* [1] required to repeat - first to toggle triangle and second to sort items. [Finder's BUG] [2] required to switch sort column temporarily. [Finder's BUG] *) end tell end tell end tell




Don't know about recent OSes.


Regards,

H

Jul 11, 2015 9:19 AM in response to Hiroto

Hiroto wrote:


Hello Pierre L.,


I think we can safely conclude that Finder's scripting interface on list view options is partially broken under OS X 10.6.8 and totally broken under OS X 10.10.4. 😝


Well
 not totally. I think I've finally found the solution:


tell application "Finder"

if not (existsFinder window 1) then return

set current view of Finder window 1 to list view

set theTargetFolder to target of window 1

set theSortColumn to sort column of list view options of Finder window 1

if theSortColumn is not (columnidname column of list view options of Finder window 1) then

set sort column of list view options of Finder window 1 ÂŹ

to columnidname column of list view options of Finder window 1

set sort direction of sort column of list view options of Finder window 1 to normal

else

if sort direction of sort column of list view options of Finder window 1 is normal then

set sort direction of sort column of list view options of Finder window 1 to reversed

else

set sort direction of sort column of list view options of Finder window 1 to normal

end if

end if

closeFinder window 1

opentheTargetFolder

end tell

Jul 11, 2015 9:45 AM in response to FCPeditor

There was a potentially serious error (target of window 1 instead of target of Finder window 1) in my previous post.


The following version of the script should finally do exactly what you were asking for:


tell application "Finder"

if not (existsFinder window 1) then return

set current view of Finder window 1 to list view

set theTargetFolder to target of Finder window 1

set theSortColumn to sort column of list view options of Finder window 1

if theSortColumn is not (columnidname column of list view options of Finder window 1) then

set sort column of list view options of Finder window 1 ÂŹ

to columnidname column of list view options of Finder window 1

set sort direction of sort column of list view options of Finder window 1 to normal

else

if sort direction of sort column of list view options of Finder window 1 is normal then

set sort direction of sort column of list view options of Finder window 1 to reversed

else

set sort direction of sort column of list view options of Finder window 1 to normal

end if

end if

closeFinder window 1

opentheTargetFolder

end tell

Jul 11, 2015 11:41 AM in response to Pierre L.

Hello


Thanks for the idea to close the window to let poor Finder reflect the changes. 🙂


Here's a revised script that works under OS X 10.6.8.



tell application "Finder" tell Finder window 1 set current view to list view tell its list view options set text size to 12 set uses relative dates to false set sort column to its column name column tell sort column set sort direction to reversed -- normal | reversed end tell end tell set f to target -- [2] close -- [1] end tell open f -- [2] (* [1] required to close it to reflect changes. [Finder's BUG] [2] reopen it (optional) *) end tell




Regards,

H

Jul 12, 2015 3:51 AM in response to Pierre L.

Hello


You mean that my last script fails to set the sort direction to normal even if it is set to normal in script? If so, I have no idea how to correct it under 10.10. Or you simply mean that it does not implement any logic to switch the current sort direction? If so, it is my intention, for I don't think switching sort direction is requested by the original poster.


All the best,

Hiroto

sort window by name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.