UPDATE — March 29, 2012 Part of why I share AppleScripts here on Veritrope.com is so that interested people can contribute and help take a project to the next level. When I first published the original version of this script in September 2010 (and, of course, especially in the days after Yahoo announced they were […]

(*
http://veritrope.com
Delicious Bookmark Importer for Evernote
Version 2.5
(Original Veritrope.com 1.x Code Modified by Chris Rasmussen, digitalformula.net)
March 14, 2012

Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/delicious-bookmark-importer-for-evernote/

-- CHANGELOG FOR v2.5 (Justin)
        Fix for untitled items
       
-- CHANGELOG FOR v2.4 (Justin)
        Workaround for Note Titles Which Contain Apostrophes

-- CHANGELOG FOR v2.3 (Justin)
        Improved Dialog Boxes
        Added Import Completion Alert
        Improved Verification of Links
        HTML Entity Decode for Bookmark Titles
        Added Cancel / Error Handling
        Removed Extraneous Code
        Added Additional Comments
        Standardized Script Format

-- CHANGELOG FOR v2.0 - 2.2 (A Major Update By Chris Rasmussen)
        Bad bookmarks are now added to a notebook called 'Bad Bookmark list' (createNote method changed to allow that)
        Added prompt for verifying URL
        Added prompt for saving notes with actual title as title or URL as title
        Added prompt for location i.e. U.S. or Japan or everywhere else
        Added prompt for saving the bookmark or full page as the note's content (recommended for Evernote premium users, only)

        (Note : The above prompts would usually be too many but since this script is probably intended to be run once only,
        more options are better than none)
*)


(*
======================
/// EDITABLE SCRIPT PROPERTIES
======================
*)


-- TITLE FOR DIALOG PROMPTS
property scriptTitle : "Import Delicious Bookmarks Into Evernote"

-- SET THE VARIABLE BELOW TO THE NAME OF THE EVERNOTE FOLDER TO USE FOR IMPORTED BOOKMARKS
property EVnotebook : "Delicious Import"

-- NOTEBOOK FOR BAD BOOKMARKS (LOCAL ONLY)
property badNotebook : "Bad Bookmark List"

(*
======================
/// OTHER SCRIPT PROPERTIES
(USE CAUTION WHEN CHANGING VALUES)
======================
*)


property linkList : {}
property theTags : {}
property linkExists : ""
property theCount : ""
property badCount : ""

property NewEncodedText : ""
property theDecodeScript : ""

(*
======================
/// MAIN PROGRAM
======================
*)


try
   
    -- FIRSTLY, CREATE THE 'BAD BOOKMARKS' NOTEBOOK
    -- WE DO THIS NOW BECAUSE OTHERWISE APPLESCRIPT WILL, BY DEFAULT, CREATE A SYNCHRONISED NOTEBOOK
    -- ALSO, IF WE DON'T CHECK TO SEE IF THE NOTEBOOK EXISTS FIRST, AN ERROR WILL BE THROWN IF THE NOTEBOOK ALREADY EXISTS
    tell application "Evernote"
        if (not (notebook named badNotebook exists)) then
            create notebook badNotebook with type local only
        end if
    end tell
   
    -- PROMPT THE USER FOR THEIR LOCATION
    set location to (display dialog "Please Choose Your Location:" buttons {"U.S.", "Japan", "Outside Japan / U.S."} default button "U.S." with title scriptTitle ¬
        with icon path to resource "Evernote.icns" in bundle (path to application "Evernote"))
    if button returned of location = "U.S." then
        set formatDates to "YES"
    else if (location = "Japan") then
        set formatDates to "JP"
    else
        set formatDates to "NO"
    end if
   
    -- VERIFY BOOKMARK URLS?
    -- IF THE USER RESPONDS IN THE AFFIRMATIVE, ALL URLS WILL BE VERIFIED AS WORKING BEFORE THEY'RE ADDED AS NOTES
    -- IF THE USERS RESPONDS IN THE NEGATIVE, URLS ARE NOT VERIFIED AND WILL BE ADDED EVEN IF THEY DON'T WORK
    set verifyResult to (display dialog "Verify URLs Before Import?" buttons {"Yes - Verify All URLs", "No - Just Import Everything", "Cancel"} default button "Yes - Verify All URLs" cancel button "Cancel" with title scriptTitle ¬
        with icon path to resource "Evernote.icns" in bundle (path to application "Evernote"))
    if button returned of verifyResult = "Yes - Verify All URLs" then
        set verify to true
    else
        set verify to false
    end if
   
    -- USE BOOKMARK TITLE OR URL AS NEW NOTE'S TITLE?
    -- THE ORIGINAL SCRIPT ADDED NOTES WITH THE BOOKMARK'S URL AS THE NOTE TITLE
    -- THIS WILL ALLOW THE USER TO KEEP THIS OPTION, IF THEY LIKE, OR HAVE THE NOTES' TITLE AS THE BOOKMARK TITLE (BETTER, IN MY OPINION)
    set titleResult to (display dialog "Use Bookmark URL or Title as Note Title?" buttons {"Use Bookmark's Title", "Use Bookmark's URL", "Cancel"} default button "Use Bookmark's Title" cancel button "Cancel" with title scriptTitle ¬
        with icon path to resource "Evernote.icns" in bundle (path to application "Evernote"))
    if button returned of titleResult = "Use Bookmark's URL" then
        set titleType to "url"
    else
        set titleType to "title"
    end if
   
    -- SAVE THE PAGE'S FULL CONTENT AS THE NOTE'S CONTENT?
    -- IF THE USER RESPONDS IN THE AFFIRMATIVE, THE PAGE'S ENTIRE CONTENT WILL BE SCRAPED AND SAVED AS THE NOTE'S BODY
    -- THIS IS ONLY RECOMMENDED FOR EVERNOTE PREMIUM USERS
    set pageResult to (display dialog ¬
        "Save Full Web Page As Note's Content?
(This Is Only Recommended For Evernote Premium Users Or Small Bookmark Lists)"
buttons {"Yes - Save Full Page", "No - Save summary only", "Cancel"} default button "Yes - Save Full Page" cancel button "Cancel" with title scriptTitle ¬
        with icon path to resource "Evernote.icns" in bundle (path to application "Evernote"))
    if button returned of pageResult = "Yes - Save Full Page" then
        set contentType to "full"
    else
        set contentType to "summary"
    end if
   
    -- RESET VALUES BEFORE BEGINNING
    set AppleScript's text item delimiters to ""
    set linkList to {}
    set theCount to 0
    set badCount to 0
   
    -- SELECT THE HTML FILE
    set deliciousFile to (choose file with prompt "Choose Delicious Bookmark Export File")
   
    -- OPEN THE HTML FILE
    set deliciousData to read deliciousFile
    set deliciousBody to my extractBetween(deliciousData, "<dl>", "</dl>")
   
    -- MAKE A LIST OF ALL LINKS EXTRACTED FROM EXPORT FILE
    set delDelimiter to "<dt>"
    set deliciousRAW to read deliciousFile using delimiter delDelimiter
    my processLinks(deliciousRAW, linkList)
   
    -- PROCESS EACH LINK INTO A SEPARATE WEBCLIPPED NOTE IN EVERNOTE, KEEPING TAGS AND DATES INTACT
    my evernoteImport(linkList, theTags, formatDates, verify, titleType, contentType)
   
    -- ANNOUNCE COMPLETION OF IMPORT
    activate
    display dialog "IMPORT COMPLETED!

"
& theCount & " Links Imported to
\""
& EVnotebook & "\" Notebook.

"
& badCount & " Links Need Review in
\""
& badNotebook & "\" Notebook." with title scriptTitle buttons {"Exit"} with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
   
    -- ERROR HANDLING
on error number -128
    display dialog "Import Cancelled!" with title scriptTitle buttons {"Exit"} with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
end try


(*
======================
/// SUBROUTINES
======================
*)


-- FIND DELICIOUS HREF INFORMATION
on processLinks(deliciousRAW, linkList)
    repeat with i from 1 to (length of deliciousRAW)
        set theItem to item i of deliciousRAW
        if theItem contains "A HREF" then
            set end of linkList to theItem
        end if
    end repeat
end processLinks

-- PROCESS EACH LINK'S DATA
on evernoteImport(linkList, theTags, formatDates, verify, titleType, contentType)
    repeat with i from 1 to (length of linkList)
        set theItem to item i of linkList
       
        -- EXTRACT DATA FROM BOOKMARK FILE
        set theURL to my extractBetween(theItem, "A HREF=\"", "\"")
        set linkExists to "true" as boolean
        set epochseconds to my extractBetween(theItem, "ADD_DATE=\"", "\"")
        set deliciousTags to my extractBetween(theItem, "TAGS=\"", "\"") --as list
        set itemTitle to "Untitled"
        try
            set itemTitle to my extractBetween(theItem, "\">", "</A>")
        end try
        -- UTF-8 CONVERSION OF TITLE / DECODE ANY HTML ENTITIES IN TITLE
        set NewEncodedText to do shell script "echo " & quoted form of itemTitle & " | iconv -t UTF-8 "
       
        --SINGLE QUOTE WORKAROUND
        set NewEncodedText to my replaceText(NewEncodedText, "'", "'")
       
        set the_UTF8Text to quoted form of NewEncodedText
        set theDecodeScript to "php -r \"echo mb_convert_encoding(" & the_UTF8Text & ",'UTF-8', 'HTML-ENTITIES' );\"" as text
        set itemTitle to do shell script theDecodeScript
       
        -- CHANGE TEXT DELIMITERS TO PARSE TAGS
        set oldDelims to AppleScript's text item delimiters
        set AppleScript's text item delimiters to ","
        set theTags to text items of deliciousTags as list
       
        -- CHANGE TEXT DELIMITERS BACK
        set AppleScript's text item delimiters to oldDelims
       
        -- IF THE USER SELECTED TO VERIFY ALL URLS, THE URL WILL BE VERIFIED HERE
        if verify is true then
            my verify_Link(theURL)
        else
            set linkExists to true
        end if
       
        if linkExists is not false then
           
            -- ADD THE BOOKMARK TO THE IMPORT NOTEBOOK
            if titleType = "title" then
                my createNote(itemTitle, theURL, theTags, epochseconds, EVnotebook, contentType, formatDates)
            else
                my createNote(theURL, theURL, theTags, epochseconds, EVnotebook, contentType, formatDates)
            end if
            set theTags to {}
           
            -- COUNT THE SUCCESS!
            set theCount to (theCount + 1)
           
        else
           
            -- ADD THE BOOKMARK TO THE "BAD LINKS" NOTEBOOK
            if titleType = "title" then
                my createNote(itemTitle, theURL, theTags, epochseconds, badNotebook, contentType, formatDates)
            else
                my createNote(theURL, theURL, theTags, epochseconds, badNotebook, contentType, formatDates)
            end if
            set theTags to {}
           
            -- COUNT THE BAD LINK!
            set badCount to (badCount + 1)
        end if
       
    end repeat
end evernoteImport

-- VERIFY WORKING URL
on verify_Link(theURL)
    try
       
        --CHECK THE HTTP HEADER FOR PAGE STATUS
        set the_page to do shell script "curl -siL \"" & theURL & "\""
        set the_code to items 10 thru 12 of the_page as string
        if the_code is greater than 301 then
            -- THIS VALUE ("301") WILL SEND MOST REDIRECTS AND ERRORS TO THE "BAD LINKS" NOTEBOOK.
            -- (I CHOSE TO INCLUDE REDIRECTS HERE BECAUSE SOME DNS SERVERS WILL SEND UNKNOWN / BAD LINKS TO THEIR OWN SEARCH PAGE.)
            -- (CHANGE THIS VALUE TO "400" IF YOU WANT TO EXCLUDE REDIRECTS)
            set linkExists to "false" as boolean
        end if
    end try
end verify_Link

-- CREATE NOTE IN EVERNOTE
on createNote(itemTitle, theURL, theTags, epochseconds, notebookName, contentType, formatDates)
    tell application "Evernote"
        try
            set createDate to my epoch2datetime(epochseconds, formatDates)
            -- CREATE THE NOTE BASED ON USER'S CHOICE OF SUMMARY OR ENTIRE PAGE CONTENT
            if contentType = "full" then
                -- SAVE ENTIRE PAGE (THIS IS THE ORIGINAL SCRIPT)
                set n to create note from url theURL created createDate notebook notebookName tags theTags
            else
                -- SAVE SUMMARY ONLY (NEW OPTION, GOOD FOR EVERNOTE BASIC ACCOUNT HOLDERS)
                set n to create note title itemTitle with text theURL created createDate tags theTags notebook notebookName
                set source URL of n to theURL
            end if
        end try
    end tell
end createNote

-- CONVERT UNIX EPOCH TO DATE/TIME
on epoch2datetime(epochseconds, formatDates)
    -- ADAPTED FROM SCRIPT FOUND AT ERIK'S LAB
    -- (http://erikslab.com/2006/09/05/how-to-convert-an-epoch-time-to-a-meaningful-date-and-time/)
    set myshell1 to "date -r "
    if formatDates is "YES" then --ADDED DATE FORMAT SWITCH HERE
        set myshell2 to " \"+%m/%d/%Y %l:%M %p\"" -- Formatted for U.S.
    else if formatDates is "JP" then
        set myshell2 to " \"+%Y/%m/%d %l:%M %p\"" -- Formatted for Japan
    else
        set myshell2 to " \"+%d/%m/%Y %l:%M %p\"" -- Formatted for Everyone Else
    end if
    set theDatetime to do shell script (myshell1 & epochseconds & myshell2)
    return date theDatetime
end epoch2datetime

-- EXTRACT TEXT BETWEEN HTML TAGS
on extractBetween(SearchText, startText, endText)
    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to startText
    set endItems to text of text item -1 of SearchText
    set AppleScript's text item delimiters to endText
    set beginningToEnd to text of text item 1 of endItems
    set AppleScript's text item delimiters to tid
    return beginningToEnd
end extractBetween

--REPLACE
on replaceText(theText, serchStr, replaceStr)
    set tmp to AppleScript's text item delimiters
    set AppleScript's text item delimiters to serchStr
    set theList to every text item of theText
    set AppleScript's text item delimiters to replaceStr
    set theText to theList as string
    set AppleScript's text item delimiters to tmp
    return theText
end replaceText