Skip to content

Instantly share code, notes, and snippets.

@metbril
Last active January 28, 2016 19:04
Show Gist options
  • Save metbril/e763d66427e2802533b2 to your computer and use it in GitHub Desktop.
Save metbril/e763d66427e2802533b2 to your computer and use it in GitHub Desktop.
Convert plain text journal to evernote. If you have one plain text file containing your journal and would like to migrate that to individual entries in Evernote. This type of journal is typically used with the jrnl app found at https://maebert.github.io/jrnl/
(*
Title: Import Journal to Evernote
Description: Convert plain text journal to evernote. If you have one plain text file containing your journal and would like to migrate that to individual entries in Evernote. This type of journal is typically used with the jrnl app found at https://maebert.github.io/jrnl/
Author: Robert van Bregt (http://www.robertvanbregt.nl)
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*)
-- USER CONFIG --
property JournalPath : "/path/to/journal.txt" -- full path to journal
property JournalNotebook : "" -- Existing Evernote notebook. Empty for default notebook
-- read text file
set JournalText to my readFile(JournalPath)
set JournalParagraphs to every paragraph of JournalText
set noteTitle to ""
set noteText to ""
set noteCreated to missing value
-- loop through paragraphs
repeat with OneParagraph in JournalParagraphs
set TheParagraph to (text of OneParagraph)
if (length of TheParagraph) < 16 then
set JournalDate to missing value
else
-- try to parse first characters as valid date/time
set t to my StringLeft(TheParagraph, 16)
set JournalDate to my ISODateStrToDate(t)
end if
-- start new entry if the paragraph begins with a date
if not (JournalDate is missing value) then
-- save previous item
if (not noteText is "") then -- not for first item
my CreateNote(noteTitle, noteText, noteCreated)
end if
-- start new item
set noteTitle to TheParagraph
set noteText to TheParagraph
set noteCreated to JournalDate
else
-- next paragraph for entry
set noteText to (noteText & "
" & TheParagraph) -- append text
end if
end repeat
-- don't forget last entry
my CreateNote(noteTitle, noteText, noteCreated)
-- Read a UTF-8 encoded text file:
on readFile(unixPath)
set foo to (open for access (POSIX file unixPath))
set txt to (read foo for (get eof foo) as «class utf8»)
close access foo
return txt
end readFile
to StringLeft(s, n)
return text 1 thru n of s
end StringLeft
to StringRight(s, n)
return text -n thru -1 of s
end StringRight
on ISODateStrToDate(theStr)
try
set dt to (current date)
set savedDelimeters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"-", "T", " ", ":"}
set {dt's year, dt's month, dt's day, dt's hours, dt's minutes} to (every text item of theStr)
set AppleScript's text item delimiters to savedDelimeters
set dt's seconds to 0
on error
set dt to missing value
end try
return dt
end ISODateStrToDate
on CreateNote(theTitle, theText, theDateCreated)
tell application "Evernote"
try
set n to create note with text theText title theTitle created theDateCreated notebook JournalNotebook
end try
end tell
end CreateNote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment