Getting the file path of a currently playing iTunes track with Applescript

applescriptitunes

I've got an Applescript program that works really well for getting most of the info of a currently playing track into a series of named variables:

tell application "System Events"
    if application process "iTunes" exists then
        tell application "iTunes"
            set state to player state
            if state = playing
                tell current track
                    set theArtist to artist
                    set theTitle to name
                    set theAlbum to album
                    set theTime to time
                    set theGrouping to grouping
                    set theGenre to genre
                    set theKind to kind
                    set theYear to year
                    set theComments to comment
                    set playCount to played count
                    set theTrack to track number
                    set numTracks to track count
                    set theDisc to disc number
                    set numDiscs to disc count
                    set bitRate to bit rate
                end tell
            else
                set state to "none"
            end if
        end tell
    else
        set state to "none"
    end if
end tell

But I'm completely baffled on how to get actual file path of the current track. The Applescript library for iTunes has a "file track" entry that has a "location" item, but I haven't been able to figure out how to use it in this context. Any help would be greatly appreciated.

Best Answer

You don't need System Events, iTunes can tell if it's running or not.

Also, set the properties of current track to a variable and then get the info from the variable. The reason for this is you make only one lower level call (properties of current track) and then the assigning of the other variables gets the info from a variable containing all the info, and not making 15 separate lower level calls to get the info, This should be faster and more efficient code.

tell application "iTunes"
    if running then
        set state to player state
        if state = playing then
            set trackProperties to (properties of current track)
            set theArtist to artist in trackProperties
            set theTitle to name in trackProperties
            set theAlbum to album in trackProperties
            set theTime to time of trackProperties
            set theGrouping to grouping in trackProperties
            set theGenre to genre in trackProperties
            set theKind to kind in trackProperties
            set theYear to year of trackProperties
            set theComments to comment in trackProperties
            set playCount to played count in trackProperties
            set theTrack to track number in trackProperties
            set numTracks to track count in trackProperties
            set theDisc to disc number in trackProperties
            set numDiscs to disc count in trackProperties
            set bitRate to bit rate in trackProperties
            try
                set thePath to POSIX path of (get location of current track)
            on error
                set thePath to "Not playing from local library."
            end try
        else
            set state to "none"
        end if
    else
        set state to "none"
    end if
end tell