rb-dayone

DayOne is an excellent journalling tool for OS X and iOS. It’s particularly handy in that journal entries are stored as XML, which means that it’s really easy to create and edit entries yourself.

rb-dayone is a ruby gem for interacting with DayOne. Using rb-dayone you can create new entries, search through your journal, and edit or delete existing entries.

Installation

You can install rb-dayone via rubygems:

1
gem install rb-dayone

or you can put it in your Gemfile:

1
2
3
source "https://www.rubygems.org"

gem "rb-dayone", "~> 0.7.1"

or you can even download and install it from github.

The binary

rb-dayone ships with a simple binary, dayone, which you can use to create DayOne entries via the command line:

1
dayone create --text "Entry text here" --starred

If you don’t supply text, dayone will look for it through STDIN:

1
cat journalentry | dayone create

The dayone binary currently only allows you to add entries - no searching, editing or deleting from the command line until I work out an interface I like. There’s plenty more options for entries added using the command line - run dayone create --help to see them.

Using in ruby

rb-dayone really shines as its own library in ruby. There’s three main things you’ll want to do with it:

Create new entries

1
2
3
4
5
6
require 'rb-dayone'

e = DayOne::Entry.new "# Hello world!"
e.starred = true

e.create!

You can create a new DayOne entry using the DayOne::Entry class. You can modify several aspects before saving including:

Once you’re happy with the entry, call create! to save the entry to disk.

Search for entries

Searching is done via the DayOne::Search class:

1
2
3
4
5
6
7
8
9
10
require "rb-dayone"

search = DayOne::Search.new do
  entry_text.include "foo"
  starred.is true
  tag.exclude "bar"
  creation_date.before Date.new(2012,01,01).to_time
end

results = search.results # An array of DayOne::Entry objects

The above will find all entries which contain the string “foo” in their entry text, are not tagged “bar”, are starred, and were created before the 1st of January 2012.

You can use multiple entry_text and tag calls to match multiple conditions:

1
2
3
4
DayOne::Search.new do
  entry_text.include "bar"
  entry_text.include "baz"
end

This search will only find posts that have both “bar” and “baz” in them.

Finally, creation_date has two methods: before and after. These will, as you may well guess, limit the search to entries created before or after a given time. Specifying both allows you to pick entries in a particular range: for example, this will find all entries made yesterday with the tag “journal”:

1
2
3
4
5
6
7
8
9
10
require "date"

DayOne::Search.new do
  midnight_today = Date.today.to_time
  midnight_yesterday = (Date.today-1).to_time

  tag.include "journal"
  creation_date.before midnight_today
  creation_date.after midnight_yesterday
end

Editing and deleting entries

Once you have an array using the search tools above, you can edit these entries as you would an ordinary object. Once you’ve edited it, you can save the entry using DayOne::Entry.save!.

1
2
3
4
5
6
7
8
9
search = DayOne::Search.new do
  entry_text.include "#log"
  tag.exclude "log"
end

search.results.each do |entry|
  entry.tag "log"
  entry.save!
end

Alternatively, you can delete entries using DayOne::Entry.delete!

1
2
3
4
5
6
7
search = DayOne::Search.new do
  tag.include "boring"
end

search.results.each do |entry|
  entry.delete!
end

Future plans

At this stage, rb-dayone is somewhat feature-complete. While I had ideas for making slightly more advanced searches, I think this sort of thing is beyond the scope of casual use.

If you find a place where rb-dayone doesn’t do something you think it should, let me know and maybe I’ll add something else.

Links