ミュージック.appの全てのアルバムカバーアートを書き出す

[主題]
ミュージック.appの音楽ライブラリに格納されているアルバムカバーアートを書き出すAppleScriptを作成したので、公開する。

[背景]
ミュージック.appで現在再生中のトラック(Current Track)から、アルバムカバーアートを書き出すAppleScriptは、ネット上のリソースにある。しかし、ライブラリにある全てのアルバムカバーアートを書き出すものは見かけない。
そこで、再生中のトラックからアルバムカバーアートを書き出すものを参考に、ライブラリにある全てのアルバムカバーアートを書き出すAppleScriptを筆者が自作したので、手前味噌ながら公開したい。

[環境]
参考までに、筆者の環境を記載しておく
MacBook Air 2023 15.3inch(Apple Silicon Mac, M2)
macOS Sonoma 14.2.1
Xcode 15.2
CommandLine Tools for Xcode 15.1.0.0.1

[前提]
本スクリプトは、アルバム数の取得にディスク番号を用いている。よって、ディスク番号が正しいこと(枚数が1枚の時、空欄のものは許容)が前提となる。

[AppleScript]
スクリプトを以下に示す。内容については、スクリプト内のコメントを参照されたい。

        --ベースにしたスクリプト
        --以下の2つのスクリプトを大いに参考にさせて戴きました。感謝します。
        --Save Current Track's Artwork
        --https://dougscripts.com/itunes/2014/10/save-current-tracks-artwork/
        --iTunes のアートワークをファイルに書き出す
        --https://ashplanning.blogspot.com/2006/12/itunes.html

        on run

            --出力フォルダの選択
            try
                set choosedFolder to (choose folder with prompt "アートワークを出力するフォルダを選択して下さい。") as string
            on error
                return
            end try

            --重複ファイル検査用カウンタ
            set aCTR to 0

            tell application "Music"

                --ミュージック.appのライブラリを取得
                set everyTrack to (every track of playlist "ミュージック")

                repeat with aTrack in everyTrack

                    --ディスク番号が1未満は、空欄を意味する。
                    if disc number of aTrack < 2 then

                        --その中のトラック1を抽出
                        if track number of aTrack is 1 then

                            set thisArtist to artist of aTrack
                            set thisAlbum to album of aTrack

                            --複数のアートワークに対応するためには、このようにtellする必要がある。
                            --アーティスト名とアルバム名により、一意のトラックを識別する。
                            tell (track 1 whose album is thisAlbum and artist is thisArtist)

                                --アートワークがある場合
                                if exists artworks then

                                    --複数のアートワークに対応。
                                    set thisArtwoksCount to count of artworks

                                    repeat with i from 1 to thisArtwoksCount

                                        set fileName to thisArtist & "_" & thisAlbum & "_" & (i as string)

                                        --ファイル名に":"を含まないようにする
                                        set repFname to repChar(fileName, ":", "_") of me

                                        tell artwork i

                                            --画像フォーマットはjpgかpngのどちらか
                                            if format is JPEG picture then
                                                set imgFormat to ".jpg"
                                            else
                                                set imgFormat to ".png"
                                            end if

                                            --出力ファイル名
                                            set theFile to (choosedFolder & repFname & imgFormat) as string

                                            tell application "Finder" to set fileExists to exists of alias theFile

                                            --ファイル名が重複しない場合は書き出す。
                                            if not fileExists then

                                                --set theResult to my saveToFile(theFile, data)
                                                my saveToFile(theFile, data)

                                                --if theResult then
                                                --ファイルタイプ/クリエータタイプは何を設定しても無駄。
                                                --拡張子を削除すると、テキストエディット書類になる。
                                                --my openWithPreview(theFile, imgFormat)
                                                --end if
                                            else
                                                --Finderによる重複ファイルカウントのカウントアップ
                                                set aCTR to aCTR + 1

                                            end if
                                        end tell
                                    end repeat
                                end if
                            end tell
                        end if
                    end if
                end repeat
            end tell

            if aCTR > 0 then
                activate
                display dialog "重複ファイルが " & aCTR & " 件ありました。" as string
            end if

            beep

        end run

        on saveToFile(theFile, theData)
            try
                set fh to open for access file theFile with write permission
                write theData to fh starting at 0 --スタートは0で良い。
                close access fh
                return true
            on error errMsg number errNum
                try
                    close access fh
                end try
                log {errNum, errMsg}
                return false
            end try
        end saveToFile

        --使わない
        on openWithPreview(theFile, imgFormat)
            tell application "Finder"
                if imgFormat is ".jpg" then
                    set file type of (theFile as alias) to "JPEG"
                else
                    set file type of (theFile as alias) to "PING"
                end if
                set creator type of (theFile as alias) to "prvw"
            end tell
        end openWithPreview

        --文字置換 v2
        --Thanks for Piyomaru Software
        --http://piyocast.com/as/archives/476
        on repChar(origText as string, targChar as string, repChar as string)
            set curDelim to AppleScript's text item delimiters
            set AppleScript's text item delimiters to targChar
            set tmpList to text items of origText
            set AppleScript's text item delimiters to repChar
            set retText to tmpList as string
            set AppleScript's text item delimiters to curDelim
            return retText
        end repChar
    

上記スクリプトをScriptEditorで開く(注)

(注)リンクを開くと、2段階の確認ダイアログが表示される。(下図)

ScriptEditorを開く確認ダイアログ


Script作成確認ダイアログ

最初に開くのは、ScriptEditorを開くかどうかを確認するダイアログ。次に開くのは、スクリプトの開発元が不明であることを警告するダイアログである。後者のダイアログについて、筆者を信頼するかしないかはお任せするが、このAppleScriptが危険なものか判断できない人は、使わないで戴きたい。

[参考記事]

以上。

この投稿へのコメント

コメントはありません。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

この投稿へのトラックバック

トラックバックはありません。

トラックバック URL