iTunesで聞いてる曲をbrightkiteにPostするAppleScript

以前に、
iTunesで聞いてる曲をTwitterにPostするAppleScript - Macと愛猫
を作ったんですが、
Twitterの認証方式が変わってしまい、Basic認証が通らなくなってOAuth認証しか受け付けなくなりました。

PHPObjective-Cで作ったアプリなら簡単にOAuth認証に変更できるんですが、
AppleScriptではどうも難しいようで、できそうな気もしますがぐぐってもサンプルソースが出てこなかったのでとりあえず諦めました。


Twitterがダメなら他のやつにアップすればいいじゃない、最終的にTwitterに行くし、ってわけでbrightkiteにアップするように変更しました。


Brightkiteにアップするなら写真くらいつくようにしないと芸がないので、iTunesからアートワークを引っこ抜いてアップするようにしました。
アートワーク関連のコードはこちらを思いっきり参考にさせて頂きました。
http://members.at.infoseek.co.jp/obto/script/script3.html#artworksave
感謝です。


brightkiteっていったいなんだろう?って人は以下のエントリを参照してください。
Brightkiteのススメ - Macと愛猫




ここからアプリの説明です。


ダウンロードはこちら
http://files.lingurin.com/BKTunes.zip

インストール方法は、
ダウンロードして解凍したスクリプト
/Users/ユーザ名/ライブラリ/iTunes/scripts/
に放り込む。scriptsディレクトリが無かったら作って放り込む。

BKTunesをAppleScriptエディタで開いて、

    • 一番上の「ユーザ名:パスワード」となっている箇所をBrightkiteのアカウントとパスワードに変更(コロン「:」で挟んで)
    • change the status message to your liking here:のところを自分らしいメッセージに変更

して保存しておく。


使い方は、
iTunesで再生中に、メニューバーのスクリプトのマークからBKTunesを選ぶとダイアログが出てくるのでOKを押すだけ。

デスクトップにBKTunesTemporaryArtWork.pngとかBKTunesTemporaryArtWork.jpgってファイルができる。ポストが終わったら消してしまってOK。



以下スクリプトソース。

--BKTunes

set loginuserpass to "ユーザ名:パスワード"

on replace(src, tg, rp)
	
	set oldDel to AppleScript's text item delimiters
	set AppleScript's text item delimiters to tg
	set myList to text items of src
	set AppleScript's text item delimiters to rp
	set myText to myList as string
	set AppleScript's text item delimiters to oldDel
	return myText
	
end replace

on getartwork()
	
	--アートワークを保存
	tell application "iTunes"
		set seltrack to a reference to selection
		set thefile to ""
		repeat with x in seltrack
			
			set {theartist, thealbum, ext} to {artist of x, album of x, ""}
			
			repeat with y from 1 to (count artworks of x)
				
				set err to 0
				try
					set theartwork to raw data of artwork y of x
					set theformat to format of artwork y of x as string
					if theformat contains "JPEG" then set ext to ".jpg"
					if theformat contains "PNG" then set ext to ".png"
					if ext is "" then
						display dialog theartist & " \"" & thealbum & "\" " & "のアートワークは書き出せません。"
						set err to 1
					end if
				on error
					display dialog theartist & " \"" & thealbum & "\" " & "にはアートワークがありません。"
					set err to 1
				end try
				
				if err is 0 then
					set thefilename to "BKTunesTemporaryArtWork" & ext
					try
						tell application "Finder"
							set thefile to make new file at desktop with properties {name:thefilename, kind:"file", owner privileges:"read write"}
						end tell
					on error
						
					end try
				end if
				
				if err is 0 then
					set thefile to ((path to desktop folder) as string) & thefilename as alias
					open for access thefile with write permission
					write theartwork to thefile
					close access thefile
				end if
				
			end repeat
			
		end repeat
		
	end tell
	
	if thefile = "" then
		return ""
	else
		set ret to POSIX path of thefile as string
		return ret
	end if
	
end getartwork

--iTunesから情報をゲット
tell application "iTunes"
	set thisTrack to current track
	set trackName to the name of thisTrack
	set trackArtist to thisTrack's artist
	set trackAlbum to thisTrack's album
	set janru to thisTrack's genre
	set hoge to rating of current track
end tell

--アートワークゲット
set thefilename to getartwork()
if thefilename = "" then
	display dialog "アートワークがありませんが続行しますか?"
end if


--評価を編集
if hoge = 0 then
	set hoshi to "は未評価"
end if
if hoge = 20 then
	set hoshi to "★・・・・"
end if
if hoge = 40 then
	set hoshi to "★★・・・"
end if
if hoge = 60 then
	set hoshi to "★★★・・"
end if
if hoge = 80 then
	set hoshi to "★★★★・"
end if
if hoge = 100 then
	set hoshi to "★★★★★"
end if

-- change the status message to your liking here:
set tweet to trackArtist & "の" & trackAlbum & "の" & trackName & "を聴いてる。ジャンルは" & janru & "。俺の評価" & hoshi & "。"
set tweet to replace(tweet, "&", "&")

-- let the user edit
display dialog "Edit your post to Brightkite" with title "BKTunes" default answer tweet cancel button 1 default button 2 buttons {"Cancel", "Send"}
set tweet to (text returned of result)


--post to twitter 旧コード
--set twitter_status to quoted form of ("status=" & tweet)
--set results to do shell script "curl --user " & loginuserpass & " --data-binary " & twitter_status & " http://twitter.com/statuses/update.json"

--to BK
if thefilename = "" then
	set results to do shell script "curl -u " & loginuserpass & " -X POST \"http://brightkite.com/objects.json\" -d \"object[body]=" & tweet & "\" -d \"object[share_with]=everybody,twitter,facebook\"  "
else
	set results to do shell script "curl -u " & loginuserpass & " -X POST \"http://brightkite.com/objects.json\" -F \"object[body]=" & tweet & "\" -F \"object[share_with]=everybody,twitter,facebook\"  -F \"object[photo]=@" & thefilename & "\" "
end if

display dialog "Brightkite Post was completed." & tweet with title "BKTunes" default button 1 buttons "OK"


お約束:自己責任でお願いします。このスクリプトを使用してなんらかの損害を被っても制作者の私は一切責任を負えません。