iChat Status to Twitter script

Twitter is one of those neat technologies that comes along every so often. I’d heard about it a few times before my friend Gabriel got me on, and slowly, people I know are starting to use it as well.

About a week ago, I put two and two together (and nearly reached five), and thought up a good application to try writing in Applescript.

I use Twitter primarily through iChat (when that is up), and it occurred to me that it might be handy to be able to display the iChat status messages, including what is currently playing in iTunes.

Anyone that knows me, knows that I *love* music, listen to music pretty much all day long, and love introducing people to new music. Those on my iChat lists or who watch my blog (note the “Currently Listening” panel on the right) get to watch what I’m listening to through out the day – sometimes eliciting comments.

Here it is, my first official, public Applescript :

You can download the zipped Application iChat-to-Twitter-0.14.zip

or copy and paste the code:


(* iChat Status to Twitter
Version 0.14
March 26 2007
By W. Thomas Leroux - http://www.leroux.ca/
Thanks to the Applescript mailing list for the help in getting this thing to work.
*)

(* SET VARIABLES *)
set update_interval to 5 (* minutes *)
set twitter_url to "http://twitter.com/statuses/update.json"
set exclusion_character to "*"
set old_status to ""
set show_itunes to "yes"
set itunes_message to "iTunes: "
set update to "yes"
set music_playing to ""
set warn_before_sending to true (* or false *)

(* Ask user if they want to approve each Twitter before it is sent *)
if button returned of (display dialog "Would you like a warning message before each Twitter is sent for your approval or rejection?" & return & "" buttons {"No", "Yes"}) is "No" then set warn_before_sending to false

(* Set the interval between Twitters *)
set update_interval_entered to text returned of (display dialog "Select an interval between 3 and 60 minutes (other input will be ignored):" default answer update_interval)

(* Check the input - if nonsense, use default *)
if update_interval_entered is greater than 0 and update_interval_entered is less than 61 then
set update_interval to (update_interval_entered * 60)
else
set update_interval to (update_interval * 60)
end if

(* Retrieve Twitter Username and password from Keychain *)
tell application "Keychain Scripting"
set twitter_key to first Internet key of current keychain whose server is "twitter.com"
set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
end tell

repeat
(* Check if iTunes is running and grab current song / artist *)
tell application "System Events"
if show_itunes = "yes" then
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
set music_playing to itunes_message & current_song & " by " & current_artist
if (current_artist is "") then set music_playing to ""

end if
end tell
end if
end if

(* Check if iChat is running *)
if exists process "iChat" then
tell application "iChat"
(* Get the Status Message *)
set the_status to status message
if show_itunes = "yes" and the_status = "%Track - %Artist" then set the_status to music_playing
(* Checks to see if the last character of the status message is the exclusion character *)
if the_status is not "" then
set last_character to last character of the_status
if last_character is equal to exclusion_character then set the_status to ""
end if
end tell
else
display dialog "iChat isn't running, silly."
end if
end tell

(* Is there need to send an update? *)
if (the_status is equal to old_status) or (the_status is equal to "") then
set update to "no"
else
(* Show warning, if requested *)
if warn_before_sending and "Yes" is button returned of (display dialog "Send this: " & the_status & return & "" buttons {"No", "Yes"}) then
set update to "yes"
else
set update to "no"
if warn_before_sending is false then set update to "yes"
end if
end if

(* Send to Twitter *)
if update is "yes" then
set the_status to quoted form of ("status=" & the_status)
(*display dialog "Sending Twitter" & update_interval & ":" & last_character*)
try
set twitter_results to do shell script "curl --user " & twitter_login & " --data-binary " & the_status & " http://twitter.com/statuses/update.json"
on error
(* In case curl fails for some reason, just don't do anything *)
end try

end if

set twitter_results to ""
set old_status to the_status
delay update_interval
end repeat

When you run it it will ask if you want a warning message displayed before each message is sent to Twitter, and another to select an interval of posting to Twitter (default is 5 minutes). If nothing has changed, in the five minutes, a new message isn’t sent. There are probably bugs. Contact me and let me know how to reproduce the problem, and I’ll get to it when I can.

(The real first Applescript I wrote is the one that adds the content for the “”Currently Listening” pane to the blog – when it is more polished, I’ll offer that up as well)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *