Last active
April 6, 2018 02:34
-
-
Save craigeley/adfe870a2fae8bd17f8b to your computer and use it in GitHub Desktop.
When run with Hazel, this script takes a Day One file and creates a sidecar Markdown file and an alias to that file. When the Day One entry is modified, so is the Markdown file, even if it is moved and/or renamed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby | |
require 'plist' | |
# Fix encoding errors | |
if RUBY_VERSION =~ /2.2/ | |
Encoding.default_external = Encoding::UTF_8 | |
Encoding.default_internal = Encoding::UTF_8 | |
end | |
## Change the next line so it points to your Day One file | |
dayone_path = '/path/to/DayOneFile.dayone/entries/' | |
# Get Day One file and parse it as a plist | |
file_path = ARGV[0] | |
result = Plist::parse_xml(file_path) | |
uuid = result['UUID'] | |
alias_path = "#{dayone_path}/#{uuid}.md" | |
# If alias already exists, update the markdown file | |
if File.exist?(alias_path) | |
text = result['Entry Text'] | |
content = "---\nUUID:\s#{uuid}\n---\n#{text}" | |
path = %x{ osascript <<APPLESCRIPT | |
tell application "Finder" | |
set theItem to (POSIX file "#{dayone_path}/#{uuid}.md") as alias | |
if the kind of theItem is "alias" then | |
return the posix path of ((original item of theItem) as text) | |
end if | |
end tell | |
APPLESCRIPT} | |
path = path.chomp! | |
f = File.open(path, 'w+') {|f| f.write("#{content}") } | |
# If alias does not exist, create it as well as the Markdown file | |
else | |
text = result['Entry Text'] | |
format = "---\nUUID:\s#{uuid}\n---\n#{text}" | |
mark = "~/Desktop/#{uuid}.md" | |
f = File.open(mark, 'w+') {|f| f.write("#{format}") } | |
%x{ osascript <<APPLESCRIPT | |
tell application "Finder" | |
make new alias to file (POSIX file "#{mark}") at (POSIX file "#{dayone_path}") | |
end tell | |
APPLESCRIPT} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi I'm not well versed in Ruby any Idea how to get around this issue?
/usr/local/lib/ruby/gems/2.2.0/gems/plist-3.1.0/lib/plist/parser.rb:105:in
'parse': Unimplemented element (RuntimeError) from /usr/local/lib/ruby/gems/2.2.0/gems/plist-3.1.0/lib/plist/parser.rb:29:in
parse_xml'from /Users/marty/dotscripts/scripts/dayone_to_markdown.rb:16:in `