0
votes

Can this applescript be converted to use the iTunes Library framework instead of using the standard iTunes applescript .

My current script reads the users iTunes library and creates a file consisting of TrackName, TrackLocation and PersistentId, it works reliably but can be slow when user has a large iTunes library

tell application "iTunes"
    with timeout of 2400 seconds
        if (count of every file track of library playlist 1) is equal to 0 then
            set thePath to (POSIX file "/tmp/songkong_itunes_model.new")
            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 timeout
end tell

set thePath to (POSIX file "/tmp/songkong_itunes_model.new")
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 ¬
                & "::" & POSIX path of item i of its locs ¬
                & "::" & item i of its persistids
            write nextline & linefeed as «class utf8» to fileref
        end try
    end repeat
end tell
close access fileref

The big different is this new framework doesn't require iTunes to actually be running and I expect it should be considerably faster. However the sparse instructions only discuss ObjectiveC I'm not clear how I would write a version in applescript (or even better in Java)

1

1 Answers

0
votes

This is the AppleScriptObjc equivalent using the iTunesLibrary framework. It does not write the text line by line but creates an array, inserts the new line characters then writes the entire file at once. It uses also a better error handling when writing the file.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "iTunesLibrary"

set {library, theError} to current application's ITLibrary's libraryWithAPIVersion:"1.0" |error|:(reference)
if theError is not missing value then
    display dialog theError's localizedDescription() as text buttons {"Cancel"} default button 1
end if
set tracks to library's allMediaItems()

set thePath to "/tmp/songkong_itunes_model.new"

set theLines to {}
repeat with aTrack in tracks
    try
        set nextline to (aTrack's title as text) ¬
            & "::" & (aTrack's location's |path| as text) ¬
            & "::" & (aTrack's persistentID()'s intValue())
        set end of theLines to nextline

    end try
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set theText to theLines as text
set text item delimiters to TID
try
    set fileref to open for access thePath with write permission
    set eof fileref to 0
    write theText as «class utf8» to fileref
    close access fileref
on error
    try
        close access thePath
    end try
end try