ruby script to set skype and adium mood message with twitter on osx

Twitter is a great way to learn many little web2.0ish things. I wanted to set the status message on my Skype and Adium clients using my last twitter message. So I found a howto document by Michael Tyson which I adapted a bit to add Skype support and to only post twits that were not replies to someone else - I decide there was just too much loss of context for that to make sense.

#!/usr/bin/env ruby
#
# Update iChat/Adium/Skype status from Twitter
#
# Michael Tyson 
# http://michael.tyson.id.au
# Contributor: Henry Story

# Set Twitter username here
Username = 'bblfish'

require 'net/http'
require 'rexml/document'
include REXML

# 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 = XPath.match(doc,"//content").detect { |c| not /@/.match(c.text)}
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" +
         'if exists process "Skype" then tell application "Skype" to send command "set profile mood_text "'
         + ' & message script name "twitter"'
         + "\\n" +
         'end tell' + "\\n"

IO.popen("osascript", "w") { |f| f.puts(script) }

This can then be added to the unix crontab as explained in Michael's article, and all is good.

What can one learn with this little exercise? Quite a lot:

  • Ruby - this is my first Ruby hack
  • Atom - twitter uses an atom xml feed to publish its posts
  • unix crontab
  • AppleScript to send messages to all these silly OSX apps
  • vi to edit all of this, but that's not obligatory, you can use less viral ones
  • the value of reusing data accross applications
So that's a good way to spend a little time when one has had a little bit too much to drink the night before. Hmm, is this what one calls procrastination (video)?

Comments:

Post a Comment:
Comments are closed for this entry.
About

bblfish

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today