Alphabetizing a list

I’m trying to add a subroutine to a script of mine that will alphabetize a list of words. The list is defined as a variable defined from the clipboard.

I thought about using the shell command “fmt -1 myfilename | sort” but that would require a lot of extra scripting to get the clipboard into a text file. This wouldn’t work properly anyway because it took each word separately and did not preserve the line breaks.

At the moment what I do is paste the list of words into appleworks and sort the spreadsheet alphabetically.

If anyone can help me out here, it’d much greatly appreciated. Thanks.

iceman,
have you looked into pbpaste and pbcopy? They are the command line tools for copying and pasting to the clipboard. Pipelining the commands with sort will put the sorted variable on the clipboard:

do shell script "pbpaste | sort | pbcopy"
set sortedVariable to the clipboard

Look up pbpaste and pbcopy on macosxhints for more examples.

Here’s a sort routine you can use inside your script if you’ve already got a list:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks a lot for the replies, they’ve been helpful. This is not a difficult script I’m trying to write, but I just can’t figure this one out. Usually after a couple of hours I get it, but not this one.

Here is the script, it doesn’t need much modification.

I added this in as alternative code to make a list out of the string, I took it out though.

Essentially all I want to do is for the script to get this string from the clipboard:

Steve Hackett - Voyage Of The Acolyte
Mike Oldfield - Ommadawn
Camel - Harbor Of Tears
Rush - Hemispheres
Yes - Relayer
King Crimson - In The Wake Of Poseidon

And turn it into this:

camel - harbor of tears
king crimson - in the wake of poseidon
mike oldfield - ommadawn
rush - hemispheres
steve hackett - voyage of the acolyte
yes - relayer

The formatting of the text is also removed with the «class ktxt» line.

If anyone reading this is interested in a script to help tag mp3s by looking up discographies let me know.

This script will take the contents of the clipboard, convert it to lowercase, sort it, and put it back on the clipboard:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thank you so much for that script, you’re a very good scripter!

If you need a favour just send me an email.

Dear LORD! why is there no alphabetizing command? :evil:

There is no alphabetizing command because applescript isn’t meant to be a substitute for php or pearl or java or… It originated as a language to tie applications together. If you want a list alphabetized, tell BBEdit or Word to read the list, then tell BBEdit or Word to alphabetize the list. Applescript shouldn’t have to duplicate a function available in dozens of scriptable programs.

I appreciate that perspective. But, with the advent of applescript studio, many of us have started using applescript as a programming language for small apps which require a short development period.

In any case, I addressed the problem with the unix sort function.

Thanks,
James :twisted:

Um… er… OK, but let’s look at it this way: Why is AppleScript the only modern scripting language on planet Earth that doesn’t have a sort command, or a find-the-index-of-a-value-in-a-list command, or etc…?

The “origins” of AppleScript aren’t significant when compared to the wants and needs of the people who use the language today. If I’m writing a simple little script to add some bit of functionality to Mail or Safari or whatever, it feels pretty hackish to launch BBEdit, open a new window, insert text, sort the text, and then quit BBEdit.

Incidentally, there are many available ways to perform sorting in AppleScript, many of which predate the availability of “do shell script”.

http://applemods.macscripter.net/getMod.php?script_ID=43
http://applemods.macscripter.net/getMod.php?script_ID=33

qsort(), in 2-parts:
http://listserv.dartmouth.edu/scripts/wa.exe?A2=ind0401&L=macscrpt&D=1&H=0&I=-3&O=D&T=0&P=43527
http://listserv.dartmouth.edu/scripts/wa.exe?A2=ind0401&L=macscrpt&D=1&H=0&I=-3&O=D&T=0&P=43418

In addition to the above links, there are 2 sorting handlers to be found in the “class” scripts of Smile, and over at AppleMods.sourceforge.net, there is a List library with several excellent sorting handlers as well.