Random ramblings about Mac, Python, TeX, programming, and more  |     |          |     |  

Get the notes out of Apple Notes into OneNote (via Evernote) with AppleScript

April 9, 2021  |  tools, programming, macos

I have been using Apple Notes for all my note taking since 2016. For not so interesting reasons, I recently had to change to Microsoft OneNote. But I did not want to leave all my meeting notes behind. I had to find a solution to get all my notes out for Apple Notes. And as many times before, AppleScript (and Python) was part of the solution.

My first attempt was to search for possible tools or recipes online. None of the ones I found did actually solve my problem, but I learned a lot about how difficult such a simple task actually is. One tool that I have tested and that is promising is Exporter from Chintan Ghate (available in Mac AppStore). It exports notes from Apple Notes as markdown files.

But I wanted to get my notes into OneNote. I then discovered the tool OneNote Importer from Microsoft. It imports notes from Evernote to OneNote. Well, I used to use Evernote a long long time ago, and I still have an active account. Could a possibility be to do this via Evernote? I downloaded the Evernote client to my Mac and searched online for solutions to get my notes from Apple Notes to Evernote. And several AppleScript based examples showed up. I started with the example from Jeremy R. Nelson from 2014: Importing Apple Notes into Evernote. At my first try nothing worked. The problem was that the latest Evernote client for macOS, Evernote 10, is not scriptable. The solution was to use the older scriptable legacy version of Evernote: Install an older version of Evernote.

The next step was to modify the example from Jeremy to make it work for my case. One problem I encountered was that some of my notes with embedded images were too large. I solved this by making versions of the notes with the embedded images removed and importing that version if the note was to large. To actually remove the embedded images from the files I called Python from the AppleScript program. This is my version of an Apple-Notes-to-Evernote AppleScript program (update the values of the variables myFolder and myNotebook to match your Apple Note folder exported from, and your Evernote notebook imported to):

-- The selected Apple Notes folder and Evernote notebook
set myFolder to "The name of the Apple Notes folder"
set myNotebook to "The name of the Evernote notebook"

-- Get the notes from Apple Notes
tell application "Notes"

    -- Traverse every note of the selected Apple Notes folder
    set theMessages to every note in folder myFolder
    repeat with thisMessage in theMessages
        -- Get the note
        set myTitle to the name of thisMessage
        set myText to the body of thisMessage
        set myCreateDate to the creation date of thisMessage
        set myModDate to the modification date of thisMessage
        -- Save the content of the note to a temporary file (used if note is to big)
        set tmpfilename to "tmpnotes_" & text 3 thru -1 of ((random number) as string)
        set posixtmpfile to POSIX path of (path to temporary items folder) & tmpfilename
        set fhandle to open for access posixtmpfile with write permission
        write myText to fhandle
        close access fhandle

        -- Set myNewText to the content of the note with the embedded images removed
        set myNewText to do shell script ¬
            "python -c \"import re; f = open('" & posixtmpfile & ¬
            "'); print(re.sub(r'<img.*?>', '[removed image]', f.read()))\""

-- Remove temporary file
do shell script "rm " & posixtmpfile
-- Add the note to Evernote tell application "Evernote Legacy" -- Create the new note set myNote to create note with text myTitle title myTitle notebook myNotebook -- Try to add the content (HTML) try set the HTML content of myNote to myText set problem to false on error errormsg number errorno set problem to true end try -- If problem, try to add the conent without the embedded images if problem then try set the HTML content of myNote to myNewText on error errormsg number errorno display dialog "Unable to add content (" & myTitle & "): " & ¬ errormsg & "(" & errorno & ")" end try end if -- Set the correct meta-data of the notes try set the creation date of myNote to myCreateDate set the modification date of myNote to myCreateDate on error errormsg number errorno display dialog "An error occured (" & myTitle & "): " & ¬ errormsg & "(" & errorno & ")" end try end tell end repeat end tell

The next step was to export my notes from Evernote and import then into OneNote. This should be easy with the tool OneNote Importer from Microsoft, however it crashed between sign in and import when I tried it. I have currently tested with OneNote Importer (Preview) 1.0.4 (1) for macOS.

Last updated: April 14, 2021