りんごの備忘録

へなちょこポカ大生のへなちょこブログ

AppleScriptでアートワークを入手するまで(part2)

前回,アルバムアートワークを入手するスクリプトを作ったがいかんせん精度が低い.
rinringo429.hatenablog.com


それならば画像を人間の目で選べばいいだろうと考え新しく作ってみました.

tell application "iTunes"
        set theAlbum to item 1 of album of selection
	set theAlbumArtist to item 1 of album artist of selection
	set theArtist to item 1 of artist of selection
	set searchURL to "https://www.google.co.jp/search?q=" & theAlbum & "+" & theAlbumArtist & "&source=lnms&tbm=isch&sa=X" as string
	
	tell application "Safari" to open location searchURL
	
	set the clipboard to theAlbum
end tell

display dialog "Please select the picture"

tell application "iTunes" to set theAlbum to album of item 1 of selection
set theClip to (get the clipboard)
set theDestination to "" -- 画像を保存するパスを指定
set thePath to theDestination & ":" & theAlbum & ".png"
do shell script "curl -L " & theClip & " -o \"" & POSIX path of thePath & "\""
set targetImageFile to thePath
tell application "iTunes"
	set theTrack to selection
	repeat with i in theTrack
		set data of artwork 1 of i to (read (file targetImageFile) as picture)
	end repeat
end tell
使い方
  1. iTunesで一つのアルバムを⌘+左クリックで選択する
  2. 上のコードを実行し適当な画像のurlをクリップボードにコピーする
  3. 元から出ていたdialog のOKボタンを押す
  4. 後は待つだけ