A Robot Completed My Inventory: Fun with AppleScript

June 4th, 2009 by Brian Leave a reply »

Granted, a human had to go to the shelves and scan 13,822 barcodes.

Another bonus for the human: the ability to generate creative code.

When I scanned a section of my library into my inventory, the scanner saved all of those barcodes as a text file. But here’s a mistake I had made (did I mention 13,822 barcodes?): I had closed all of the previous librarians’ inventories. It asked me what to do with books I hadn’t inventoried yet. I said to mark them as lost, since when I manually scanned a book in, our catalog responds, “This copy had been marked as lost, but will now be found” (or something to that effect). After scanning all of the books, what was left would be truly lost books.

Little did I know that when you use a portable scanner to store up a list of barcodes, when you import that list it says that lost books had now been found, but didn’t update the copy status (they’d still be considered lost until someone hand-scanned them in)(even though I just got done scanning them with a portable scanner).

Since it was already 9pm, I didn’t want to type in 3,000 barcodes by hand.

This morning I wrote my first script in AppleScript. What makes AppleScript different from other programming languages is that it is set up specifically to automate programs that you already have.

I wrote a program that would check the file from the scanner, type in a barcode, press enter, and move on to the next line and repeat the process (like a robot typing in each barcode, line by line).

I set it up so that you run the code, choose the text file holding your barcodes, and then have time to click on the place in your catalog where you type in for books to be checked in. You have to manually click on that text box in your catalog at the start, but I set it up like that so that if your catalog wasn’t set up exactly like mine, you could still use my script.

Here’s the script:
-- variable for carriage return/enter key
set CR to ASCII character of 13

set the source_file to choose file with prompt "That freaking file is located here:"
--give me time to switch back to the circulation
delay 5

-- get file data
set fp to open for access source_file
set barcodes to read fp
close access fp

-- types each line in and presses enter/carriage return
set single_barcode to every paragraph of barcodes
set AppleScript's text item delimiters to ""
repeat with thisBarcode in single_barcode
tell application "System Events"
keystroke thisBarcode & CR
delay 5
end tell
end repeat

Feel free to run it in your AppleScript editor (located in Applications on, I think, every new Mac). I have not found something like this on a Windows machine, but I haven’t really looked.

I’m a Mac fan because my MacBook has sped up my workflow considerably, and this is another example adding to my fanboy-ness. The Apple language is very easy to understand. I saved myself hours just by creating a script this morning.

Advertisement

Leave a Reply