sort window 1 by name

this used to be easy and work 15 years ago, what happened. I just need to sort a window by name or modification date. Here is what I have and it does not work.

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

I don’t know for 15 years. As is now:


tell application "Finder"
	activate -- This need only for testing purposes (visualization the result)
	tell front Finder window
		set aFolder to target -- remember the folder, need only for testing purposes
		if current view ≠ list view then set its current view to list view -- making sure
		tell (its list view options) to set sort column to column id name column
	end tell
	-- The following 3 code lines need only for testing purposes (visualization the result)
	close front Finder window -- close not updated folder's window
	open aFolder -- open updated folder's window
	display dialog "Now your front Finder window IS SORTED BY NAME"
end tell

NOTE:

  1. instead of column id name column you can use column name column
  2. you can omit “visualization” code lines
  3. I guess, you can omit “making sure” code line too:

tell application "Finder" to tell front Finder window to tell (its list view options)
	set sort column to column name column
end tell

The list view is sorted, but with this 2nd snippet you will see the changes only when you will close and reopen the given window (as list view manually if need).

It is your choice, what you need - sorting foreground (my 1st code) or sorting background (2nd code)

Using the syntax similar to your original script, I can write my 2nd script this way:


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