Sync eReader (Sony, Kindle, etc.) with iTunes

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

This is a re-post from my hint at http://hints.macworld.com/article.php?story=20110413143511742.

In this hint, I describe the process I use to sync my Sony PRS-350 eReader with iTunes and the iBookstore. NOTE: this does not circumvent any DRM for iBookstore purchases. For simplicity, this hint assumes that books/PDFs import to the device are DRM-free, or otherwise viewable on your device.

First, some facts:
Sony eReaders, the Kindle, and many other eReaders are mounted to OSX as a regular external volume. The user can drag and drop compatible files and they will appear in the reader's library when ejected.
If the user has not opted to manually mange their files, iTunes stores imported eBooks in the ~/Music/iTunes/Books/ Directory.
The Unix tool rsync can synchronize two directories.
The Unix tool diskutil can mount and eject external volumes.
Many third party tools, such as Hazel can perform script actions when an external volume is mounted/ejected.


I make use of these four facts in this way:

I plug in my eReader.
Hazel recognizes the volume, begins rsync between iTunes and the eReader's book directory.
When rsync completes, diskutil ejects the reader (so I can remove it safely).
Lastly, a Growl alert is sent indicating sync completion.
Here are the scripts. Each of these will require some customization for your particular reader:


To Sync:
Code: Select all
rsync -va --delete /Users/YOUR USERNAME/Music/iTunes/Books/ /Volumes/READER/database/media/books/
To Eject (in the case of the Sony PRS-350, there are two partitions mounted that need to be ejected):
Code: Select all
diskutil unmount `disktool -l | grep 'READER' | sed 's/.*\(disk[0-9s]*\).*/\1/'`
diskutil unmount `disktool -l | grep 'SETTING' | sed 's/.*\(disk[0-9s]*\).*/\1/'`

For Hazel:

Code: Select all
Add a the /Volumes directory for a 'Folder with name YOUR EREADER VOLUME NAME.'

For the action, add a shell script containing the scripts above.

The full rule looks something like this:

Image

I also have a Hazel rule that imports to iTunes all *.epub files (and other reader files) that appear in the downloads directory. This action also includes a script to re-mount the eReader upon a new import. Simply change the diskutil script above to read 'mount' instead of 'unmount.'

Now, this works great if you do all your book management in iTunes, but I don't. I will do book additions through iTunes, but book deletions I do on the device. This is where a second script comes in.

First, I found a file on the Sony reader that holds the information for deleted files. I wrote a shell script to translate these paths into paths of files to delete on my Mac.
Code: Select all
#!/bin/bash
clear

DELETE_LIST=$(awk -F"\"" '{print $2}' /Volumes/READER/database/sync/deleted.xml | grep "/books" | sed "s/database\/media\/books/\/Users\/andrewfreyer\/Music\/iTunes\/Books/g")

echo "$DELETE_LIST"


Now, I delete all those file from the iTunes Books folder. However, there's a problem because iTunes does not automatically removed "bad files" from the library. Instead, it puts an exclamation mark next to the book. The following script removes files with exclamation points:

Code: Select all
set lineData to paragraphs of (do shell script "sh /Applications/Utilities/AppleScript/RemoveDeviceDeletedFiles.sh")

try
   do shell script ("rm \"/Volumes/READER/database/sync/deleted.xml\" > /dev/null 2>&1")
end try


repeat with thisLine from 1 to count of lineData
   set currentPath to item thisLine of lineData
   try
      do shell script ("rm \"" & currentPath & "\"")
   end try
end repeat

set removeTotal to 0

tell application "iTunes"
   set mainLibrary to library playlist 1
   set totalTracks to count of file tracks of mainLibrary
   with timeout of 10 seconds
      repeat with t from totalTracks to 1 by -1
         try
            set this_track to file track t of mainLibrary
            if this_track's location is missing value and this_track's duration is missing value then
               removeTotal = removeTotal + 1
               delete this_track
            end if
         end try
      end repeat
   end timeout
end tell

if removeTotal > 0 then
   #display dialog "There were " & removeTotal & "book(s) removed from the iTunes library"
end if



Run this iTunes script either before or after the rsync.

Once this is set up, it's truly awesome.
Last edited by a_freyer on Sat Apr 16, 2011 6:41 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Sync eReader (Sony, Kindle, etc.) with iTunes Sat Apr 16, 2011 6:22 pm • by sjk
a_freyer wrote:
Code: Select all
#!/bash/bin

FYI, that should be:
Code: Select all
#!/bin/bash
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Ah yes, silly mistake. Corrected.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado


Return to Tips & Tricks - DO NOT POST QUESTIONS

cron