Making Applescript suck less with Ruby
I was messing around with Adium’s iTunes status message functionality today and ended up coming across RubyOSA, a gem that bridges AppleEvent functionality with Ruby. Â You can use RubyOSA to do anything that AppleScript can do, using familiar Ruby syntax. Â It also comes with a command-line tool that you can use to generate RDoc-style documentation for an application that you’re going to control with RubyOSA.
I created a ruby script that updates my status message every 30 seconds to say which web page I’m looking at in Safari. I’m calling it BrowserCam.
Update: I let this run for a few hours the other day and it was a really interesting experience. In some ways it’s less invasive than a webcam, but in some circumstances I would have felt more comfortable with someone seeing my face than seeing what web page I’m looking at. And I was only looking at softcore porn! Just kidding. The exercise did make me more conscious about how much time I was spending on certain pages (such as my gmail page) and was a valuable experience from that perspective if nothing else. I’d like to play around with this a little more sometime; there’s an academic paper lurking in there somewhere.
Here’s how it works.
First, you need to install the gem:
sudo gem install rubyosa
Then you can use this script to grab the frontmost window in Safari and update your status message appropriately. I set it to run 2x a minute on a cron job, so my status message was getting updated every 30 seconds.
#!/usr/bin/ruby
require 'rubygems'
require 'rbosa'
adium = OSA.app('Adium')
return if adium.nil?
def get_status_message
safari = OSA.app('Safari')
return "*isn't looking at any web page*" if safari.nil?
window = safari.windows.first
if window.nil?
message = ""
else
message = window.name
end
time_str = Time.now.strftime("%I:%M:%S %p")
status_message = "*is looking at a web page titled '#{message}'* (#{time_str})"
end
adium.adium_controller.my_status_message = get_status_message