3

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.

1

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
  • Thanks for the tip on setting a properties variable - that works great! But unfortunately, the line: set thePath to POSIX path of (get location of current track) is returning an error: execution error: iTunes got an error: Can’t get location of current track. (-1728) – Charles Johnson Sep 3 '17 at 2:02
  • @Charles Johnson, The example code I posted works, as is, on my system. What version of OS X/macOS and iTunes are you running? – user3439894 Sep 3 '17 at 2:15
  • El Capitan with the latest version of iTunes (12.6.2.20), which should be basically the same. I'm actually running this through the Terminal with osascript - I'll try with the Script Editor. – Charles Johnson Sep 3 '17 at 2:30
  • Welp, the Script Editor returns the same error. Starting playing a track, ran the script, and it says: error "iTunes got an error: Can’t get location of current track." number -1728 from location of current track – Charles Johnson Sep 3 '17 at 2:33
  • Ah hah. Figured it out - I was playing a track that's in my library via Apple Music, so it doesn't actually exist in the file system. When I played a track that I actually have in the file system it worked fine. Thanks for the assistance, all! – Charles Johnson Sep 3 '17 at 2:35
0

This works for me on the latest version of Sierra

tell application "iTunes" to POSIX path of (get location of current track)

tell application "iTunes"
    set fileLocation to POSIX path of (get location of current track)
end tell

tell application "System Events"
    if application process "iTunes" exists then
        tell application "iTunes"
            set state to player state
            if state = playing then
                set fileLocation to POSIX path of (get location of current track)
                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
  • Thanks! But how do I use this to set a named variable like the others in the script above? – Charles Johnson Sep 3 '17 at 1:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .