twitterに今聴いてる曲(iTunes)を自動で送信する

twitterに今聴いてる曲(iTunes)を自動で送信する。AppleScriptで。

目指すは「全てをAppleScriptで」だけど、とりあえず出してみる。
_delayとか無駄っぽい変数も整理しないといけないし、混んでる時はスクリプトが落ちる。(curlでエラーになった時の処理が。。。)あと、curlに渡してるオプション文字列。。。

使えば意外に便利なAppleScript。だけど、なかなか慣れないなあ。。。

set conf_delay to 30
set conf_msg to "now playing: "
set conf_user to "YOUR USERNAME"
set conf_pass to "YOUR PASSWORD"

on get_itunes_currenttrack()
	-- check if iTunes is active and playing
	tell application "System Events"
		if (application process "iTunes" exists) then
			set is_iTunes to true
			tell application "iTunes"
				-- check if iTunes is play or not
				if player state is playing then
					set currenttrack to current track
					set track_info to (name of currenttrack & " / " & artist of currenttrack)
				else
					set track_info to false
				end if
			end tell
		else
			set track_info to false
		end if
	end tell
	return track_info
end get_itunes_currenttrack

on timer_handle(__delay, _prev_track)
	global conf_msg
	global conf_user
	global conf_pass
	set current_track to get_itunes_currenttrack()
	--	log conf_msg & current_track
	if current_track = _prev_track then
		log "identical"
	else
		if current_track is not false then
			log "track changed!"
			set twitter_msg to conf_msg & current_track
			set twitter_login to conf_user & ":" & conf_pass
			set shell_cmd to "curl -s --basic --user \"" & twitter_login & "\" --data-ascii \"status=" & twitter_msg & "\"  \"http://twitter.com/statuses/update.json\""
			log shell_cmd
			do shell script shell_cmd
		end if
	end if
	return current_track
end timer_handle

on timer_loop(_delay)
	set prev_track to ""
	repeat
		set prev_track to timer_handle(_delay, prev_track)
		delay _delay
	end repeat
end timer_loop

log "here we go"
timer_loop(conf_delay)

curlで更新する部分はシェルスクリプトを参考にしたのだけど、どこで見つけたんだったか。。。


AppleScriptスーパーpre記法が対応してない。寂しい。。。