How to access Apple Music album art in iTunes via

2020-07-26 02:31发布

I'm trying to create an application that sets the desktop background to the album artwork of the current song playing in iTunes, but have come across an issue where my script can't read the album art of an Apple Music song unless it has been added to the user's music collection or playlist.

Currently I am using this code to extract the album art.

-- get the raw bytes of the artwork into a var
tell application "iTunes" to tell artwork 1 of current track
    set srcBytes to raw data
    -- figure out the proper file extension
    if format is «class PNG » then
        set ext to ".png"
    else
        set ext to ".jpg"
    end if
end tell

-- get the filename to ~/Desktop/cover.ext
set fileName to (((path to desktop) as text) & "cover" & ext)
-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile

Does anybody know why this won't work when playing direct from Apple Music?
So I could add an Apple Music song to my Library or to a playlist, and my script can read its album art (the song doesn't have to be downloaded), but when just browsing the New or For You section, for example, and playing a song, my script won't read it and my application will crash.

Any suggestions would be greatly appreciated as I'd love for anyone who wants it to be able to use this application.

1条回答
三岁会撩人
2楼-- · 2020-07-26 03:17

Sorry, when streaming Apple Music the current track is a URL track. Although this inherits from track, the artwork element list is not exposed through iTunes scriptability so no cover art is available.

tell application "iTunes"
    tell current track
        if exists (every artwork) then
            tell artwork 1
                get properties
            end tell
        else
            tell me to display alert "No Cover Art found."
        end if
    end tell
end tell
查看更多
登录 后发表回答