codahale.com٭blog

This is my old blog. My current writing is here: codahale.com

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 »

Send As IM = Adium + Quicksilver

I am now officially in the process of making everything I do Quicksilver-friendly. This round: Adium. The Adium plugin for Quicksilver has been dead since ß36, which means we have to click on things to send IMs. Until now.

The upshot? Cmd+Space, period, “Contact name: Message”, tab, S, enter.

First, The Script

using terms from application "Quicksilver"
  on process text im_text
    repeat with im_delimiter_position from 1 to (length of im_text)
      if character im_delimiter_position of im_text = ":" then exit repeat
    end repeat
    set im_contact_name to characters 1 thru (im_delimiter_position - 1) of im_text as string
    set im_message to characters (im_delimiter_position + 2) thru (length of im_text) of im_text as string
    tell application "Adium"
      send (first contact whose (online is true and (display name starts with im_contact_name or UID starts with im_contact_name))) message im_message
    end tell
    return nothing
  end process text
end using terms from

Second, Installing It

Paste the script into Script Editor and save it in ~/Library/Application Support/Quicksilver/Actions as Send As IM.scpt.

Restart Quicksilver (Cmd+Ctrl+Q).

Third, Using It

Open up Quicksilver (either Ctrl+Space or Cmd+Space), and hit period. Type your message in the following format: “Contact name: message body.” (e.g., “Bob: Hey man, what’s up?”). Hit tab when you’re done. Type out as much of “Send As IM” as you need to select the Send As IM action. Hit enter. Done!

Send As IM finds the first online contact on your buddy list whose display name or screen name starts with what you entered as the contact name. If you’re not already chatting with them, it’ll open up a new chat window and send your message. If you’re already chatting with them, it’ll use the existing window.

Ok… now what else do I use the mouse for?

43 comments »

Tweet = Twitter + Quicksilver

Jay wants to know how to post to Twitter from Quicksilver. I don't blame him.

I’m not entirely sure why, but I signed up again for Twitter this morning. Now that it’s not just Tony sending me text messages at 2am about chocolate cake, I can see why people like it. I’ve also been spending some serious time getting geeky with Quicksilver, so I decided to hack together a script to make the two play nicely together.

The upshot? Cmd+Space, period, type your message, tab, tw, enter.

First, The Script

using terms from application "Quicksilver"
  on process text tweet
    tell application "Keychain Scripting"
      set twitter_key to first Internet key of current keychain whose server is "twitter.com"
      set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
    end tell
    set twitter_status to quoted form of ("source=qucs&status=" & tweet)
    set results to do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " http://twitter.com/statuses/update.json"
    -- display dialog results
    return nothing
  end process text
end using terms from

Copy this to the clipboard.

Second, Installing It

Paste the script into Script Editor and save it in ~/Library/Application Support/Quicksilver/Actions as Tweet.scpt.

Restart Quicksilver (Cmd+Ctrl+Q).

Third, Configuring It

If you’re not already using Twitterrific, open Keychain Access and add a new password with the following data:

  • Keychain Item Name: http://twitter.com
  • Account Name: Your email address
  • Password: Your Twitter password

If you’re already using Twitterrific, this password will already be in your Keychain. So just sit tight.

Fourth, Using It

Open up Quicksilver (either Ctrl+Space or Cmd+Space), and hit period. Type out your message, and hit tab when you’re done. Type out as much of the word “Tweet” as you need to select the Tweet action. Hit enter. There, it’s sent.

There is no error checking, so if you’ve got the wrong login info, or if Twitter is down, you’ll never know about it. I guess I’m an optimist. Also, it’s Twitter!

Enjoy!

Update: 1/16/07 Added escaping for the status so that special POSIX characters in your tweet don’t freak out the Bash shell.

Update: 1/20/07 Switched over to --data-binary from -F so that messages which started with @ can be posted.

Update: 1/21/07 Just realized that Quicksilver picks up the script as an action if you put it in the Actions folder, making everything way easier and way less complicated. Yay!

Update: 9/2/08 Mike Keen writes in to say: 

I have been enjoying your Quicksilver/Twitter script. I contacted twitter and added “Quıcĸsıɩⅴεʀ” as a source, so now when people Tweet using your script, it will say “from Quıcĸsıɩⅴεʀ” on Twitter with a link to your blog post. Just change line 3 to:

set twitter_status to quoted form of ("source=qucs&status=" & tweet)

and use the -d CURL option instead of -F

It works like a charm!

I’ve updated the script with his changes. Thanks, Mike!

98 comments »