Set iChat/Adium status line to latest Twitter message

Oct 13, '08 07:30:00AM

Contributed by: miketyson

Expand the reach of your Twitter status updates by making them propagate to your instant messaging accounts in iChat or Adium. Here's a Ruby script that grabs your latest Tweet and sets it as the status for iChat and Adium, if they're running. Add it to your crontab to run it regularly and keep in sync.

Instructions:

  1. Save the following code to ~/Library/Scripts, and name it sync-status-with-twitter.rb:
    #!/usr/bin/env ruby
    #
    # Update iChat/Adium status from Twitter
    #
    # Michael Tyson 
    # http://michael.tyson.id.au
    
    # Set Twitter username here
    Username = 'MyUserName'
    
    require 'net/http'
    require 'rexml/document'
    
    # Download timeline XML and extract latest entry
    url = "http://twitter.com/statuses/user_timeline/" + Username + ".atom"
    xml_data = Net::HTTP.get_response(URI.parse(url)).body
    doc    = REXML::Document.new(xml_data)
    latest = doc.root.elements['entry/content']
    message = latest.text.gsub(/^[^:]+:\s*/, '')
    
    exit if ! message
    
    # Apply to status
    script = 'set message to "' + message.gsub(/"/, '\\"') + "\"\n" +
             'tell application "System Events"' + "\n" +
             'if exists process "iChat" then tell application "iChat" to set the status message to message' + "\n" +
             'if exists process "Adium" then tell application "Adium" to set status message of every account to message' + "\n" +
             'end tell' + "\n"
    
    IO.popen("osascript", "w") { |f| f.puts(script) }
  2. Open it in, say, TextEdit and replace MyUserName at the top of the file to your actual Twitter username.
  3. Open Terminal (in Applications » Utilities), and make the script executable: chmod +x ~/Library/Scripts/sync-status-with-twitter.rb
  4. Open your crontab: crontab -e
  5. Add a line to crontab to run the script regularly:
  6. Press escape to stop editing
  7. Hold down shift and press ZZ (Z key twice) to save and quit
You're done. Wait a little bit and it'll update, or if you like, run it immediately from Terminal (~/Library/Scripts/sync-status-with-twitter.rb). If you want to update Facebook too, you'll have to add the Twitter application to Facebook itself. This script was inspired by a similar script by John Nunemaker over at RailsTips.org. Thanks, John.

For more help with this script, please see the original post on my blog.

[robg adds: I haven't tested this one.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20081009195405132