Automatic Tag Generator for DayOne Journals synced with Dropbox

Here’s how I created a system to automatically sync up keywords to tags in DayOne Journal. The journal is stored on my Dropbox, so if you have it in iCloud, I don’t know what you’re going to do.

First Step: Get text list of tags in a text file: (do this in the entries folder of your DayOne Journal, cd to it, if you can’t, google-fu.)

find . -type f |xargs sed -n “/<array>/,/<\/array>/p”|grep “<string>”|sed ‘s/<[^>]*>//g’|sed “s/^[ \t]*//”|tr -d ‘\011’|sort -f|uniq > tags.txt

Then copy this tags.txt folder to your home directory. Edit the file, be careful of phrases in XML that would confuse the system, Weather, Apple, DTD, String – that sort of thing, also be very careful around TLA’s as they can have unintended side effects. The later scripts that do the keyword searches don’t give a damn about cases, so your keywords or your KeYwOrDs will be equivalent.

You’ll need two more scripts. This one adds tags based on keywords passed to it as parameters: (be VERY careful, little L and capital I look very much the same, read man pages, yo)

tag.sh:

#!/bin/bash

cd /Users/username/Dropbox/Apps/Day\ One/Journal.dayone/entries

find . -type f -print0 |xargs -0 grep -L “<string>”$1″</string>”| xargs grep -li “”$1″” |xargs grep -l “<key>Tags</key>”|xargs -I file /usr/libexec/PlistBuddy -c “add Tags:Key string “$1″” file

find . -type f -print0 |xargs -0 grep -L “<string>”$1″</string>”| xargs grep -li “”$1″” |xargs grep -L “<key>Tags</key>”|xargs -I file /usr/libexec/PlistBuddy -c “add Tags array” file

find . -type f -print0 |xargs -0 grep -L “<string>”$1″</string>”| xargs grep -li “”$1″” |xargs grep -l “<key>Tags</key>”|xargs -I file /usr/libexec/PlistBuddy -c “add Tags:Key string “$1″” file

Next, you’ll need another script called updatetags.sh, looks like this:

#!/bin/bash
cat /Users/andy/Dropbox/Apps/Day\ One/tags.txt|xargs -n 1 /Users/andy/tag.sh

The last bit will be a new LaunchAgent, so go to ~/Library/LaunchAgents and mock up a plist file by copying one from there, if you need an example, try this:

more ~/Library/LaunchAgents/com.andymchugh.tag.plist
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN”
“http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Label</key>
<string>org.andymchugh.tag</string>
<key>ProgramArguments</key>
<array>
<string>/Users/andy/updatetags.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>06</integer>
</dict>
</dict>
</plist>

The name of the LaunchAgent has to be unique and the inside name (<string>org.andymchugh.tag</string>) should match the file name like you see here. Swap out the com bit for org, and forget the plist bit. This script will run every day at 6am. You can flavor it to your liking.

Then the final command to tie it all together:

launchctl load -w com.andymchugh.tag.plist

So now, when you write DayOne Journal entries you can mark up the tags.txt file that lives in your Dropbox. Value added, if you change this file anywhere, Dropbox will make sure it syncs up with all your other systems, so you can edit it and forget it pretty nicely as long as it’s not open-for-editing when your script fires off at 6am. That would be messy.

The first time you do it, you’ll notice your Dropbox churn for a long while and then all your other connected Dropboxes will churn as they sync up. Your iPhone and iPad will have huge downloads to do, but they’ll catch up well enough. As you create new DayOne Journal entries you can rest assured that your script will be doing keyword searching for you on your behalf and hooking up your tags to your DayOne Journal entries for you.

Just one more thing automated and out of the way. Hooray!

P.S. Backup your Journal, DIY. Be safe, be smart, learn how to use tar. man tar. Read it. Live it. tar is your delight and your savior. 🙂

EDIT: tags.sh needed a cd command first, otherwise it would just wander aimlessly. Sorry about that.

Tag Painting in Day One Journal

I’ve been really enjoying DayOne and they have recently updated their app so that the iPad, iPhone and Mac Apps can all create and manage tags. What’s been missing is a way to blast in tags based on keywords.

In this example, every time I have a journal entry with “Scott” in it, I want it to be tagged “Scott”.

Here’s how I did it:

1) Open Terminal, go to ~/Dropbox/Apps/Day One/Journal.dayone/entries

2) FOR FILES THAT DON’T HAVE TAGS, A TAG SECTION, PAINT THE SECTIONS WITH THE TAG

find . -print0 |xargs -0 grep -L “<string>Scott</string>”|xargs grep -l “Scott” |xargs grep -l “<key>Tags</key>”|xargs -I file /usr/libexec/PlistBuddy -c “add Tags:Key string ‘Scott'” file

3) FOR FILES THAT DON’T HAVE TAGS, DON’T HAVE A TAG SECTION, CREATE TAG SECTION

find . -print0 |xargs -0 grep -L “<string>Scott</string>”|xargs grep -l “Scott” |xargs grep -L “<key>Tags</key>”|xargs -I file /usr/libexec/PlistBuddy -c “add Tags array” file

4) Then go back to #2 and re-run it. Everything that has your text should be tagged with the text you choose.

Swanky! The only thing you have to watch out for here is the little l (little ell) looks a lot like a capital I (capital-eye) – might be best to copy this into a browser and set the font to Courier just to make sure before you run it, also, the last xargs does the changes, so skipping out on that might be smart. I can’t make any guarantees that it’ll work, but as far as I can tell, it works great!

YMMV. Careful.

For the want of pgrep on Mac OSX

I’ve got an issue at work, of course. I’ve got a Mac OSX xServer that has grown crotchety and so I’ve gotten to making things better by using killall on various running processes in order to “clean up the mess”. This is all fine and good and these processes respawn and the world goes back to normal and everything is fine, however I also want to renice this pesky command and give it a lower priority. While killall can do a search by name, renice requires a pid. The way you get pids is to run the ‘ps’ command, but this gives you a big pile of data and really all you want is just the pid itself, so you can pass that to renice.

So here’s how to get your cake and eat it too on Mac OSX Leopard Server:

1) First, change your shell – the default for root is /bin/sh, do this by issuing this command:

chsh -s /bin/bash root

2) Then you’ll need to give bash a profile, create a new file call it .bash_profile and fill it with this text:

[[ -s ~/.bashrc ]] && source ~/.bashrc

3) Next you’ll need to fill out that .bashrc because that contains the function you need to replicate pgrep:

pgrep() for arg; do ps aux|grep $1|grep -v grep|awk '{print $2}';done;

4) Log out and log back in and you’ll end up in bash, not sh, and you’ll have a new command at your disposal, pgrep. You can then use pgrep CommandName and it’ll spit out the pid related to what you are after.

5) Then you can use this new function with renice this way:

renice 20 `pgrep CommandName`

One thing to note here is that the ` character is the backtick character. You’ll find this hiding out in the upper left corner of your keyboard, it’s the unshifted tilde button.