

先週末に iTunes のアートワークがあまりにも入ってないことに気づいて Cover Flow がかなりしょぼいことになってた。アートワークを Amazon やらから取得するツールはたくさんあるのだけど解像度が高い画像をとれないのがちょっと。
そこで Xcode を使ってアプリでも作るかと思ったけど C++ は書けても Objective-C なんぞは無理。じゃあ前から気になっていた AppleScript で、ということで簡単に。
基本は Amazon ESC 使って Large サイズのサムネイルを取得してそれを iTunes にアートワークに追加するという流れ。アートワーク追加がまだうまくいってないのでとりあえずはサムネイル取得とそれを表示までやってみた。
Amazon からのサムネイル取得は以下のソースを思い切り流用。アクセスキーを個人的に持ってなかったので旧来のサブスクリプションIDを使用するように変更。
ちゃらんぽらん : Amazon Web サービス
Xcode でダウンロード開始する clicked アクションを持ったボタンと ImageView(以下のソースだと "imageFrame" という名前) を配置。そして以下のようなスクリプトを書いてビルドするとOK。
property tmpFolder : path to temporary items folder from user domain
property tmpXMLFile : POSIX path of ((tmpFolder as Unicode text) & "request.xml")
property tmpPNGFile : POSIX path of ((tmpFolder as Unicode text) & "request.png")
property tmpJPGFile : POSIX path of ((tmpFolder as Unicode text) & "request.jpg")
property baseurl : "http://webservices.amazon.co.jp/onca/xml"
property service : "AWSECommerceService"
--property accesskey : " " -- ここにアクセスキー
property subscriptionId : " " -- ここにサブスクリプション ID
property associateid : "xxxxxxxx-22" -- ここにアソシエイト ID
property operation : "ItemSearch"
property searchIndex : "Music"
property responsegroup : "Request,Small,Images"
property AWSVersion : "2006-05-17"
property pagenumber : "1"
property sort : "salesrank"
property targetPathMac : ""
property targetPathPosix : ""
on clicked theObject
--Get Current Track
tell application "iTunes"
set curTrack to current track
set theArtist to artist of curTrack
set theTrack to name of curTrack
end tell
set searchWords to (theArtist & " " & theTrack)
set encodedText to my encodeURL(searchWords)
set keywords to encodedText
set request to baseurl
set request to request & "?Service=" & service
--set request to request & "&AWSAccessKeyId=" & accesskey
set request to request & "&SubscriptionId=" & subscriptionId
set request to request & "&AssociateTag=" & associateid
set request to request & "&Operation=" & operation
set request to request & "&Keywords=" & keywords
set request to request & "&SearchIndex=" & searchIndex
set request to request & "&ResponseGroup=" & responsegroup
set request to request & "&Page=" & pagenumber
set request to request & "&Version=" & AWSVersion
set request to request & "&Sort=" & sort
do shell script "curl -o " & quoted form of tmpXMLFile & " " & quoted form of request
tell application "System Events"
set xmlFile to XML file tmpXMLFile
set root to XML element 1 of xmlFile
set itemsElement to XML element "Items" of root
set imageURL to ""
repeat with thisElement in (XML elements of itemsElement whose name is "Item")
set theURL to value of XML element "DetailPageURL" of thisElement
set largeImageElement to XML element "LargeImage" of thisElement
set largeImageURL to value of XML element "URL" of largeImageElement
set imageURL to largeImageURL
exit repeat
end repeat
end tell
--For Debug
--display dialog imageURL
--JPG or PNG
if imageURL contain ".jpg" then
set targetPathPosix to tmpJPGFile
else if imageURL contains ".png" then
set targetPathPosix to tmpPNGFile
else
beep
return
end if
--Path Conv
set targetPathMac to (POSIX file targetPathPosix) as text
--Download
tell application "URL Access Scripting"
try
download imageURL to file targetPathMac replacing yes
on error
display dialog imageURL & " download error"
return
end try
end tell
--Display Downloaded Image
tell window of theObject
try
set loadedImageData to load image targetPathPosix
on error
display dialog "Load Image Failed : " & targetPathPosix
return
end try
set contents of image view "imageFrame" to loadedImageData
end tell
end clicked
on encodeURL(theText)
if not ((theText starts with "'") and (theText ends with "'")) then
set theText to quoted form of theText
end if
try
do shell script "echo " & theText & " | " & "php -r 'echo rawurlencode(`cat -`);'"
on error eMessage number eNumber
tell application (path to frontmost application as Unicode text)
activate
display dialog (eNumber & return & eMessage & return & return) as Unicode text buttons {"OK"} default button 1 with icon 0
end tell
return ""
end try
end encodeURL
簡単であるが、やや癖のある AppleScript。今後使っていくことも多そうだ。Mac のアプリケーション機能をすぐに引き出せるところが魅力的。