Using the Android Scripting Environment to find out if a book is in the Library

Being a developer type I couldn't wait to start experimenting with coding on my new HTC Desire. Being Android powered it is very friendly and coding mainly takes place in Java, which I'm familiar with. This class of mobile device is incredibly powerful of course, armed not just with portability and web access but also decent processing power and a variety of sensors. We're still learning about what exactly this means and what opportunities it can bring and so it is very important to be able to experiment, try out ideas and build rough prototypes. This is where the Android Scripting Environment (ASE) comes in, and I used it to build a prototype application to scan a barcode on a book and use that code to see if it is in a library. Handy when deciding whether to buy a book!

First of all it is important to point out that the ASE is absolutely not for making applications for end users. Instead it is a handy way to experiment with the possibilities of your device. In some ways it reminds me a little of the Quickly system on Ubuntu, it has a lower entry barrier in terms of the skills you need to learn to try writing a program than developing a full blown application and has an element of enabling fun programming (not that programming in Java is not fun of course). It also should be a quicker way to get a prototype. You can also write the script on the phone itself. This is a bit awkward in practice, but it can be done.

A little while ago I read the blog post: “Android barcode scanner in 6 lines of Python code” and got intrigued. The script used the camera on the device to scan a bar code on the back of a book and then pass the ISBN number to Google Books to find out information about it. As I work in a university I am lucky enough to have access to an academic library so wondered about adapting the script to search our library's catalogue instead which would be handy in situations where I am about to buy the book. It seems things have changed slightly with ASE since the script was written, so it did not work as it was, but fortunately there were some helpful hints in the comments that provided fixes.

Installing ASE and Python

Strangely, the Android Scripting Environment is not available in the Android Market. It still can be installed with the minimum of fuss though. I got the Barcode Scanner app from the Zxing team from the Android Market. Then from the home screen I pressed the Menu key then SettingsApplications and ticked the Unknown Sources entry. This allows you to install apps from places other than the market. Now go into your applications menu and start up the Barcode Scanner. You should be able to scan the barcode on the ASE project page (, when you open this URL it will install. When you have ASE installed, you should untick the Unknown sources entry to keep your phone secure.

You should now be able to start up ASE from your applications menu. When you do you will notice things look a bit sparse, this is because ASE only ships with a Shell interpreter, you have to tell it what other interpreters you would like to install. To install Python press Menu then ViewInterpreters. Only Shell will appear in this list at the moment. Click Menu again then Add and select Python. The new interpreter will now be downloaded and you will shortly be ready to write some scripts in this great language.

Is that book in my library?

A lot of libraries have on line catalogues that can be searched over the web. You can enter many bits of information to find a book, but one that is especially useful is the ISBN code. Why? Because this is the code you get when you scan the barcode on the back of the book. You could adapt the script to search any catalogue online that accepted ISBN codes. Note that this script is not meant to be an exhaustive way to search you library, it is more of a use case for ASE. If you chat to a Librarian you might be able to get a different edition of the book or something very similar – matches that will be missed with this technique.

The script works by invoking the bar code scanner function of ASE. This function return a structure that contains the ISBN from the scanned bar code which is extracted this is inserted into the template URL to replace the “%s” characters and this is passed to a web browser. To establish this URL I did a manual search on our catalogue and noted down the URL generated to get the query results.

The final script is here:

# -*- coding: utf-8 -*-
import android
droid = android.Android()
(id, result, error) = droid.scanBarcode()
isbn = str(result['extras']['SCAN_RESULT'])
url = "^*&searchType=0" % isbn
droid.startActivity("android.intent.action.VIEW", url)

With only a slight modification to a code sample I found on the web I could create a short script that could potentially help me utilise the library I am a member of much more effectively. It also lets me try out an idea quickly to see if the concept is any good before needing to commit to writing a full end user targeted Java application. ASE is great for experimentation and having a bit of fun with your device. Is it ok to have fun while programming? Absolutely.


Hiah Liam, I did something similar when I was experimenting with LibraryThing - but to be honest I didn't quite get the seamless experience I was hoping for :-)

It looked like you could save a script shortcut to the home screen, but then the launch process for ASE took a while, and then you had to hack a URL to persuade LibraryThing to accept the ISBN and add to your account with as few steps as possible (having saved sign in details).

It would certainly be interesting if database holders made this process easier - and I have to say LibraryThing was more successful than Google Books for adding to an account...

Some of this I guess was due to a slightly underpowered Vodafone HTC Magic (!) - so will try again on the Nexus One soon I think :-)

I was going to mention LibraryThing as well - but in the context of 'ThingISBN' which would allow you to take the ISBN you've scanned and find the ISBNs for other editions etc - so that if you are scanning from the hardback you could then get the ISBN for the paperback, and include that in your search (free for non-commercial use)

Rhodri - the guy behind LibraryThing is pretty open to suggestions so it might well be worth talking to him - @librarythingtim on twitter.

Add new comment

Comments are always very welcome, but please note the following:
  • Sadly due to the high number of spam comments recently all comments are now manually moderated. You comment will therefore not appear on the site instantly.
  • Comments on this web site are monitored for spam using Mollom. By posting a comment, you accept that your message and other personal details about you will be analysed and stored for anti-spam and quality monitoring purposes, in accordance with Mollom's privacy policy.
  • Please use your own name not a company or website name to submit comments. Your comment will be removed if you don't do this.
  • All links in comments will be marked with a no follow attribute. That means posting a link to your site here won't help your search engine rankings.
  • By submitting a comment you agree that your comment can be reproduced under the same licensing terms as the rest of the content on the site.
  • Comments can be removed at any time without explanation, but won't be removed just because you disagreed with something I said.