AppleScript Text Sort via Unix

Sometimes you need to sort a list before presenting it to the user via “choose from list.” This is particularly important when working with Helix RADE, as the list of icons is sorted by selection order, not by name.

While various methods have been put forward, none rival the speed of the Unix ‘sort’ command. The following code is modified from a hint found at http://hints.macworld.com/article.php?story=20040513173003941.

on sortItems(itemList)
      -- modded from http://hints.macworld.com/article.php?story=20040513173003941
   local listString, newString, newList
   set oldDelims to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed
   set listString to (itemList as string)
   set newString to do shell script "echo " & ¬
      quoted form of listString & " | sort -g" -- add -r for reverse
   set newList to (paragraphs of newString)
   set AppleScript's text item delimiters to oldDelims
   return newList
end sortItems
google_pluslinkedingoogle_pluslinkedinby feather

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.