Skip to content

Instantly share code, notes, and snippets.

@jsulak
Created April 24, 2012 01:42
Show Gist options
  • Save jsulak/2475369 to your computer and use it in GitHub Desktop.
Save jsulak/2475369 to your computer and use it in GitHub Desktop.
Log to Day One from the command line
#!/usr/bin/env tclsh
# logtodayone.tcl
# James Sulak (http://www.jamessulak.com)
# Adapted from logtodayone.rb by Brett Terpstra (http://brettterpstra.com)
# Use and modify freely, attribution appreciated
#
# This script works with the Day One[1] command line utility
# It parses an input string for an exclamation point prefix to mark starred
# and/or a [date string] at the beginning to parse natural language dates
#
# Example usage:
# logtodayone.tcl "! This is a starred entry."
# logtodayone.tcl "[yesterday 3pm] Something I did yesterday at 3:00PM"
# logtodayone.tcl "! [-2 1:30am] A starred entry about something I did two days ago"
#
# Note that the Day One command line utility is inside the Day One Application bundle,
# at "/Applications/Day One.app/Contents/MacOS/dayone".
# You can create a convenient symbolic link with:
# ln –s /Applications/Day\ One.app/Contents/MacOS/dayone /usr/local/bin/dayone
# This script assumes you have done so.
if { $argc > 0 } {
set input [string trim [join $argv " "]]
} else {
puts "Log entry: "
gets stdin input
set input [string trim $input]
}
# If the input starts with an exclamation point, make it starred
set starred [expr {[regexp {^!} $input] ? "true" : "false" }]
# remove the bang from the input string
set input [string trimleft $input "! "]
# If there's a [date] specified, parse it
if { [regexp {^\[(.*?)\]} $input -> datestring] } {
# if the date starts with -X, assume it means X days ago
regsub {^\-(\d+)} $datestring {\1 days ago } datestring
# Replace a single 'y' within the date brackets with "Yesterday" for parsing
regsub {\yy\y} $datestring "yesterday" datestring
# Parse the resulting date string
catch { set d [clock scan $datestring] }
}
# If there's no date string, then use the current date/time
if { [info exists d] == 0 } {
set d [clock seconds]
}
set finalDateString [clock format $d -format "%m/%d/%Y %l:%M%p"]
set input [string trim [regsub {^\[.*?\]\s*} $input ""]]
exec {/usr/local/bin/dayone} -d="$finalDateString" -s=$starred new << $input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment