Tuesday, June 3, 2008

Applescripts for Delicious Library 2

Here are a few applescripts I threw together for Delicious Library 2. Applescripts go into ~/Library/Scripts/Applications/Delicious Library 2/.

1) Mass import from a list of ISBNs.


-- Menu Path: [2], "Import", "Import Scripts"
-- Get a file name
tell application "Finder"
set isbnList to paragraphs of (read (choose file with prompt "Pick text file containing ISBNs to import"))
end tell

-- Statistic variables
set numIsbn to 0
set existingIsbn to 0
set lookedup to 0

tell first document of application "Delicious Library 2"
set isbnListRef to a reference to isbnList
repeat with isbnItem in isbnListRef
set numIsbn to numIsbn + 1
-- Check to see if book with this ISBN is already in the library
if (every book whose isbn is isbnItem) is {} then
set lookedup to lookedup + 1
-- DL2 call to look up ISBN on Amazon
look up isbnItem
-- Delay is to prevent flooding DL2s look up mechanism
delay 0.01
else
set existingIsbn to existingIsbn + 1
end if
end repeat
end tell

-- Report stats
display dialog "Done: " & numIsbn & " ISBN in list. " & lookedup & " ISBN looked up. " & existingIsbn & " ISBN already in DB."


I set this up to let me import my OpenDb list. In this case, the script will be located under the File|Import menu.
Use the "Export my Items" action in OpenDb. You will need to specify Books specifically to be able to export ISBNs.

2) Renaming titles based on series and number in series fields.


tell first document of application "Delicious Library 2"
set selectedItems to selected media
if selectedItems is {} then
display dialog "No entry is selected. Run on all entries in the library? (This may take a while.)"
set bookNames to every book whose series is not missing value and number in series > 0
else
set bookNames to selectedItems
end if
set bookNamesRef to a reference to bookNames
repeat with bookItem in bookNamesRef
set bookName to name of bookItem
set bookSeries to series of bookItem
set bookNumber to number in series of bookItem
if bookSeries is not missing value and bookNumber > 0 then
set name of bookItem to bookSeries & ": v. " & bookNumber
end if
end repeat
end tell


I collect lots of manga which tend to run multiple volumes. Unfortunately, sorting by title runs into problems when jumping from single digit volumes to multiple digit volumes (v.10 coming before v.2, for instance). Also, importing the information from Amazon can be wildly inconsistent with formatting.

This script will make use of the "series" and "number in series" fields to rewrite the "name" field.

An improvement would be if DL2 added an "on update" script that could automatically run this script when changes are made.

3) Pulling the volume number from a title.


tell first document of application "Delicious Library 2"
set selectedItems to selected media
if selectedItems is {} then
display dialog "No entry is selected. Run on all entries in the library? (This may take a while.)"
set bookNames to every book whose series is not missing value and number in series is 0
else
set bookNames to selectedItems
end if
set bookNamesRef to a reference to bookNames
repeat with bookItem in bookNamesRef
set bookName to name of bookItem
set bookSeries to series of bookItem
set bookNumber to number in series of bookItem
if bookSeries is not missing value and bookNumber = 0 then
set testlist to (a reference to characters of bookName)
set validChars to "0123456789"
set numberFound to false
set foundNums to ""
repeat with i from length of testlist to 1 by -1
if validChars contains item i of testlist then
set numberFound to true
set foundNums to item i of testlist & foundNums
else
if numberFound then
exit repeat
end if
end if
end repeat
if numberFound then
set foundNums to foundNums as number
set number in series of bookItem to foundNums
end if
end if
end repeat
end tell


Of course, filling the "series" and "number in series" fields is a bit of a pain itself. This script will assume that the last number in a title is the volume number and assign that to the "number in series" field.

It will only process items that have a "series" field set. It is fairly easy to batch set a group of items to the same series and then run this script to pull the volume numbers out. Afterwards, I run script 2 to format all of the titles.


These scripts are set to deal specifically with book items but they should work well enough for DVDs with a minor modification.

The last step for me is to figure out a way to reliably and automatically set the "series" field from the "name" field. This is much more difficult due to the inconsistency with how Amazon lists manga though it seems to be more consistent with recent titles.

7 comments:

gclowney said...

Hi! I'd love to be able to use your script to batch import a list of ISBNs, but it doesn't work for me: DL2 adds all the items but fails to look up and fill in any details, so each has only an ISBN.

Also, I'd really like to modify it so it duplicates existing items.

Any chance you might be able to help with this?

Thanks!
Greg

Lewis said...

I second this question--in fact I don't even see where it is adding the ISBNs? It is a great idea--what are we doing wrong?
THANKS
Lewis

jhsu said...

I haven't used this in a while. It's possible that either DL2's Applescript API or Amazon's API has changed which is causing the problem.

To allow duplicates, change the "if (every book...) then" line to "if true then".

The book is added on the "look up isbnItem" line. This is a DL2 command that will send the ISBN to Amazon and process the result.

I tried it on my machine just now and it seemed to work. I don't really have any ideas why it might not work for you. Sorry.

Lewis said...

Thanks for your reply--I'm using a TextEdit file of ISBNs, not the OpenDb program--but I'm assuming that shouldn't make a difference? It's one ISBN per line, something like this:
4011222030108
018111000178
0132281279
660200307721

THANKS
Lewis

jhsu said...

I put those into a text file and DL2 loaded 3. Amazon wasn't able to find 4011222030108.

It found the book, DVD and CD for the other 3. I really don't know what the issue is offhand.

I found this thread on getsatisfaction which seems to indicate there was a bug in DL2.1? I haven't tried their method, but I just tested using 2.6.3 v12901.

http://getsatisfaction.com/deliciousmonster/topics/unable_to_import_from_text_file_in_library_2_1

Lewis said...

Oh--I think that last number was wrong! So in that case your script worked fine! THANKS for testing it--I'll try again.
Lewis

clement said...

the duplicate check seems to be not working... not sure why though.. anybody?