Skip to content

Instantly share code, notes, and snippets.

@tsaiid
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaiid/4bc72b1f69de03a07356 to your computer and use it in GitHub Desktop.
Save tsaiid/4bc72b1f69de03a07356 to your computer and use it in GitHub Desktop.
Watch Evernote for updates and put the current content of the editor into a preview file for Marked 2.app
#!/usr/bin/env ruby
# Modified from everwatch.rb by Brett Terpstra, 2011
# http://brettterpstra.com/2011/11/14/marked-scripts-nvalt-evernote-marsedit-scrivener/
# http://support.markedapp.com/kb/how-to-tips-and-tricks/marked-bonus-pack-scripts-commands-and-bundles
# Modified from everwatch.rb by regedor, 2014
# https://github.com/regedor/everwatch
#
# Watch Evernote for updates and put the current content of the editor into a preview file for Marked 2.app
account = ARGV[0] || ENV['EVERWATCH_ACCOUNT'] || ENV["USER"]
watch_folder = File.expand_path("~/Library/Containers/com.evernote.Evernote/Data/Library/Application Support/com.evernote.Evernote/accounts/www.evernote.com/#{account}/content")
unless Dir.exist?(watch_folder)
watch_folder = File.expand_path("~/Library/Application Support/com.evernote.Evernote/accounts/www.evernote.com/#{account}/content")
abort("watch_folder not exists!") unless Dir.exist?(watch_folder)
end
counter = 0
marked_note = File.expand_path("~/EvernoteSelection.md")
File.open(marked_note, 'w') {} unless File.exist?(marked_note)
`open -a 'Marked 2' #{marked_note}`
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
set the_title to title of item 1 of the_selection
set the_content to HTML content of item 1 of the_selection
set the_note to "# " & the_title & "\n" & the_content
return the_note
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 -inputencoding utf-8 -format html -convert txt -stdout}
watch_note = File.new("#{marked_note}",'w')
watch_note.puts txtnote.gsub('__APOSTROPHE__', "'").gsub(/\u00a0/, ' ')
watch_note.close
end
# sleep an extra second on changes because Marked only
# reads changes every 2 seconds
sleep 1
end
sleep 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment