Blog Planet Wiki
Feed Feed It

Song of the Day Script

Saturday, October 12, 2008 01:10 PM

I have tried to religiously start twittering my song of the day. I think that I might twitter this information more often if I did not have to type it out either on my laptop or via text messaging. I thus decided write a script to automate the process. There's plenty of resources online to help with this kind of thing. I found a applescript that pulled the song you are currently playing in iTunes and twittered it, so I decided to modify it to my needs. To run this script, just open up Script Editor on OS X.

P.S. To my twitter friends sorry for posting two "song of the day" tweets in a row.

(*
Song of the Day to Twitter by Nick Granado
Massive amounts of inspiration from http://www.leroux.ca/archive/dsp_view_474.cfm
kthxbye
*)

(* SET VARIABLES *)
set twitter_username to "twitter_username_here"
set twitter_password to "password_here"

global twitter_login
global twitter_url
global song_of_the_day

set twitter_login to quoted form of (twitter_username & ":" & twitter_password)
set twitter_url to "http://twitter.com/statuses/update.json"
set song_of_the_day to ""

on get_song_of_the_day()
	(* Check if iTunes is running and grab current "artist - song" *)
	set current_artist to ""
	set current_song to ""
	set temp_song_of_the_day to ""
	
	tell application "System Events"
		if exists process "iTunes" then
			tell application "iTunes"
				if player state is playing then
					set current_song to (name of current track) as Unicode text
					set current_artist to (artist of current track) as Unicode text
					if current_artist is equal to "" then set current_artist to "Unknown"
				end if
			end tell
		else (* What to do if iTunes *isn't* running *)
			if button returned of (display dialog "iTunes isn't running" 
				& return 
				& "" buttons {"Wait", "Quit"}) is "Quit" then quit
		end if
	end tell
	set temp_song_of_the_day to "song of the day: " 
					& change_case(current_artist, 0) 
					& " - " 
					& change_case(current_song, 0)
	return temp_song_of_the_day
end get_song_of_the_day

on send_to_twitter(status)
	set the_status to quoted form of ("status=" & status)
	try
		(* Shelling out is cheating, but it gets the job done. *)
		set twitter_results to do shell script "curl --user " 
					& twitter_login 
					& " --data-binary " 
					& the_status 
					& " " 
					& twitter_url
	on error
		(* In case curl fails for some reason, just don't do anything *)
	end try
end send_to_twitter

on change_case(this_text, this_case)
	if this_case is 0 then
		set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		set the source_string to "abcdefghijklmnopqrstuvwxyz"
	else
		set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
		set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	end if
	set the new_text to ""
	repeat with this_char in this_text
		set x to the offset of this_char in the comparison_string
		if x is not 0 then
			set the new_text to (the new_text & character x of the source_string) as string
		else
			set the new_text to (the new_text & this_char) as string
		end if
	end repeat
	return the new_text as Unicode text
end change_case

set song_of_the_day to get_song_of_the_day()
send_to_twitter(song_of_the_day)

Pink for October

Thursday, October 02, 2008 07:14 AM

Big thanks to the Merc for the reminder on Breast Cancer Awareness month. Looks like I'll be pink for the rest of October. I hope next year I can be pink for all of October!!