brightkiteにiTunesで聴いてる曲をポストするBKTunesを更新


早速更新。

    • アートワークの取得方法を見直し。操作によっては別の曲のアートワークを取得してたかも。また、アートワークが複数あっても最初の一個を取るようにしました。
    • ポストする前にプレビューできるようにしました。ソースの中の最初の方のconfirmPreviewをtrueにすると有効に。動きがあやしいのでどうしてもポストする前に確認したい人向け。
    • アートワークのテンポラリファイルを削除するようにしました。でも途中でキャンセルしたりすると残りますが気にしないでください。
    • 特定の場所でポストできるようになりました。ソース中のplace_idを書き換えてください。最初はSomewhere in the worldになってます。place_idはbrightkiteのチェックイン場所のリンク、例えば「http://brightkite.com/places/1653b5213d08a92265df634a524542dc」の謎の文字列がそれです。


設置方法、ダウンロードなどは昨日のエントリを見てください。
iTunesで聞いてる曲をbrightkiteにPostするAppleScript - Macと愛猫


以下、スクリプトソース。

--BKTunes

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

set place_id to "00000000000000000000000000000000" --Somewhere in the world

set confirmPreview to false


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 ""
		
		set x to current track
		
		set {theartist, thealbum, ext} to {artist of x, album of x, ""}
		
		--repeat with y from 1 to (count artworks of x)
		set y to 1
		
		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 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 "アートワークがありませんが続行しますか?"
else
	if confirmPreview then
		--プレビューを立ち上げる
		activate application "Preview"
		tell application "Preview"
			open thefilename
		end tell
		--続行するかどうか聞く
		display dialog "アートワークが取得できました。続行しますか?"
	end if
	
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\"  -d \"object[place_id]=" & place_id & "\" "
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[place_id]=" & place_id & "\" -F \"object[photo]=@" & thefilename & "\" "
end if

display dialog "ポスト完了 " & tweet with title "BKTunes" default button 1 buttons "OK"

--アートワークファイルを削除
if thefilename ≠ "" then
	do shell script "rm " & thefilename
end if

所詮はスクリプトアプリなので、動きがおかしくてもだましだまし使ってくださいね。