To keep track of my ever-growing to-do list, I am using a fabulous little application called "Things". And most of my work-related to-do items are bugs in Mozilla's bugzilla bug tracker.
So, me being a geek and all, I quite naturally wanted to integrate the two and wrote a little AppleScript that asks the user for a bugzilla.mozilla.org bug number, obtains its bug title, and makes a new to-do item for it in Things' Inbox folder.
The script is available as a gist on github. Click here to download it.
If you look at the code, you'll notice that I went ahead and embedded some Python code to the script to do the heavy lifting. The problem with AppleScript is not only that it has a hideous syntax, it also completely lacks a standard library for things like downloading websites and regex-parsing strings. Let's look at it a little closer:
set bugtitle to do shell script "echo \"" & bugzilla_url & "\" | /usr/bin/env python -c \"
import re
import sys
import urllib2
bug = urllib2.urlopen(sys.stdin.read())
title = re.search('<title>([^<]+)</title>', bug.read()).group(1)
title = title.replace('–', '-')
print title
\""
set bugtitle to do shellscript ""
means, assign whatever this shell expression returns to the variablebugtitle
. This way, we just need toprint
our final result to stdout and keep using it in AppleScript.echo \"" & bugzilla_url & "\" | /usr/bin/env python
feeds some input data into the Python script through stdin. We read that a few lines later withsys.stdin.read()
. Another method, especially for more than one input values, would be command-line parameters, all the way at the end of the Python block (after the source code).- Finally, in
python -c \"mycode\"
the-c
marks an inline code block to be executed by the Python interpreter. Other languages, such as Perl, PHP, or Ruby, have similar operating modes, so you can use those as well.
If you want to install the Things-Bugzilla AppleScript, make sure to download the entire Gist as it also contains an install script for your convenience.