Applescript programming

I wrote a nontrivial AppleScript to merge duplicates in my iTunes library.

It is easily the most gruesome thing I have ever written.

Some things of notes:

The language's smatterings of levity are, sadly, quickly overwhelmed by how dysfunction it is.

Here are actual snippets of actual code that I actually had to write:

set AppleScript's text item delimiters to d
set l to s's every text item
set i_track to (item i of l)'s track number
to trash_file(l)
  do shell script "mv " & ¬
    quoted form of POSIX path of (l as string) & ¬
    " " ¬
    & quoted form of POSIX path of (path to trash as string)
to cancel(m)
  display dialog m
  error number -128
set i to my split(f, ".")'s first item as number
if n's length ≠ o's length
if p's visible and ¬
   p's special kind = none and ¬
   not p's genius and ¬
   not p's smart and ¬
   index of last track of p < 1500
  set normal to normal & {p}
set old_plays to old's «class pPlC»
delete (some track of library playlist 1 whose database ID is old_dbid)
repeat with i from 1 to count of n
  set old to item i of o
  set new to item i of n
  set oldid to old's persistent ID

  repeat with j from 1 to count of normal
    set p to item j of normal
    set ts to tracks of p
    repeat with k from 1 to count of ts
      set t to item k of ts
      if t's persistent ID = oldid
        delete t
        duplicate new to p

This script should run instantly, but takes more than a minute when 10 tracks are selected. This is because of the above triply nested repeat loop, which loops over all the tracks I want to merge, all the playlists in my music library, and all the tracks in those playlists. I had to do it this way because I don't have any form of random access or dictionaries.

Even though the script is pretty simple, it still fails nondeterministically. I am unable to determine what could make such a simple script fail, and nondeterministically at that.

If hell has a programming language, it is definitely AppleScript.