Why does this Applescript for talking to iTunes fail with timeout for some customers? This script works okay for me and most of my customers, but for a few customers it times out with error -1712; I've no idea why. I've tried increasing timeout, but it makes no difference.

The file /tmp/itunes_model.txt doesn't get created so it doesn't seem to be doing anything.

How can I debug/fix this?

with timeout of 1200 seconds
tell application "iTunes"

    if (count of every file track of library playlist 1) is equal to 0 then
        set thePath to (POSIX file "/tmp/itunes_model.txt")
        set fileref to open for access (thePath) with write permission
        set eof fileref to 0
        close access fileref
        return
    end if

    tell every file track of library playlist 1
        script performancekludge
            property tracknames : its name
            property locs : its location
            property persistids : its persistent ID
        end script
    end tell
end tell

set thePath to (POSIX file "/tmp/itunes_model.txt")
set fileref to open for access (thePath) with write permission
set eof fileref to 0

tell performancekludge
    repeat with i from 1 to length of its tracknames
        try
            set nextline to item i of its tracknames ¬
                &amp; "::" &amp; POSIX path of item i of its locs ¬
                &amp; "::" &amp; item i of its persistids
            write nextline &amp; linefeed as «class utf8» to fileref
        end try
    end repeat
end tell
close access fileref
end timeout


Added a try-catch around the error as requested and reran; the first time it was run it actually worked which is interesting, but the second time it failed again gleaning no more useful information.
It should something like this:

tell application "iTunes"
    with timeout of 1200 seconds
    ...
    end timeout
end tell


Your timeout should be inside the tell block instead before/above/outside of it.

It seems as if you might be setting the timeout for the script itself instead of iTunes which has a default timeout of 120 seconds (all applications have a default timeout of 2 minutes).

This could be failing for a number of reasons besides normal timeouts, such as if the person just upgraded iTunes, and your script opens it for the first time after it was recently installed, it won't be able to execute normal iTunes functions until after closing out of the splash screen that says "Welcome to iTunes version X - Check out all of the new features!, etc."

Also, it's been awhile since I've worked with AppleScript, so I don't recall whether or not if you tell a system application to do something if it automatically opens or not.  If not, it's possible, you may need to Tell SystemEvents to open application "ITunes" (or whatever the syntax is) -- or you could always use the do shellscript command to open the application, in which case the actual shell command would be open /Applications/iTunes.app/ -- which you can verify in Terminal.

The error could also be if the iTunes app needs GUI focus for these operations to take place and the application automatically loses focus while the script is being run when another application is opened or the user clicks away or some scenario of that sort, which your script will need to be able to account for.  In cases such as this, you may want to tell SystemEvents to activate application "iTunes" before any important actions take place.  This will cause the iTunes window to regain focus.

It may actually be this same SystemEvents command (you probably need to check/correct my syntax) that initially opens the application if it is not already open at the time the script is run.