ISBN validation for idiots...
ISBN validation for idiots...
- Subject: ISBN validation for idiots...
- From: Ruth Bygrave <email@hidden>
- Date: Mon, 14 May 2007 09:27:37 +0100
I have a barcode scanner and am doing lookup/data entry for my books.
ISBNs come in 2 forms, 10 digits long and 13 digits, each of which
have a checksum digit. The 13-digit one hasn't increased the data
space: it's a '978' prefix to make sure people don't scan the wrong
barcode on the back of the book by mistake. I think Amazon's ASIN
number (unambiguous per-item code identifying a book) is made of the
short one; at least I can often get a lookup on the short one rather
than the long one.
Barcode scanners put in the long number, so it needs the '978'
knocked off the front and the checksum recalculated to be ready to
search Amazon for the ISBN.
I managed to find on the web a description simple enough that even I
can understand it:
For each of the first 9 digits, multiply the digit by its position in
the ISBN - so, multiply digit 1 by 1, digit 2 by 2, digit 3 by 3, and
so on to digit 9 * 9. Add the lot of them up. Take the result mod 11.
This gives you your last digit - with the caveat that if the result
mod 11 is 10, the last digit is X
Then I put it into Applescript and it seems to work as follows:
----------------------
set s to (text returned of (display dialog "Please enter an ISBN-13:
" default answer "")) as string
if s starts with "978" then
set cutString to (characters 4 thru 12 of s) as list
set theNum to 0
repeat with n from 1 to (length of cutString)
set theNum to theNum + ((item n of cutString as integer) * n)
end repeat
set theNum to theNum mod 11
if theNum = 10 then
set theNum to "X" as string
end if
set theISBN to (cutString as string) & theNum as string
set s to theISBN
end if
return s
------------------------
At this point I remembered that the widget I tend to use for quick
amazon searches (which I like because it just gives me a radio button
to set which version of Amazon to use and then opens a browser to
search for whatever I type in) is in Javascript, which I don't know.
So I looked at the javascript, which said:
-------------
if (widget)
widget.openURL(url + s)
-------------
Even I can nearly figure it out, I thought, and I changed it to:
------------
if (widget)
s=s.isbn10(s)
widget.openURL(url + s)
------------
and, after a lot of Googling on 'for loop' and 'string functions',
put this function at the bottom:
----------------
function isbn10(s)
{
if (s.substring(0,3) == "978")
{
s=s.substring(0,11);
var theNum = 0;
for (var n = 1; n <= 9; n++)
{
var newNum = s.substring(n,n++) as integer;
theNum = theNum + (newNum * n);
}
theNum = theNum mod 11
if (theNum == 10)
{
var checkDigit = "X"
}
else
{
var checkDigit = theNum as string
}
return s + checkDigit
}
----------------------------------------------------
At which point the widget was broken, so I had to reload it from the
original, thinking unappreciative thoughts about Scary Languages That
Look Like C and This Is Why I Prefer Applescript.
Is there a simple way to glue the Applescript to the widget, or do I
just have to wait a year or so until I buy Leopard and get Dashcode?
Or can anyone recommend a Really Really Simple Book On Javascript
which isn't too Windows-biased (I Googled "O'Reilly" and
"Javascript", but couldn't seem to find any 'beginners' ones)?
Regards, Ruth
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden