forked from spetschu/everwatch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
everwatch.rb
executable file
·67 lines (53 loc) · 2.2 KB
/
everwatch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
# 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
# Updated for latest Evernote configuration (2013) and quick launch of Marked.app - spetschu
#
# Watch Evernote for updates and put the current content of the editor into a preview file for Marked.app
# <http://markedapp.com>
marked_note = File.expand_path("~/EvernoteSelection.md")
File.open(marked_note, 'w') {} unless File.exist?(marked_note)
`open -a Marked #{marked_note}`
trap("SIGINT") { exit }
account = ARGV[0] || ENV['EVERWATCH_ACCOUNT'] || ENV["USER"]
watch_folder = File.expand_path("~/Library/Containers/com.evernote.Evernote/Data/Library/Application Support/Evernote/accounts/Evernote/#{account}/content")
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}
unless note == '' # if we got something back from the AppleScript
txtnote = %x{echo '#{note}'|textutil -stdin -convert txt -stdout}
watch_note = File.new("#{marked_note}",'w')
watch_note.puts txtnote
watch_note.close
end
# sleep an extra second on changes because Marked only
# reads changes every 2 seconds
sleep 1
end
sleep 1
end