#!/usr/bin/ruby # tp-dailylog.rb - Log TaskPaper tasks completed on the current day to a Day One entry # Brett Terpstra 2012 # # Run it with launchd at 11pm and forget about it # # Notes: # * Uses `mdfind` to locate all .taskpaper files changed in the last day # * Scans for @done(xxxx-xx-xx) tags in each line matching today's date # * Does not alter TaskPaper files in any way # * Does not require the Day One CLI tool # * Only generates report if there are completed tasks found # * It's configured to locate an iCloud-synced journal. # If you use Dropbox or other, you'll want to just hardcode the path (comment line 23, edit `dayonepath` line 24) # * To set the automatic Day One entries to starred, just change `starred = false` to true require 'time' require 'erb' uuid = %x{uuidgen}.gsub(/-/,'').strip datestamp = Time.now.utc.iso8601 starred = false dayonedir = %x{ls ~/Library/Mobile\\ Documents/|grep dayoneapp}.strip dayonepath = "~/Library/Mobile\ Documents/#{dayonedir}/Documents/Journal_dayone/entries/" template = ERB.new <<-XMLTEMPLATE Creation Date <%= datestamp %> Entry Text <%= entrytext %> Starred <<%= starred %>/> UUID <%= uuid %> XMLTEMPLATE today = Time.now().strftime('%Y-%m-%d') files = %x{mdfind 'kMDItemContentModificationDate >= "$time.today(-1)" && kMDItemKind == "TaskPaperDocumentType"'} projects = [] files.each do |file| if File.exists?(file.strip) f = File.open(file.strip) lines = f.read f.close project = "### " + File.dirname(file).gsub(/^.*?\/([^\/]+)$/,"\\1") + ":\n\n" found_completed = false lines.each do |line| if line =~ /@done\(#{today}\)/ found_completed = true project += line.gsub(/@done\(.*?\)/,'').strip + "\n" end end if found_completed projects.push(project) end end end def e_sh(str) str.to_s.gsub(/(?=["\\])/, '\\') end if projects.length > 0 entrytext = "# Task report for #{today}\n\n" projects.each do |project| entrytext += project + "\n\n" end fh = File.new(File.expand_path(dayonepath+uuid+".doentry"),'w+') fh.puts template.result(binding) fh.close end