如何使用 AppleScript 获取 Apple 音乐作品

How to get Apple music artwork using AppleScript

我有一个 AppleScript,它可以检索在 Apple Music 上播放的当前曲目的几个属性,但我无法检索插图

if application "Music" is running then
    tell application "Music"
        if player state is playing or player state is paused then
            set currentTrack to current track

            return {get player state} & {get artist of currentTrack} & {get name of currentTrack} & {get album of currentTrack} & {get kind of currentTrack} & {get duration of currentTrack} & {player position} & {get genre of current track} & {get id of current track}
        else
            return "stopped"
        end if
    end tell
else
    return "stopped"
end if

这很简单,我只是找不到它,所以 Doug 制作了一个非常简单的脚本,您可以找到它 here 我做了一些修改,它将当前曲目插图保存到与脚本并将其命名为 tmp.jpg 或 png

tell application "Music"

    try

        if player state is not stopped then

            set alb to (get album of current track)

            tell artwork 1 of current track

                if format is JPEG picture then

                    set imgFormat to ".jpg"

                else

                    set imgFormat to ".png"

                end if

            end tell

            set rawData to (get raw data of artwork 1 of current track)

        else

            return

        end if

    on error

        display dialog "Problem getting track info." buttons {"OK"}

        return

    end try

end tell


--get current path
tell application "Finder"
    set current_path to container of (path to me) as alias
end tell

--create path to save image as jpg or png
set newPath to ((current_path as text) & "tmp" & imgFormat) as text

try
    --create file
    tell me to set fileRef to (open for access newPath with write permission)

    --overwrite existing file
    write rawData to fileRef starting at 0

    tell me to close access fileRef

on error m number n

    log n

    log m

    try

        tell me to close access fileRef

    end try

end try