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.