Sort lists in AppleScript using the Unix sort command

May 17, '04 09:13:00AM

Contributed by: erickaterman

To sort a list in AppleScript, try using text item delimiters and the shell command sort, like this:

set the_list to {"c", "b", "e", "a", "d"}
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed
set list_string to (the_list as string)
set new_string to do shell script "echo " & quoted form of list_string & " | sort -f"
set new_list to (paragraphs of new_string)
set AppleScript's text item delimiters to old_delims
return new_list
[robg adds:This is obviously just a sample code snippet to show what can be done by using the shell's sort command within an AppleScript, but it's a pretty good demo of the Terminal/AppleScript integration.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040513173003941