Is there any way to add 'sort by ...' to the open folder action

hello,
I wrote the award winning macro below which simply opens a folder.
Is there a way to add an action which would sort the folder contents by name?
thanks very much !

In theory, you should be able to use this script in an Execute AppleScript action after opening the folder:

tell application "Finder" to set the sort column of list view options of window 1 to the name column

But in practice, the Finder doesn’t seem to want to reflect the change in the sort column (at least on 10.12.4 on my iMac) unless I manually open another folder in the same window and then go back to the old folder (and even that doesn’t work when I try it with my Downloads folder). Still, feel free to try that script until someone who actually knows what they’re doing with AppleScript shows up with a better option; maybe you’ll have better luck with it than I did.

(and not to hijack the thread, but as an AppleScript beginner, if anyone who actually knows what they’re doing with AppleScript wouldn’t mind telling me why this script doesn’t seem to actually get the Finder to reflect the change in sort column, I’d be all ears)

1 Like

Once the folder has been opened, you could have KM use the action for Select Or Show A Menu Item to (in the Finder) select View > Arrange By > Name.

1 Like

I'm afraid this won't work. The wording is admittedly confusing on macOS's end, but arranging is different from sorting; it affects how items in a Finder window are grouped, not how they're listed. As an example, here's my current desktop folder sorted by Date Modified and arranged by Name:

And the opposite, sorted by Name and arranged by Date Modified:

Honestly, I didn't know about this difference either until I started looking into the OP's question. Live and learn!

1 Like

I believe your script would need to set source direction of the desired column to normal or reverse, and then use use Finder's sort verb.

1 Like

Something like this? Because I’m afraid I couldn’t get this to work either:

tell application "Finder"
	tell list view options of window 1
		set sort direction of sort column to normal
		set sort column to name column
	end tell
end tell
1 Like

I have tried many things, but it think there is a bug somewhere.

The only way I found to make it work is with a stupid workaround:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
  tell front Finder window
    tell its list view options
      set sort column to name column
      set sort direction of sort column to normal
    end tell
    # Updating doesn’t  help
    --update items
    # Workaround: set window target to something different and then back to the previous target
    set oldTarget to target
    set target to path to home folder
    set target to oldTarget
    set the current view to list view
  end tell
end tell
2 Likes

This worked! Thanks, Tom! Glad to hear I wasn’t the only one experiencing problems with what should have been a simple script.

1 Like

@gglick @Tom @korm @NaOH
thanks very much for all your suggestions and the solution

1 Like