Sorting alias list. Shortest code challenge.

So, I want to sort the files that are dropped on a droplet before processing them. I came up with the code below, which does what I want - it converts an alias list to strings so they can be sorted with a shell script and then the final list is converted back to an alias list.

Seems to me this could be a lot shorter. Any ideas how to get this down to a couple of lines?

set theList to {alias "Macintosh HD:Users:Matt:Desktop:Test3.txt", alias "Macintosh HD:Users:Matt:Desktop:test7.txt", alias "Macintosh"}
set theNewList to {}

set text item delimiters of AppleScript to ASCII character 10
set theList to "" & theList
set text item delimiters of AppleScript to ""

do shell script "echo " & quoted form of theList & " | /usr/bin/sort"
repeat with thisPath in (paragraphs of result)
	set theNewList to theNewList & (thisPath as alias)
end repeat

Model: PowerPC G5 Tower OS 10.4.8
AppleScript: XCode 2.5
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I’m not sure if it would be faster, but since they’re all in the same folder you could have the Finder sort the folder and then pick the items from the list of files in the folder.

Your sort sorts by path, but presumably you actually want to sort by item name. cwtnospam’s suggestion of the Finder sort works with lists of aliases too, though it returns lists of Finder references:

set theList to {alias "Macintosh HD:Users:Matt:Desktop:Test3.txt" , alias "Macintosh HD:Users:Matt:Desktop:test7.txt" , alias "Macintosh" }

tell application "Finder" to set theNewList to (sort theList by name)

repeat with thisRef in theNewList -- or simply coerce each reference as you process the item later.
	set thisRef's contents to thisRef as alias
end repeat

Thanks. Using the FInder made more sense. It sorts to a list of paths, so I put in one final loop to change it back to an alias list.

	tell application "Finder" to set these_items_Sorted to (sort these_items by name)
	set these_items to {}
	repeat with thisPath in these_items_Sorted
		set these_items to these_items & (thisPath as alias)
	end repeat

How about something like this?

tell application "Finder"
	set x to choose folder
	sort x by name
	set folderList to every item of folder x as alias list
end tell

Hi, cwtnospam.

Matt-Boy needed to sort the list parameter supplied to the ‘open’ handler in a droplet.

‘sort’ doesn’t affect the order of the items in a folder. It simply returns a sorted list. :slight_smile:

Oops! It seemed to work. Apparently the list is always by name, so unless you need it by some other criteria, you don’t have to do anything to it.

I haven’t checked recently, but it used to be that the returned order of items in a folder could change if you made changes to any of those items. The changed items would come at the end of the list. I can’t remember if it was closing the folder or opening it again that restored the name order.