AppleScriptを使ってiTunesのアートワークを入手するまで
iTunesの”アートワークの入手”の精度があまりにも低い!!これはなんとかしなければということで,AppleScriptを用いてwebからアートワークを持ってくるものを作ってみました.JavaScriptのsrcを使って画像のソースを取得してurlのときとbase64のときで分けて処理しました.
Googleの画像検索でフルサイズの画像を入手する方法が分からなかったため解像度はよくありません.
いい方法がございましたら教えて頂けたら嬉しいです.
--事前にアートワークを入手したいアルバムをcommand+左クリックで選択しておく tell application "iTunes" --set theSelctionAlbum to a reference to selection set theAlbum to item 1 of album of selection set theArtist to item 1 of album artist of selection set searchURL to "https://www.google.co.jp/search?q=" & theAlbum & "+" & theArtist & "&source=lnms&tbm=isch&sa=X" as string tell application "Google Chrome" to open location searchURL tell application "Google Chrome" --display dialog "Please Wait" delay 5 tell window 1 tell active tab loading_confirm(20) of me set theNumber to execute javascript "document.images.length" if theNumber > 30 then set theNumber to 30 end if set theURL to "" set theFlag to 0 repeat with i from 1 to theNumber try set theHeight to execute javascript "document.images[" & (i as string) & "].height" set theWidth to execute javascript "document.images[" & (i as string) & "].width" set theSrc to execute javascript "document.images[" & (i as string) & "].src" set theCounter to (count of theSrc) --display dialog theCounter if (theHeight > 200) and (theWidth > 200) and (theCounter > 500) then set theBase64 to theSrc set theFlag to 1 if (count of theSrc) > (count of theBase64) then set theBase64 to theSrc end if else if (theHeight > 200) and (theWidth > 200) and (theCounter < 500) then set theURL to theSrc set theFlag to 2 end if end try end repeat end tell close active tab end tell end tell end tell set theDestination to "" --your path set thePath1 to theDestination & "/" & theAlbum & ".txt" set thePath2 to theDestination & "/" & theAlbum & ".png" --base64の場合デコードを行う if theFlag is 1 then set theBase64Ref to refTextByDelimiters(theBase64) tell application "Finder" set theTextFile to open for access thePath1 with write permission set theEOF to get eof of theTextFile try write theBase64Ref starting at (theEOF + 1) to theTextFile on error theErrorText display dialog theErrorText end try close access theTextFile end tell do shell script "base64 -D " & thePath1 & " -o " & thePath2 do shell script "rm " & thePath1 else if theFlag is 2 then do shell script "curl -L " & theURL & " -o " & POSIX path of thePath2 else if theFlag is 0 then display dialog "Cannot find an artwork of " & theAlbum end if --文章をあるデリミタで区切る on refTextByDelimiters(theText) set tmp to AppleScript's text item delimiters set AppleScript's text item delimiters to "," set refText to last text item of (theText as string) set AppleScript's text item delimiters to tmp return refText end refTextByDelimiters --タイムアウトの時間設定 on loading_confirm(time_out) delay 2 repeat with i from 1 to the time_out tell application "Google Chrome" tell window 1 tell active tab if (execute javascript "document.readyState") is "complete" then return true else if i is the time_out then return false else delay 1 end if end tell end tell end tell end repeat return false end loading_confirm