Jump to content

Export Fails After 6 Notebooks


Recommended Posts

I'm using an AppleScript (from the former Evernote Ambassador) to export each notebook to its own ENEX backup. For some reason, the script fails every time after exporting the same 6 notebooks. It never gets to the seventh. I can't even figure out if the seventh is corrupted or contains a un-exportable note because EN isn't exporting in alphabetical order.

Help? I use notebook organization, and EN's bulit-in export ignores notebooks. Thus, I need a backup system like this script that does per-notebook backups without failing.

Here's the script, the exported result up until the failure, my list of notebooks. You can see that the export starts with the "Project..." notebooks, and even those out of order, so I don't see a way to predict the notebook causing the export to fail with the confusing (because the script was using the existing directory) "no such file or directory" error.

set curDate to do shell script "date +'%Y%m%d'"

-- Change the path below to the location you want the notes to be exported
set exportPath to "/Volumes/Documents Drive/Crucial Files/Systems - Global/Evernote Backups/"

do shell script "if [ ! -e " & exportPath & " ]; then mkdir " & exportPath & "; fi;"

with timeout of (30 * 60) seconds
	tell application "Evernote"
		repeat with nb in every notebook of application "Evernote"
			set theName to the name of nb
			
			set matches to find notes "notebook:" & "\"" & theName & "\""
			set f to exportPath & theName & "-" & curDate & ".enex"
			export matches to f
		end repeat
	end tell
	
end timeout

 

Screen Shot 2016-05-27 at 10.55.26.png

Screen Shot 2016-05-27 at 10.55.54.png

Link to comment
  • Level 5*

>>I'm using an AppleScript (from the former Evernote Ambassador) to export each notebook to its own ENEX backup. For some reason, the script fails every time after exporting the same 6 notebooks. It never gets to the seventh. I can't even figure out if the seventh is corrupted or contains a un-exportable note because EN isn't exporting in alphabetical order.

You might want to review your notebook names and make sure they are valid names for a file.

Also, you might want to bypass empty notebooks

Link to comment

Aha! The empties were it (and forcing a sync after putting notes in the empty notebooks). Thank you!

Could you tell me what I'd need to add to the script to automatically handle empty notebooks?

Link to comment
  • Level 5*
On May 27, 2016 at 0:38 PM, iamPariah said:

Could you tell me what I'd need to add to the script to automatically handle empty notebooks?

 if matches is not {} then

My personal backup script is below and posted here https://discussion.evernote.com/topic/86152-backing-up-and-restoring-evernote-data-reference-article/?do=findComment&comment=406486

on Backup_WeeklyFull()

    

    tell application "Evernote"

        

        set backupLog to backupLog & return & (current date) & " Weekly Backup started"

        set allNotebooks to every notebook

        

        repeat with currentNotebook in allNotebooks

            

            set notebookName to (the name of currentNotebook)

            set backupLog to backupLog & return & (current date) & " Backing up notebook - " & notebookName

            set fileBackupenex to "/Users/david/Desktop/Evernote Backup/Notebook-" & notebookName & ".enex"

            set fileBackupenex to POSIX path of fileBackupenex

            set theNotes to every note in notebook notebookName

            set theNotesCt to count of theNotes

            set backupLog to backupLog & " (" & theNotesCt & ")"

            if theNotes is not {} then

                try

                    

                    with timeout of (20 * 60) seconds

                        export theNotes to fileBackupenex format ENEX with tags

                        do shell script "/usr/bin/gzip -f " & quoted form of fileBackupenex

                    end timeout

                    

                on error errorMessage

                    set backupLog to backupLog & return & (current date) & " Backup Failed - " & errorMessage

                    return errorMessage

                    

                end try

            end if

            

        end repeat

    end tell

    

end Backup_WeeklyFull

 
Link to comment

I don't need the daily and weekly backups of your full script. I also can't get your above script to do anything. It produces no result. (Obviously I changed the fileBackupenex path and added Backup_WeeklyFull to the end.)

Link to comment
  • Level 5*
On 5/27/2016 at 1:02 PM, iamPariah said:

I'm using an AppleScript (from the former Evernote Ambassador) to export each notebook to its own ENEX backup. For some reason, the script fails every time after exporting the same 6 notebooks. It never gets to the seventh. I can't even figure out if the seventh is corrupted or contains a un-exportable note because EN isn't exporting in alphabetical order.

@iamPariah, here's a revised script, based on the one you were using, that should work.  You can either use it directly, or examine it for making changes to your script.

EDIT:  2016-05-30 02:12 CT (Mon) -- Script Updated

  1. Added function to clean Notebook name
  2. Added Test Mode
  3. Added Timeout (1 HR)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

My revised script does the following:

  1. Does NOT export Notebooks that are either empty, or are NBs that you have joined (you must identify these).
    1. By default, ALL notebooks are returned to AppleScript, including those that you have joined.
    2. Most likely, you will NOT want to backup joined NBs.
    3. Unfortunately, EN Mac does NOT provide a NB property that indicates it is a "joined" NB.
    4. So, you will have to enter your Joined NBs in the script, into the "joinedNBList" list.
  2. Prompts user if specified export folder does not exist, and remembers it for use next time.
  3. For each NB exported, outputs a log entry (in the Script Editor window), and a standard Mac notification

I've only given it limited testing, so proceed with caution.  You might export only a few NBs and to a test folder to begin with.  I have added a "Test Mode" where you can limit the number of notebooks exported.

Please let me know if you find any issues.

The Notifications look like this:

EN-Mac-AS-Export-ENEX-Notify.png

The log output looks like this:

Quote

(*exportFolder: /Volumes/ApoNAS-Main/Backup/Evernote/ENEX/*)
(*TEST Shared NB -- 2016-05-27.enex*)
(*OL.Mobile -- 2016-05-27.enex*)
(*S.MacTips -- 2016-05-27.enex*

Here's the script:

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
VER:  1.1      Last Update: 2016-05-30 02:11 CT (Mon)
 
 SCRIPT SETUP

CHANGE THE BELOW TO FIT YOUR Evernote ACCOUNT
   exportFolder property
   joinedNBList
   Testing Mode
  
  (All identified by the label "## SETUP CHANGE ##")S
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)


## SETUP CHANGE ##
--- Change the exportFolder property below to the location where you want the notes to be exported ---
--    NOTE: IF Folder is NOT found the first time, your choice will be
--      remembered/used the next time you run the script.

property exportFolder : "/Volumes/Documents Drive/Crucial Files/Systems - Global/Evernote Backups/"

if not my existsFinderItem(exportFolder) then
  set exportFolder to POSIX path of (choose folder)
end if
log ("exportFolder: " & exportFolder)

##   SETUP CHANGE ##
--- CHANGE TO THE NOTEBOOKS THAT YOU HAVE JOINED ---
--    Otherwise, they will be exported as well
--    Add or Delete names/lines as needed

set joinedNBList to 
  "NB_1_Name", ¬
  "NB_2_Name", ¬
  "NB_3_Name"}

set curDate to do shell script "date +'%Y-%m-%d'" ##CHG:JM

## JM:  I don't like this approach of automatic creation of folder ##
#
#do shell script "if [ ! -e " & exportFolder & " ]; then mkdir " & exportFolder & "; fi;"
#


##   SETUP CHANGE ##
-- Set to Testing Mode if desired

set testingBol to false -- set to true when testing.
set numTestNB to 2 -- number of Notebooks to export when testing.

with timeout of (60 * 60) seconds -- 1 hour
  
  tell application "Evernote"
    
    set notebookList to every notebook
    set numNBExported to 0
    
    repeat with oNB in notebookList
      
      set nameNB to the name of oNB
      set noteList to every note in oNB ## best way to get all Notes in a NB
      set numNotes to count of noteList
      
      --- EXPORT TO ENEX ONLY IF ---
      --      NOT in Joined NB List
      --     NOT Empty
      
      if (nameNB is not in joinedNBList) and (noteList  {}) then
        
        set numNBExported to numNBExported + 1
        
        
        set rootFileName to my cleanName(nameNB) & " -- " & curDate & ".enex"
        set exportFilePath to exportFolder & rootFileName
        log "[" & numNBExported & "] " & rootFileName
        my Notify(name of me, nameNB & ": " & numNotes & " Notes")
        
        ------------------
        export noteList to exportFilePath
        ------------------
        
        --- IF TESTING, LIMIT NUMBER OF NOTEBOOKS EXPORTED ---
        if testingBol and numNBExported  numTestNB then
          log "*** Export Terminated Due to Testing ***"
          exit repeat
        end if
        
      end if
    end repeat
  end tell
  
end timeout
--~~~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~

on existsFinderItem(pPathStr)
  
  if pPathStr starts with "/" then
    set hfsPath to POSIX file pPathStr
  else if pPathStr contains ":" then
    set hfsPath to pPathStr
  else
    error "Invalid Path String: " & pPathStr
  end if
  
  set existsBol to false
  tell application "Finder"
    if exists hfsPath then set existsBol to true
  end tell
  
  return existsBol
  
end existsFinderItem



on Notify(pTitleStr, pNameNB)
  display notification with title ¬
    "NOW Exporting Notebook" subtitle pNameNB
  return
end Notify


on cleanName(pNameFinderItem)
  
  local invalidChars, cleanName
  
  set invalidCharsStr to "/\\:*?\"<>|"
  set cleanName to pNameFinderItem
  
  repeat with theChar in (characters in invalidCharsStr)
    set cleanName to my replaceString(cleanName, theChar, " ")
  end repeat
  
  return cleanName
  
end cleanName

on replaceString(pStringToSearch, pFindStr, pReplaceStr)
  
  local listText, stringWithReplacements
  
  --- SPLIT STRING INTO LIST USING FIND STRING ---
  set AppleScript's text item delimiters to pFindStr
  set listText to text items of pStringToSearch
  
  --- MERGE LIST INTO STRING USING REPLACE STRING ---
  set AppleScript's text item delimiters to pReplaceStr
  set stringWithReplacements to listText as text
  
  set AppleScript's text item delimiters to ""
  
  return stringWithReplacements
  
end replaceString

 

Link to comment
  • Level 5*
On May 27, 2016 at 1:33 PM, iamPariah said:

I don't need the daily and weekly backups of your full script. I also can't get your above script to do anything.

It produces no result. (Obviously I changed the fileBackupenex path and added Backup_WeeklyFull to the end.)

If you're going to run my script as is, remove the Backup_WeeklyFull lines.  They identify the code as a subroutine (handler) which doesn't execute unless called

Link to comment
  • Level 5*
21 hours ago, JMichaelTX said:

One thing that it does NOT do, that should be done, is to check for valid file name characters in the Notebook name.

I just wanted to follow up on this issue.

Strictly speaking, the ONLY characters that are invalid in a Mac OSX file name are:  
: /

Colon is used by the Mac HFS, and the forward slash is used by POSIX paths and the underlying unix system, as folder (directory) separators.

But, to be safe in case you might want to use or save these files on another system, then you should avoid:
/ \ : * ? " < > |

This list of characters has a space between each invalid character for readability.
Here's just the actual characters:

/\:*?"<>|

I substitute the SPACE character for all of the above when creating files and folders.

For more info, see What Characters Are Not Allowed in File Names? 

Link to comment
  • Level 5*
On May 27, 2016 at 11:02 AM, iamPariah said:

I can't even figure out if the seventh is corrupted or contains a un-exportable note because EN isn't exporting in alphabetical order.

If you're watching your script run, you can log/display your notebook names as shown above by @JMichaelTX.  

Since my backup script runs unattended, I added a log to identify the notebook(s) being exported;      
     set backupLog to backupLog & return & (current date) & " Backing up notebook - " & notebookName
and errors;
     on error errorMessage
                  set
backupLog to backupLog & return & (current date) & " Backup Failed - "  & errorMessage
  
             return errorMessage

My script is scheduled to run overnight;on completion it emails the backupLog to me which I can review in the morning

    Backup Log 
    Sunday, May 29, 2016 at 10:41:26 AM Daily Backup started (6)
    Sunday, May 29, 2016 at 10:41:34 AM Weekly Backup started
    Sunday, May 29, 2016 at 10:41:34 AM Backing up notebook - @Inbox (40)
    Sunday, May 29, 2016 at 10:41:58 AM Backing up notebook - Filing? (6960)
    Sunday, May 29, 2016 at 10:55:49 AM Backing up notebook - Ivy (9)
    Sunday, May 29, 2016 at 10:56:48 AM Backup completed

An error is indicated as

     Saturday, May 28, 2016 at 6:11:56 PM Backing up notebook - Filing? (6960)
     Saturday, May 28, 2016 at 6:12:56 PM Backup Failed - Evernote got an error: AppleEvent timed out.

 

Link to comment
  • Level 5*
On May 27, 2016 at 7:25 PM, JMichaelTX said:

here's a revised script, based on the one you were using, that should work.  You can either use it directly, or examine it for making changes to your script....

### DON'T NEED ### with timeout (30 * 60) seconds ...

 

I was curious about this comment.  
My experience has been that with the default timeout (2 min) the export fails on a timeout error (7000 notes).
I'm not seeing any harm with including this code

Link to comment
  • Level 5*
On 5/27/2016 at 9:25 PM, JMichaelTX said:

One thing that it does NOT do, that should be done, is to check for valid file name characters in the Notebook name.

Here is a handler/function that will "clean" the Notebook name to replace any invalid file characters with a space.

on cleanName(pNameFinderItem)
  
  local invalidChars, cleanName
  
  set invalidCharsStr to "/\\:*?\"<>|"
  set cleanName to pNameFinderItem
  
  repeat with theChar in (characters in invalidCharsStr)
    set cleanName to my replaceString(cleanName, theChar, " ")
  end repeat
  
  return cleanName
  
end cleanName

on replaceString(pStringToSearch, pFindStr, pReplaceStr)
  
  local listText, stringWithReplacements
  
  --- SPLIT STRING INTO LIST USING FIND STRING ---
  set AppleScript's text item delimiters to pFindStr
  set listText to text items of pStringToSearch
  
  --- MERGE LIST INTO STRING USING REPLACE STRING ---
  set AppleScript's text item delimiters to pReplaceStr
  set stringWithReplacements to listText as text
  
  set AppleScript's text item delimiters to ""
  
  return stringWithReplacements
  
end replaceString

Add this code to the bottom of my above script, and

REPLACE:
set rootFileName to nameNB & " -- " & curDate & ".enex"

WITH:
set rootFileName to my cleanName(nameNB) & " -- " & curDate & ".enex"

To make it easy for future readers, I'll update the my above script with these changes.

Link to comment
  • Level 5*
On 5/27/2016 at 9:25 PM, JMichaelTX said:

EDIT:  2016-05-30 02:12 CT (Mon) -- Script Updated

  1. Added function to clean Notebook name
  2. Added Test Mode
  3. Added Timeout (1 HR)

I have updated my above post with a revised script.

12 hours ago, DTLow said:

My experience has been that with the default timeout (2 min) the export fails on a timeout error (7000 notes).
I'm not seeing any harm with including this code

Good point.  I added the timeout back in, but with a value of 1 hr.

Link to comment
On 5/27/2016 at 7:25 PM, JMichaelTX said:

## JM: I don't like this approach of automatic creation of folder ## # #do shell script "if [ ! -e " & exportFolder & " ]; then mkdir " & exportFolder & "; fi;" #

I didn't either, so I omitted it from the script after posting the original.

 

JMichaelTX, your script perfectly fits my needs. Thank you very much!

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...