Daily Work Log in OS X
Recent juggling of multiple projects has created a need to track what I’ve been doing day by day, as well as keep some notes on what I want to be doing. I have thus revived a system I created long ago to edit a weekly log file in my favorite editor (vim) via a keyboard shortcut. As I’m currently working mostly in OS X, this required some modifications from the Linux version I used in the past. The desired action is relatively simple. I press a hotkey combination and a text editor window opens with the current week’s log file, which has been created on the fly if it didn’t already exist.
Update: I updated the script with --remote-silent and --servername based
on a suggestion in the comments.
Basic Script
The first thing to decide is location and naming convention for the files. As there is going to be one file per week, more or less indefinitely, I’m going to need some form of date in the file name so that when listed alphabetically, the files will also be sorted chronologically. For location, I’m using a folder inside Dropbox which is an easy way for the notes to be automatically backed up and synced to my various computers.
I would like to name each file log_yyyy-mm-dd.txt where yyyy-mm-dd is the
date of the first weekday (Monday) for that week, but the difficulty is that
the script needs to be able to create a new, properly named file when run on
any day of the week, not just on Monday. OS X ships with the BSD version of
the date command, which allows date manipulation via the -v parameter.
date -v-monday +log_%Y-%m-%d.txt
This will take the current date and rewind to the most recent Monday
(-v-monday), then reformat that date as a string suitable for a file name.
I put the year first so that earlier dates will sort correctly before later
ones. You should enclose the format parameter (+log_%Y-%m-%d.txt) in quotes
if your format string includes spaces or any characters such as * or ? which the
shell would interpret incorrectly.
This is enough to write a basic launcher script:
#!/bin/bash -e LOG_DIR=$HOME/Dropbox/work/worklog LOG_FILE=$(date -v-monday '+log_%Y-%m-%d.txt') gvim $LOG_DIR/$LOG_FILE
If for some reason your favorite text editor is not MacVim, you can replace the
last line as necessary. I save the file to ~/bin/edit_worklog.sh and make it
executable. Upon running the script, I get a new MacVim window with the empty
file ~/Dropbox/work/worklog/log_2011-12-05.txt open in it. So far so good.
Improvements
Now I’d like to make some improvements. When I make my log entries, I like to separate each day with a header line that includes the date, like this:
Monday, 12/05 * did * some * stuff Tuesday, 12/06 * continued work on stuff * meeting with Mr. X ... etc
It would be nice if the launcher script could create the 5 weekday headers for
me when creating the new log file. I already have the date for Monday for the
name of the file, but now I need dates for Tuesday through Friday. These are
simply offset by one or more days from the Monday value. For this I can use
-v+1d to add days as needed. If you are using GNU date, you can use something
like -d'monday+1 days'. Here’s the whole week:
# prints Monday, 12/05 date -v-monday '+%A, %m/%d' # prints Tuesday, 12/06 date -v-monday -v+1d '+%A, %m/%d' # prints Wednesday, 12/07 date -v-monday -v+2d '+%A, %m/%d' # prints Thursday, 12/08 date -v-monday -v+3d '+%A, %m/%d' # prints Friday, 12/09 date -v-monday -v+4d '+%A, %m/%d'
Now it would be nice to have the script open the file with the cursor position
at the end and to control the size of the editing window. These can be
accomplished by adding + and --cmd 'set lines=25 columns=60' (respectively)
to the gvim command.
Lastly, if I run the script while the file is already open for editing, I’ll get
the usual vim warning about the swap file being already in use. For this
project, I would rather bring the existing editor window to the foreground. For
this we can use vim’s remote editor capabilities. By adding --remote-silent we
ask vim to edit the file in an existing window (the remote part) and create a
new editor window if one doesn’t already exist (the silent part). We should also
add --servername worklog to give the remote window a name so that it doesn’t
replace any existing remote sessions.
Here’s my full script with all improvements and some handy comments.
#!/bin/bash
#
# edit_worklog.sh - script to edit (and create if necessary)
# a rotating weekly work log file
#
# Author: Barry Stump
# this is the path to the weekly log folder
LOG_DIR="$HOME/Dropbox/work/worklog"
# the files are named by the first day of the week (most recent Monday)
LOG_FILE="$LOG_DIR/"$(date -v-monday '+log_%Y-%m-%d.txt')
# does today's file not exist yet?
if [ ! -e $LOG_FILE ]; then
# create new log file with a line for each weekday...
# (this version is for the BSD date command that
# comes with OS X. For GNU date, use something like
# -d'monday+2 days')
# Monday
date -v-mon '+%A, %m/%d' > $LOG_FILE
echo >> $LOG_FILE
# Tuesday
date -v-mon -v+1d '+%A, %m/%d' >> $LOG_FILE
echo >> $LOG_FILE
# Wednesday
date -v-mon -v+2d '+%A, %m/%d' >> $LOG_FILE
echo >> $LOG_FILE
# Thursday
date -v-mon -v+3d '+%A, %m/%d' >> $LOG_FILE
echo >> $LOG_FILE
# Friday
date -v-mon -v+4d '+%A, %m/%d' >> $LOG_FILE
echo >> $LOG_FILE
fi
# open up the file
# --cmd "set lines=25 columns=60"
# (resize window to 60x25)
#
# + (move cursor to end of file)
#
# --servername worklog --remote-silent
# (bring window to foreground if already open)
#
gvim --cmd "set lines=25 columns=60" + \
--servername worklog --remote-silent $LOG_FILE
Keyboard Shortcut
Now that I have a handy script to launch my work log, I would like to bind it to a hotkey combination. In OS X, I accomplished this by using the Automator utility to create a service workflow and then binding a hotkey to it. These directions are for OS X 10.6 Snow Leopard. Older or newer versions of OS X may require some tweaks.
First, open Automator (from the Applications folder) and choose the Service template in the dialog that appears.

For “Service receives selected” choose “no input”. Now drag “Run Shell Script”
from the library listing on the left to the empty section on the right. Replace
the default command cat with the path and name of the saved script from above.
Here I’m using $HOME/bin/edit_worklog.sh.

When done, save the workflow with ⌘S and enter a name in the popup. If you ever need to edit the workflow again later, it is located at ~/Library/Services. I called my workflow “View Worklog”. This adds an entry “View Worklog” to the Services menu of every application. The final step is to associate a system-wide keyboard shortcut to this service. Open System Preferences, choose Keyboard, and then click the Keyboard Shortcuts pane. Click “Services” from the category list on the left, then scroll down to “General”. Double click to the right of your newly created service name to assign a keyboard shortcut. I used ⇧⌘` which appears to be unused on my system.

Close System Preferences to save the settings, and you should be able to pull up the weekly log window just by using the new keyboard shortcut combination.

Use gvim –remote-silent to open in an existing gvim instance.
Your idea was good, and I modified it a bit for my purposes, if you’re interested, my version is here:
https://gist.github.com/1471562
@Mats Rauhala That’s a good suggestion. I’ve updated the script with –remote-silent and –servername.
Delete the newlines after Tuesday, and add the search for the new line to cmd. If you push the newline ahead of your edits you can be dropped into the file on the right line. Or add a funny character ahead of the dates and get dropped onto (or after) your last non-date line.