Doin’ stuff automagically

I’m not sure if this will be interesting to anyone else but myself, but the process I used to produce my linked movie list was fairly complicated, and for myself and, perhaps, others, might be useful as an idea of how to get things done on a Mac (although there may be similar tools available on Windows).

I started by compiling the list of movies I had seen last year. Luckily I had the foresight of trying to post about, and tag, all of them. So getting the starting point was as simple as clicking that link.

I wanted the list for my year-end (year-beginning, I suppose) post to have each title linked to it’s Internet Movie Database page. But when I contemplated doing a search for each one, by hand, and copying-and-pasting each URL, and writing out each link with all its tedious a href="" and angle brackets and whatnot.

Now, I use TextExpander to save me steps when typing out common phrases, and especially when blogging and typing out links. Such genius. I’ve long used similar programs going way way back; they run in the background, and when they detect a specific key combination, the program automatically expands that into the actual phrase. For instance, if I typed ahreff, TextExpander substitutes the code for a link, and moves the cursor to the right spot for me to type out the actual URL.

But that wasn’t gonna cut it for a list of 50 movies. Even saving me a few steps, it would take forever, and with the added pain of having to copy and paste from my browser into another program… no. There had to be a way to automate it.

If I were a programmer, or better at shell scripts or scripting languages like Perl or PHP, I would spend a small amount of time writing a program to do all this for me. Alas, I’m a power user, but not a programmer. I have written small shell scripts, but I thought this was beyond me, at least now.

I could conceptualize the basic process, which is the first step:

  1. Take list of names, one by one, and:
  2. convert the name into a search term, then
  3. use that search term to find the IMDB page
  4. copy that resulting URL, then
  5. construct the link text.
  6. And insert it around the movie title in a new text document.
  7. Repeat for each item in the list.

Luckily, I discovered that there’s a way to create a URL that will force Google to give it’s top hit for any search term, also known as “I’m feeling lucky”. I don’t remember where I saw it first, but this page describes the technique.

I thought that if I could figure out a way to insert the movie name into a Google “Lucky” URL, then having some way to automate opening up a bunch of those URLs would give me the list of IMDB addresses I needed.

Then it would just be a matter of creating the whole text that represents the linked movie name.

Not being a programmer, I still had an awesome tool to work with: Apple’s built-in Automator. It’s a GUI program where you drag and drop different modules representing each step of a workflow. It’s beautiful for repetitive actions, though it has its limits. And it was perfect for me. Or so I thought. It actually took two different Automator workflows to get what I needed.

First, I had to do a bunch of search-and-replace on the list of movie names. A URL doesn’t have spaces in it, so I replaced all the spaces with + signs, and took out all the other punctuation. When I tested the Automator workflow on a small sample, I realized that some movies were so popular that some other site, not the IMDB page, showed up as the first hit. So I explicitly added +imdb to each movie name, just to be sure. Then I turned each one into a link with another set of search-and-replaces.

Next step was to open the resulting list of links in Safari, and turn the first Automator action on it. This action had three steps:

Get Current Webpage from Safari -> Get Link URLs from Webpages -> New Safari Document.

This opened 50 new windows, in alphabetical order. Then I used the next workflow I’d figured out, which consisted of:

Get Current Webpage from Safari -> Copy to Clipboard (which copies the results of the previous action, or the IMDB address I needed) -> Set Contents of TextEdit Document (by appending) -> Run AppleScript (which was tell application "Safari" to close the front window" – an important step for working back through the 50 windows) -> Loop 51 times.

I actually had to reverse-sort the original list, because once I did the second step, I had a list of addresses with no way to tell, without opening them in a browser again, which movie they represent. IMDB uses a string of numbers for their webpages, not something human-readable, like, say, Wikipedia. But using Wikipedia had it’s own problems, which I’ll leave as an exercise for the reader.

The final step took me a bit to come up with, but was pretty simple once I’d figured it out. Again, a programmer could write code that would string together each part of the link and spit out a fully-formed URL: front-half-of-IMDB-link + Movie Title + closing-tag.

What I did, instead, was copying each list into a spreadsheet (I used Google’s free online one), each type to a column, then exported it as a text file. A quick search-and-replace to remove all the tabs from the tab-delimited list, and ta-da! I had my linked list.

I think, now, that I know enough to write a simple AppleScript to run each Automator workflow, but I’m not sure how to automate the final step. Still, it’s a significant savings in time and effort.

If you start noticing lots of linked lists on my blog, now you’ll know why.