-3.8 C
United States of America
Thursday, December 5, 2024

applescript – How do I gather all of my notes and highlights from iBooks?


I’ve written a script for this objective that extracts the notes out of your Mac and outputs Evernote export information, prepared for double clicking. Maybe you possibly can modify my script if it would not fit your functions exactly.

Briefly, it reads the SQLite databases in:
./Library/Containers/com.apple.iBooksX/Information/Paperwork/BKLibrary
./Library/Containers/com.apple.iBooksX/Information/Paperwork/AEAnnotations

… and on this case, exports them to Evernote’s .enex format.

https://github.com/jorisw/ibooks2evernote/

    <?php
    /*
     *  iBooks notes to Evernote converter
     *  by Joris Witteman <[email protected]>
     *  
     *  Reads the iBooks Annotations library in your Mac and exports
     *  them, tagged with their respective e-book title and imported in
     *  separate notebooks.
     *
     *  Utilization:
     *  
     *  Transfer this script to the highest of your private dwelling listing in your Mac.
     *  That is the folder that has your identify, which the Finder opens for those who
     *  click on on the Finder icon within the Dock.
     *
     *  To export your notes to Evernote:
     *  
     *  1. Run the next command within the Terminal:
     *
     *     php ./ibooks2evernote.php
     *    
     *  2. Open the newly created "iBooks exports for Evernote" folder out of your
     *     dwelling folder, open every file in there, Evernote will open and begin 
     *     importing your notes.
     *
     */




















    // Default file areas for required iBooks information 
    outline('RESULT_DIRECTORY_NAME',"iBooks exports for Evernote");
    outline('BOOKS_DATABASE_DIRECTORY','./Library/Containers/com.apple.iBooksX/Information/Paperwork/BKLibrary');
    outline('NOTES_DATABASE_DIRECTORY','./Library/Containers/com.apple.iBooksX/Information/Paperwork/AEAnnotation');


    if(file_exists(RESULT_DIRECTORY_NAME)){
        die("The vacation spot folder for the exports already exists in your Mac.nPlease transfer that one out of the best way earlier than continuing.n");
    }

    // Confirm presence of iBooks database

    if(!file_exists(BOOKS_DATABASE_DIRECTORY)){
        die("Sorry, could not discover an iBooks Library in your Mac. Have you ever put any books in there?n");
    }else{
        if(!$path = exec('ls '.BOOKS_DATABASE_DIRECTORY."/*.sqlite")){
            die("Couldn't discover the iBooks library database. Have you ever put any books in there?n");
        }else{
            outline('BOOKS_DATABASE_FILE',$path);
        }
    }


    // Confirm presence of iBooks notes database

    if(!file_exists(NOTES_DATABASE_DIRECTORY)){
        die("Sorry, could not discover any iBooks notes in your Mac. Have you ever truly taken any notes in iBooks?n");
    }else{
        if(!$path = exec('ls '.NOTES_DATABASE_DIRECTORY."/*.sqlite")){
            die("Couldn't discover the iBooks notes database. Have you ever truly taken any notes in iBooks?n");
        }else{
            outline('NOTES_DATABASE_FILE',$path);
        }
    }


    // Fireplace up a SQLite parser

    class MyDB extends SQLite3
    {
      perform __construct($FileName)
      {
         $this->open($FileName);
      }
    }


    // Retrieve any books.

    $books = array();

    $booksdb = new MyDB(BOOKS_DATABASE_FILE);

    if(!$booksdb){
      echo $booksdb->lastErrorMsg();
    } 

    $res = $booksdb->question("
                SELECT
                    ZASSETID,
                    ZTITLE AS Title,
                    ZAUTHOR AS Creator
                FROM ZBKLIBRARYASSET
                WHERE ZTITLE IS NOT NULL");

    whereas($row = $res->fetchArray(SQLITE3_ASSOC) ){
        $books[$row['ZASSETID']] = $row;
    }

    $booksdb->shut();

    if(depend($books)==0) die("No books present in your library. Have you ever added any to iBooks?n");

    // Retrieve the notes.

    $notesdb = new MyDB(NOTES_DATABASE_FILE);

    if(!$notesdb){
      echo $notesdb->lastErrorMsg();
    } 

    $notes = array();

    $res = $notesdb->question("
                SELECT
                    ZANNOTATIONREPRESENTATIVETEXT as BroaderText,
                    ZANNOTATIONSELECTEDTEXT as SelectedText,
                    ZANNOTATIONNOTE as Word,
                    ZFUTUREPROOFING5 as Chapter,
                    ZANNOTATIONCREATIONDATE as Created,
                    ZANNOTATIONMODIFICATIONDATE as Modified,
                    ZANNOTATIONASSETID
                FROM ZAEANNOTATION
                WHERE ZANNOTATIONSELECTEDTEXT IS NOT NULL
                ORDER BY ZANNOTATIONASSETID ASC,Created ASC");

    whereas($row = $res->fetchArray(SQLITE3_ASSOC) ){
        $notes[$row['ZANNOTATIONASSETID']][] = $row;
    }

    $notesdb->shut();


    if(depend($notes)==0) die("No notes present in your library. Have you ever added any to iBooks?nnIf you probably did on different units than this Mac, be certain to allow iBooks notes/bookmarks syncing on all units.");


    // Create a brand new listing and cd into it

    mkdir(RESULT_DIRECTORY_NAME);
    chdir(RESULT_DIRECTORY_NAME);

    $i=0;
    $j=0;
    $b=0;

    foreach($notes as $AssetID => $booknotes){

        $Physique = '<?xml model="1.0" encoding="UTF-8"?>
        <!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
        <en-export export-date="'.@strftime('%YpercentmpercentdTpercentHpercentMpercentS',time()).'" software="iBooks2Evernote" model="iBooks2Evernote Mac 0.0.1">';

        $BookTitle  = $books[$AssetID]['Title'];

        $j = 0;

        foreach($booknotes as $be aware){

            $CappedText = null;
            $TextWithContext = null;

            // Skip empty notes
            if(strlen($be aware['BroaderText']?$be aware['BroaderText']:$be aware['SelectedText'])==0) proceed;

            $HighlightedText = $be aware['SelectedText'];

            // Cap the titles to 255 characters or Evernote will clean them.

            if(strlen($HighlightedText)>255) $CappedText = substr($be aware['SelectedText'],0,254)."…";

            // If iBooks saved the encompassing paragraph of a highlighted textual content, present it and make the highlighted textual content present as highlighted.
            if(!empty($be aware['BroaderText']) && $be aware['BroaderText'] != $be aware['SelectedText']){
                $TextWithContext = str_replace($be aware['SelectedText'],"<span fashion="background: yellow;">".$be aware['SelectedText']."</span>",$be aware['BroaderText']);
            }

            // Maintain some counters for commandline suggestions
            if($j==0)$b++;
            $i++;
            $j++;

            // Put it in Evernote's ENEX format.
            $Physique .='
    <be aware><title>'.($CappedText?$CappedText:$HighlightedText).'</title><content material><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
    <en-note>
    <div>
    <p>'.($TextWithContext?$TextWithContext:$HighlightedText).'</p>
    <p><span style="color: rgb(169, 169, 169);font-size: 12px;">From chapter: '.$note['Chapter'].'</span></p>
    </div>
    <div>'.$be aware['Note'].'</div>
    </en-note>
    ]]></content material><created>'.@strftime('%YpercentmpercentdTpercentHpercentMpercentS',@strtotime("2001-01-01 +". ((int)$be aware['Created'])." seconds")).'</created><up to date>'.@strftime('%YpercentmpercentdTpercentHpercentMpercentS',@strtotime("2001-01-01 +". ((int)$be aware['Modified'])." seconds")).'</up to date><tag>'.$BookTitle.'.</tag><note-attributes><writer>[email protected]</writer><supply>desktop.mac</supply><reminder-order>0</reminder-order></note-attributes></be aware>';

        }

        $Physique .='
        </en-export>
        ';

        file_put_contents($BookTitle.".enex", $Physique);
    }

    echo "Completed! Exported $i notes into $b separate export information within the '".RESULT_DIRECTORY_NAME."' folder.nn";

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles