codahale.com٭blog

Coda Hale lives in Berkeley, CA, where he writes about Ruby on Rails, usability, web design and development, and the occasional bit about bicycles.

Wesabe API vs. Quicksilver

As you may have noticed, we at Wesabe just released an API allowing you to access your financial data on your own terms.

So what do I do with APIs? I write little Quicksilver Applescripts for them. So here’s how you get a listing of your bank accounts with their current balances via Wesabe through Quicksilver.

The Script

(* Wesabe API Thingy for Quicksilver
 * (c) 2007 Coda Hale <coda.hale@gmail.com>
 * MIT License
 *)

--- pull out the username and password for a website
on getCredentials(serverName)
    tell application "Keychain Scripting"
        set ourKey to first Internet key of current keychain whose server is serverName
        set ourCredentials to (account of ourKey & ":" & password of ourKey)
        return ourCredentials
    end tell
end getCredentials

--- shell out to curl to download the accounts listing in XML
on downloadAccounts(credentials)
    set curlCommand to "/usr/bin/curl --silent --show-error --user " & quoted form of credentials & " https://www.wesabe.com/accounts.xml"
    set results to do shell script curlCommand
    return results
end downloadAccounts

--- use System Events to parse the XML and return a list of account names and balances
on getAccountBalances(xmlData)
    tell application "System Events"
        set accountBalances to ""
        set xmlDocument to make new XML data with data xmlData
        set accountElements to XML elements of XML element "accounts" of xmlDocument whose name is "account"
        repeat with accountElement in accountElements
            set accountName to value of (XML element "name" of accountElement)

            if (XML elements of accountElement whose name is "current-balance") ≠ {} then
                set accountBalance to value of XML element "current-balance" of accountElement
            else
                set accountBalance to "(none)"
            end if

            set accountBalances to accountBalances & accountName & ":  " & accountBalance & "
"
        end repeat
        return accountBalances
    end tell
end getAccountBalances

--- actually execute this stuff
set xmlData to downloadAccounts(getCredentials("www.wesabe.com"))
set balanceText to "Your Wesabe Accounts:
" & getAccountBalances(xmlData)

--- and put it on the big screen
tell application "Quicksilver"
    activate
    show large type balanceText
end tell

Installation

Toss the above into Script Editor, and save it as an Application someplace where Quicksilver will pick it up (I called mine “Wesabe Accounts”). You should have an entry in your Keychain for wwww.wesabe.com — that’s where the script reads your username/password.

Using The Damn Thing

Run it from Quicksilver. It’ll look like this:

Wesabe via Quicksilver

If you want it to do something different, go for it. Add some functionality, put it up on your blog, and add a comment here with a link there.

I’m really interested to see what kind of stuff people make with this.

5 comments »