March 7, 2009

Scripting the Apple

As I mentioned yesterday, I wanted to find a solution for creating dynamic signatures for on my Mac. Of course, there’s a little bit more to the story. On OS X 10.5 at least, I couldn’t even seem to create a styled signature because the interface for creating a signature appeared to be very bare. There were no hyperlink, bold, italic buttons to stylize the text. However, I learnt that you could style a signature quite easily by creating the text in a mail message itself (or in some other HTML or rich-text editor) and then copying and pasting the text into the signature box 🙂

So I had a fairly OK looking signature with links to my site and to my book. Now all I needed was a way to dynamically add a witty quote to it each time I sent out a mail. I did look at a couple of Mac signature generators but those which were available simply created a sig that had to be copied and pasted into your e-mail manually and that was way too much work for me :p There were a couple of scripts which were supposed to automate the work but the sites to download those scripts appeared to have disappeared into the Internet graveyard a long time ago. So I was out of luck 🙁

Unless of course, I wrote my own solution 🙂 It was when researching how to create my own solution that I was first reminded of fortune . I’ve used this little mod in various forms over the years and once I was reminded of it, I knew I could install this under OS X and get it to provide the witty sayings for me. And that was where I came across Fink 🙂 The quaintly named (they say the name comes from the German name for Finch because finches had a connection to Darwin :p) Fink is a package manager for OS X which allows you to download and install *nix applications which had been modified to run under OS X. Fink comes with a GUI named Fink Commander, which allows you to do most of your package management using a graphical user interface.

But enough of Fink. Suffice it to say that I used Fink and was able to install fortune on my Mac and had it working fine. But now I needed Terry Pratchett quotes. I already had a file full of quotes that I had been using for ages to generate signatures but they weren’t in a format suitable for use by fortune. Fortunately, (pun not intended, honest :p) I was able to find a collection of quotes already in the right format. Once installed, I had my fill of Pratchett quotes to choose from 🙂 (I must mention here that most sites tell you only the Linux file system location to copy the quote file to. If you are on Mac and had used Fink to install fortune, you might be slightly puzzled as to where the file went. As I was. The correct location is /sw/share/fortunes – just something to keep in mind :))

Once all that was in place, it was just a matter of writing a script using AppleScript which would activate, create a new outgoing message and add a signature to that mail using both a static part (links to my site and my book) and a dynamic part (the Pratchett quote output from fortune). At first, I was simply going to generate the signature on the fly and add it to the mail message. But that was where I ran into problems 🙂

I had my pre-created static signature in showing up as nicely formatted HTML output. But when I put in HTML code via my script in to the body of the mail message, the output wasn’t formatted – it was simply HTML code! And of course, I didn’t like that 🙂

So I tried creating a signature via AppleScript. I was able to create a new signature and populate it with the dynamically generated content but that signature, when used, still came out as HTML code and not formatted text. So that was out.

My final solution was a slightly cumbersome one but one that worked 🙂 I basically created the outgoing e-mail message, and then set it to a pre-existing formatted HTML signature. I also copied the quote generated by fortune to the clipboard. Then I had my script move the cursor to the very end of the message so that it was below the signature and then pasted in the fortune. Voila! dynamic signature 🙂

I then used iKey to set up a hoteky so that I could simply run the script from my desktop any time I wanted to create a new e-mail with a dynamic signature. Simple as that 🙂

Of course, there’s one scenario that the above doesn’t cover – when I reply to an existing mail. Then I’d only get the static part of the signature. So what I did there was to create a second version of the script which didn’t create a new outgoing mail message. It simply activated thus bringing whatever window was open already to the front, and then moved the cursor to the end of that window and copied in the quote generated by fortune – problem solved 🙂

If anybody is interested, here’s the final script that I came up with. (The version I provide is the full version but it should be simple enough to modify it to get the secondary version …):

tell application "Mail"
        copy return & return to myContent
        get do shell script "/sw/bin/fortune discworld"
        copy "--" & return & ¬
                the result & return to theFortune
        set the clipboard to theFortune as text
        set mySig to signature "Signature #1"
        set myMsg to make new outgoing message
        tell myMsg
                set the content to myContent
                set visible to true
                set message signature to mySig
        end tell
        tell application "System Events"
                keystroke tab
                keystroke tab
                keystroke tab
                keystroke (ASCII character 31) using command down
                --keystroke "This is where it should go"
                keystroke "v" using command down
        end tell
end tell

The above seems fairly straightforward to me (now) but if anybody wants any clarifications as to any part of the script, I’d be happy to help 🙂

Tags: , ,
Posted by Fahim at 10:25 am  |  No Comments