Download entire iTunes purchase history as CSV file (MacOS High Sierra)

The following works on currently latest macOS I'm using (High Sierra 10.13.5 Beta) and latest iTunes (12.7.4.76).

I've written an AppleScript that is using UI Scripting and compiles a list of purchased songs on iTunes. The script will open a new TextEdit document and fill in the songs in a comma delimited format and will contain the sequential number, song name, artist, album and song duration. The output will be similar to this:

"Nr","Song Name","Artist","Album","Duration"
"1","Badinerie","Lucero Tena","Lección de castañuelas","1:03"
"2","El colibrí","Lucero Tena","Lección de castañuelas","1:07"
"3","El relicario","Lucero Tena","Lección de castañuelas","1:36"

itunes

Here is the script:

tell application "iTunes" to activate
tell application "TextEdit"
    activate
    make new document at the front with properties {name:"iTunes purchased.txt"}
    delay 1
    tell application "System Events" to click menu item "Make Plain Text" of menu 1 of menu bar item "Format" of menu bar 1 of application process "TextEdit"
end tell
tell application "iTunes" to activate
tell application "System Events"
    keystroke "1" using {command down}
    set w to window "iTunes" of application process "iTunes"
    click radio button "Store" of radio group 1 of w
    delay 2
    set webarea to UI element 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of w
    click static text 1 of UI element 1 of group 6 of list 2 of webarea
    delay 2
    click radio button "All" of tab group 1 of group 2 of webarea
    delay 3
    click radio button "Songs" of tab group 1 of group 5 of webarea
    delay 5
    set allRows to UI elements of table 1 of webarea
    set entireContent to quote & "Nr" & quote & "," & quote & "Song Name" & quote & "," & quote & "Artist" & quote & "," & quote & "Album" & quote & "," & quote & "Duration" & quote & return
    tell application "TextEdit" to activate
    repeat with aRow in allRows
        if class of aRow is row then
            set gr to UI element 1 of group 1 of UI element 2 of aRow
            if class of gr is group then
                set songNum to value of static text 1 of group 1 of UI element 1 of aRow
                set songName to value of static text 1 of group 2 of UI element 2 of aRow
                set songArtist to value of static text 1 of group 1 of UI element 1 of UI element 3 of aRow
                set album to UI element 1 of UI element 4 of aRow
                set songAlbum to ""
                if (count of UI elements of album) is greater than 0 then
                    set songAlbum to value of static text 1 of group 1 of album
                end if
                set songDuration to ""
                set dur to UI element 5 of aRow
                if (count of UI elements of dur) is greater than 0 then
                    set songDuration to value of static text 1 of group 1 of dur
                end if
                set aLine to quote & songNum & quote & "," & quote & songName & quote & "," & quote & songArtist & quote & "," & quote & songAlbum & quote & "," & quote & songDuration & quote
                set entireContent to entireContent & aLine & return
                tell application "TextEdit" to set text of front document to entireContent
            end if
        end if
    end repeat
end tell

The process is not fast, on my computer it takes about 1 second per song so be prepared to wait a while. Also, if you start getting some "out of index" errors try, while the script is running, scrolling the iTunes song list in the background to make sure the entire list content is loaded before being accessed by AppleScript.