symbol blog  ·  archive  ·  about  ·  Feed feed

Song of the Day Script

Sunday, 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

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 ""
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)
        (* 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"
        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
            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()