Download entire iTunes purchase history as CSV file

Solution 1:

You asked, "is there any way to script the UI?" Yes, if you are on a Mac. You also asked, "Has anyone actually succeeded in doing this?" Sort of. My history goes back to 2005, and the iTunes Store session kept timing out, so I had to run it it batches, but the following script does work. Be aware that it runs very slowly; simply retrieving the class of an object from iTunes can take almost a second. I would welcome any performance enhancements anyone can suggest.

This worked on OS X Yosemite 10.10.5 with iTunes 12.3.2.35, and on OS X Mavericks 10.9.5 with iTunes 12.3.2.35, on or around February 28th, 2016. Any changes Apple makes to the iTunes Store account interface will likely break this script.

In iTunes, go to Store > View Account, log in, scroll to Purchase History and click See All, then, when the screen showing Batch 1 out of N is visible, run the following script in Script Editor:

tell application "System Events"
    set dateString to do shell script "date \"+%Y-%m-%d_%H.%M.%S\""
    log dateString
    set target_file to ((path to documents folder) as text) & dateString & "_iTunes_Purchase_History.txt"
    set myOutput to ""

    set webArea to UI element "loading iTunes store" of splitter group 1 of window "iTunes" of application process "iTunes"
    set batchText to value of first UI element of webArea whose value starts with "Viewing Batch"
    log batchText
    set AppleScript's text item delimiters to {" "}
    set num to last text item in batchText
    log num
    set currentNum to text item 3 in batchText
    log currentNum

    repeat num times
        set els to UI elements of webArea
        set ready to false
        set open_target_file to open for access file target_file with write permission
        --repeat with el in els
        repeat with el in els
            set cl to class of el
            if cl is button then
                set myOutput to myOutput & "\n"
            end if
            if cl is static text then
                set val to value of el
                if val starts with "Copyright" then set ready to false
                if ready then set myOutput to myOutput & val & "\t"
                if val is "Total Price" then set ready to true
            end if
        end repeat
        log myOutput
        write myOutput to open_target_file starting at eof
        set myOutput to ""
        close access open_target_file

        click button "Next" of webArea
        repeat
            delay 2
            set batchText to value of first UI element of webArea whose value starts with "Viewing Batch"
            set tempNum to text item 3 in batchText
            if tempNum is not currentNum then
                set currentNum to tempNum
                log currentNum
                exit repeat
            end if
        end repeat
        delay 2
    end repeat
end tell

This produces tab-delimited text, not CSV as the OP requested, but most spreadsheet applications will import it. Gift purchases seem to mess up the formatting, so it may need some manual curation.

I relied on information at http://n8henrie.com/2013/03/a-strategy-for-ui-scripting-in-applescript/ to learn how to do GUI scripting.