Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env ruby
# Based on everwatch.rb by Brett Terpstra, 2011, a 2013 update by spetschu, and a 2014 update by regedor
# Write in Markdown in Evernote and Backup Markdown files in Dropbox
# Change the next two lines with your account number and the path to your backup folder
watch_folder = File.expand_path("/Users/USERNAME/Library/Containers/com.evernote.Evernote/Data/Library/Application Support/com.evernote.Evernote/accounts/www.evernote.com/YOUR-ACCOUNT-NUMBER/content/")
mark_folder = "~/Dropbox/Evernotes/"
counter = 0
while true do # repeat infinitely
# recursive glob needed for current Evernote setup
files = Dir.glob( File.join( watch_folder, "**/*") )
# check for timestamp changes since the last loop
new_hash = files.collect {|f| [ f, File.exist?(f) ? File.stat(f).mtime.to_i : 0 ] }
hash ||= new_hash
diff_hash = new_hash - hash
if diff_hash.empty? # if there's no change
# if it's been less than 10 seconds, increment the counter
# otherwise, set it to zero and wait for new changes
counter = (counter < 10 && counter > 0) ? counter + 1 : 0
else
hash = new_hash
counter = 1
end
if counter > 0 # if the time is running
note = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
return HTML content of item 1 of the_selection
else
return ""
end if
end tell
APPLESCRIPT}
title = %x{ osascript <<APPLESCRIPT
tell application "Evernote"
if selection is not {} then
set the_selection to selection
return title of item 1 of the_selection
else
return ""
end if
end tell
APPLESCRIPT}
unless note == '' # if we got something back from the AppleScript
txtnote = %x{echo '#{note.gsub("'",'__APOSTROPHE__')}'|textutil -stdin -convert txt -stdout | pandoc -f html -t markdown_strict --no-wrap}
title = title.strip
file = mark_folder+"#{title}"+".md"
watch_note = File.new(File.expand_path(file),'w+')
watch_note.puts txtnote.gsub(/\\_\\_APOSTROPHE\\_\\_/, "'").gsub(/\\/, "")
watch_note.close
end
end
sleep 5
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment