2 posts tagged “code”
Now I'm running Mac OS X 10.5, I thought it might be fun to use this as an exercise in using Ruby to call Objective C, and hence the new Scripting Bridge. Together, this means you can call AppleEvents (the interprocess communication layer that underlies AppleScript) from a Ruby script, and that script can also access the rest of the ObjC APIs.
Getting started was pretty straightforward. There's a good overview on Apple's developer site which gets you going, and I lifted a small piece of code from Tom Insam's Shelf that allows you to see all the methods you can call on an ObjC object (which, handily, includes ones you can fetch from AppleEvents). (It's the dump routine from extractor.rb.) Pretty rapidly I could loop through my iPhoto library, pulling out dates and keywords, and I've had enough experience with rflickr to do the same with my remote photos, once I'd applied a couple of strategic patches. A bit of hash-munging and I had two data structures I could use to find matching photos (by datestamp) and tag them.
Unfortunately, this is where I hit a big speedbump. iPhoto 7's keyword adding interface has improved massively, but not, unfortunatly, from the scripting side. It's impossible to add a keyword that's not already defined, and even adding one that is requires that the photo be highlighted in the app. This is horrific - it potentially means that the user can interact with the program between the selection and the tagging and muck things up.
I also ran into what seemed to be a Scripting Bridge bug. Despite hitting the right syntax, the AppleEvent sent by Ruby (and Python - I tried both) ended up not working. I looked at the debugging output from all three, and it seems as if only AppleScript and Script Editor send the right parameters for a call that doesn't have a return value. In the end, I dropped back to using a shell call to osascript. Sigh. Maybe I'll change to using filesystem metadata.
Thankfully, tagging at the Flickr end was far easier (although their use of space-delimited tags means there's an annoying amount of faffy quoting), and now I have a script that will find the most recent 100 photos on Flickr, and see if they're in iPhoto. If so, it'll add the "flickr" tag to them (if necessary), while Flickr gets two machine tags (so they don't scare real people), one with the original file name and the other with the location of the current iPhoto image. (This isn't technically the same as the image that was uploaded, unfortunately, but it's still useful.)
The script is currently very much "programmer quality" - you need to be able to get a Flickr API key and know where to edit it into a Ruby script, plus it has a few caveats (as I said, it'll only do 100 photos, and it's also going to get confused if you have more than one image a second) - but if you're interested, feel free to download it and give it a go. (You can see the Flickr-side machine tags on most of my recent photos.) If you're at all interested in scripting applications, it might have a few useful tips, too.
So, what have we learnt?
- Flickr's API is pretty cool. Ruby's Flickr libraries are bitrotten.
- Scripting Bridge and language support in 10.5 is pretty cool too.
- Unfortunately, it's not entirely bug free, and the bugs are baffling.
- Some applications have rubbish scripting definitions.
- Tom Insam is full of useful hints for programming.
I think that'll do for one weekend.
The idea of Hackday London 2007 was, unsurprisingly, to hack. Beforehand I'd had little idea of what to do, but candace managed to come up with a few ideas. Notably, one evening last week she noticed some photos on SpaceWeather.com of the International Space Station, as taken from the Netherlands, and thought that perhaps we'd have a chance of seeing it. We checked Heavens-Above, a venerable satellite tracking/prediction site, and we caught a flyby which included a moment of brightness as the newly-deployed solar panels caught the sun.
Wouldn't it be great, she mused, if it was possible to get messages to your phone when such things were going to happen? As well as ISS flybys, there are also Iridium flares, where the redundant communications satellites reflect sunlight down to the ground, and it'd be nice to be told about those, too. It looked like we had an idea.
Implementing the idea wasn't terribly tricky, either. There are two parts to it. Firstly, there's a scraper for Heavens-Above. We set up a special London account, and wrote a script that authenticates against the site, and downloads and parses the data tables for the ISS and iridium flares. This goes into a plain text file, with the date and time as one field and the text message to send as the next. Since the data tables list events for seven days in advance, this script doesn't have to run frequently- at the moment it's doing so once a day.
Secondly, there's the sender script, which runs every five minutes. It reads in the data file, parses the date (slightly hackily- I'll need to fix that eventually), and, if the event is within twenty minutes, sends it to Twitter (which we use as it's a simple way of sending SMSes to multiple users). Also - and this is where where the required use of a BBC or Yahoo! API comes in - the script checks the Yahoo feed's "current weather conditions" value, and if it's likely you'll be able to see the event, continues onwards to send it. Otherwise, it doesn't bother (but I do get an email from cron telling me what the weather actually was).
I was able to get all the coding done and put it on my colo before one of the flares that evening, but sadly the weather wasn't quite clear enough and we didn't have visibility in the right direction. Still, we had text messages and a Twitter page that we could point to as proof of a working hack, so we went home. (There's an aside here about the difference between the SF culture and London's more lackadaisical one, perhaps, but it'll have to wait for another day.)
I spent another 30 minutes on Sunday morning tidying up the verbosity of the script (it now only prints, and hence sends email, at the same time it has output to send), and then came the slightly nervewracking presentation, which thankfully seemed to go down well, despite us having nothing really in the way of UI to demo. (One of the best things about Hackday- you don't have to write up yourself...)
That evening I added a feed for SF (and took the chance to comment alongside all the bits of code that needed changing). If you're interested in getting messages for passes Above London or Above SF then get a Twitter account and follow the appropriate user. (Longer-term Twitter users might want to adjust their phone notification settings if they want to get SMSes late at night.)
What's next, then? Well, I've since looked at a Perl module (Astro-satpass) that would have let us cut out Heavens-Above, and possibly opened the door to more customised notifications. In particular, we've made some arbitrary decisions (we don't send flares that don't climb above 20°, for example) and it's really hard to add a new location. It'd be nice to remove those limitations, but doing so introduces a new problem; namely, Twitter is a very easy platform for notifications, although I'm concerned about its reliability and timeliness. Customised messages mean either abandoning it or (ab)using the direct messaging syntax.
It was notable that Twitter was used in a fair amount of the hacks (from the live blog post written during the presentations, at least 10%). I think it'd be a perfect fit for Yahoo, alongside Flickr and del.icio.us, as a developer-friendly site that, perhaps, needs a bit of resourcing behind it to make it truly reliable (and, perhaps, more international; the UK number isn't always cheap). How about it?
Anyway, the two applications/accounts/bots should now run without human intervention (at least until that date hack I mentioned rears its head around Christmas), and hopefully I'll remain inspired to tackle the more complex project of personalised feeds and notifications later in the summer. For now, enjoy spotting satellites.