Librarians Should Know How To Code – Simple Example


There’s a big push in the library world to get librarians on the coding bandwagon and convince library professionals that an ability to code should be just as much a part of their toolkit as a web browser. That’s fine and everything, but where do you start with such a thing? At what point in one’s library school career should they be presented with this?

function newPopup(url) {
popupWindow =

I mean, what? (For those who are curious, this generates a pop-up window on a clicked link. You can call on it from a web page with even more JavaScript code.)

Linus Torvalds, creator of Linux.

Linus Torvalds, creator of Linux.

Unless you’re a web librarian or something, you’re probably not needing the coding skills to do web stuff. What you might need, more than anything else, is something that automates and handles the most mundane of tasks for you.  As a librarian, do you really need to learn some high level language like C, or something like Perl, to do stuff that you’d have to do yourself, but would rather not? Linus Torvalds, the creator of Linux, once said that “Intelligence is the ability to avoid doing work, yet getting the work done.” I think that wraps up a large section of the use case scenarios for many librarians who have something they need to do, but it’s repetitive, boring, mundane, and yet ultimately necessary. Let me give you an example that actually happened to me yesterday.

Create PDFs, because print is dead.

Create PDFs, because print is dead.

The Boring Job

Okay, so as the Web Content Guy, one of my responsibilities is the library’s Facebook presences. Now then, there’s a county policy in place that says we need to somehow archive those presences, especially interactions that happen on them. I know that sounds odd, and it is, but there’s no way around it now and I have to do it. We’d gotten an email from the State Archive that suggested we create a PDF of the sites and save them somewhere. I can envision the reader’s brows furrowing like corrugated cardboard, and yes, I think that’s a silly thing too.

But I have to do it. It’s my job, dammit.

Okay, so what I’d need to do is bring up one of our Facebook sites, create a PDF, save the PDF, and then bring up the next site, create a PDF, save the PDF, and then bring up the next site….


Look, I’ve got stuff to do, okay?

But, there’s good news! That’s a repetitive task that has you doing the same things over and over again multiple times! Wait, that doesn’t sound like good news at all, does it? Believe me it is. See, there’s something that computers are very good at and that’s repetitious tasks that have to be done over and over again. That means we can automate this, and we can do it in a manner that doesn’t involve a whole lot of computer programming jargon and dogma.

Oh and it’s easy to learn.

Enter AutoHotkey

I’ve talked about AutoHotkey before and I’ve actually written complete apps with it. At one point I discussed how to write an app that monitors a drive-up self check-in machine with nine bin sorter and have it send a text message when the thing fails or goes down. This is something with a simpler flair. Basically, we want to build something that does the things we’d do, so we don’t have to do it ourselves.

That’s actually very much like a machine. We can twist caps onto toothpaste tubes by hand, or we can have a machine do it for us.

AutoHokey is excellent for creating scripts to automate mundane tasks. All you really need to do is sit down, figure out exactly what you’d do to get something done, and then tell AutoHotkey to do that.

For instance, I need to:

  1. Open a browser.
  2. Type a URL.
  3. Wait for the page to load.
  4. Print it to PDF.
  5. Wait for the PDF to render before saving it.
  6. Type in a name for the PDF.
  7. Save it.
  8. Go on to the next page and repeat steps 2 through 7.

No problem then, especially since I know the shortcut keys to do this in my browser (Chrome) and it’s even better that I have Chrome set to print a PDF as a default setting. (Seriously, do I really need more paper?) With the shortcut keys, I never have to fiddle with the mouse. So we can break this down further by saying:

  1. Open a browser.
  2. Type Ctrl L, which selects the URL bar and prepares it to receive a URL
  3. Type the URL
  4. Wait for the page to load.
  5. Type Ctrl P, which prints it. (PDF is already selected, remember?)
  6. Wait for the PDF to render.
  7. Type Enter to trigger the save.
  8. Type in a location and file name for the PDF.
  9. Type Enter to finish the save.
  10. Do that with the next pages.

Or to put it in AutoHotkey parlance:

; Launch Chrome and wait 8 seconds for the app to load
Run "C:Program Files (x86)GoogleChromeApplicationchrome.exe"
Sleep, 8000

; Save PDF of MCLD Main Facebook Page
Send ^l
Sleep, 1000
Send {Enter}
Sleep, 15000
Send ^p
Sleep, 15000
Send {Enter}
Sleep, 15000
Send "c:SocialPrintMCLD-Main-Page.pdf"
Sleep, 15000
Send {Enter}

That’s it. That does it all for me. Let’s take a look at it really quick so you can see how easy it is.

First off, any line started with a semicolon is a comment. It’s not code, but it does tell me what the code does. So the first line is just a comment, that’s all. It tells me that the next line launches Google Chrome and waits for the hard drive to churn and load the progamme fully. How long does it wait? Well, the Sleep command there tells the code to sit tight for a given period of time, in milliseconds, before proceding with the rest of the code. 8,000 milliseconds is 8 seconds. That’s plenty of time to load Chrome, even if I’m loading it cold.

The next bit is the real meat of the matter. The Send command does like you’d expect, it sends keypresses to the operating system, just like as if I was typing on the keyboard myself. Send ^l means “Type Ctrl l on the keyboard” which, you might remember, selects the URL bar in Chrome. Then I have it wait a full second before proceeding. (Sometimes things can go too fast, so I slow them down a touch to make sure the computer has time to respond.)

Send tells the computer to type into the URL bar. You can actually stand there and watch it type it very quickly. Send {Enter} is a little different. See, if I want the script to type a specific key on the keyboard, I might need to enclose it in curly braces, because those braces mean to actually hit the Enter key and not type the word “Enter.” There are similar things for {Tab}, {Backspace}, and so on. So in the end, all I’ve done is tell the computer to type a URL and hit Enter, just like I would do.

Then I have it wait a good fifteen seconds to load the page. That should do it no matter how the connection is behaving at work that day. It never takes that long, but it certainly could. Send ^p, like the ^l means “Type Ctrl p on the keyboard,” which, as many of us know, prints the page.

I have it wait another fifteen seconds because Chrome can be a little slow to respond to that command too. I believe it has something to do with loading its printing engine, discovering connected printers, and all that. Besides, it also needs to render a preview of the PDF. This gives it plenty of time to do all of that. Sending {Enter} tells it to actually trigger the save and I give it time to access the drives, network, and figure out all of the connections. (In this case, that’s not so much Chrome overhead, but Windows.)

Almost there! I tell it again to type “c:SocialPrint|MCLD-Main-Page.pdf” which does include the quotations. That’s a Windows thing. If you keep your path in quotations, it makes it exact and there’s fewer issues, especially when you’re totally AFK for this script.

One more fifteen second wait allows time for the file name to type in fully, any overhead preview crap Windows tries to do goes away, and then Send {Enter} finishes it.

I suppose I could’ve set up a loop that progressively replaced the URLs and updated the file names for each branch’s Facebook page, but as I said, I got stuff to do. I copy and pasted the code and quickly changed the stuff by hand. All I changed was the URL and the name of the file to save to, so that only took five minutes to get all of our branches entered into the list.

Then I ran the script. I found one bug, fixed it, reset, ran it again.

After the fourth branch, I got bored and went for coffee. When I got back, I had all the PDFs in my folder. I verified them, shoved them into a folder, and drank my coffee. I avoided the work, and got the job done, but my intelligence is seriously up for debate. I can run this once a week with a double click, go get coffee, and when I get back it’s all done. Everyone gets what they want, and my workload increased imperceptibly.

That which awaits you at the end of the coding and not doing the work yourself.

That which awaits you at the end of the coding and not doing the work yourself.

Your AutoHotkey Development Environment

You need two things to use AutoHotkey, the programme itself and Notepad. Sure you can write the programme in some funky IDE with syntax highlighting and all that, but I wrote this in Notepad and it worked fine. Check the Documentation on AHK’s site, it’s good and their examples will definitely get you started. I think you’ll find that, not only can you very simply automate some of your boring tasks with this, but you’ll find ways to simplify even more complex stuff as you go. The language of AutoHotkey is simple, easy to learn, and makes sense. Even if you’ve never coded anything in your life, I bet you could make something with it. And if that one something makes your life easier, well, then the knowledge was worth it, wasn’t it?











    1. Nah, I don’t. 🙂 I don’t code that much or that often. Indeed, most of the stuff I’ve written recently are written in anger because “X IS STUPID AND I’M GOING TO AUTOMATE IT!”

      At best, I’m a half-assed JavaScript guy who occasionally dabbles in PHP.

Leave a Reply