Setting a variable to a property

The following works as expected:

tell application "Finder"
	set sortedFiles to sort selection by name
end tell

Why doesn’t the following work?

tell application "Finder"
	set sortOrder to name
	set sortedFiles to sort selection by sortOrder
end tell

Also, I’d like to specify the sort order at the beginnng of the script outside the Finder tell block, as in the following. Is this possible?

set sortOrder to name

tell application "Finder"
	set sortedFiles to sort selection by sortOrder
end tell

I spent significant time googling this and researching the ALG but could find nothing. Thanks for the help.

Hi peavine.

‘name’ is a term common to lots of things. When you use it in a script, it’s evaluated in context. So in your second script, sortOrder gets set to “Finder”, in the third it’s set to the name of the script. The only way I can see to do what you want is to set sortOrder to a reference to ‘name’ in a Finder context. This sets the variable to the Finder’s enum for ‘name’ instead of an actual name:

tell application "Finder" to set sortOrder to a reference to name

tell application "Finder"
	set sortedFiles to sort selection by sortOrder
end tell

Hi, Peavine.

Your question is answered, but as I see, you want to provide to user choose sort order method from list. To do that you should map sort properties to its names (string representation, as choose from list works with simple AppleScript values):


set aChoice to choose from list {"Name", "Kind", "Creation Date", "Modification Date", "Locked", "Name Extension", "Owner", "Group", "Size"} with prompt "Choose Method to Sort By"

tell application "Finder"
	if aChoice is "Name" then
		sort selection by name
	else if aChoice is "Kind" then
		sort selection by kind
	else if aChoice is "Creation Date" then
		sort selection by creation date
	else if aChoice is "Modification Date" then
		sort selection by modification date
	else if aChoice is "Locked" then
		sort selection by locked
	else if aChoice is "Name Extension" then
		sort selection by name extension
	else if aChoice is "Owner" then
		sort selection by owner
	else if aChoice is "Group" then
		sort selection by group
	else
		sort selection by size
	end if
end tell

NOTE: exists many other properties to sort items of selection. See ITEM properties in the Finder’s dictionary.

Thanks Nigel and KniazidisR for the responses. The operator, “a reference to”, is just what I was looking for.