Wednesday, November 28, 2012

Drafts and Dropbox Sync

I like Drafts on iOS. It’s simple, quick and efficient. However the way it appends to Dropbox annoys me.In my Dropbox I have one directory called nvALT which is where I keep all my miscellaneous text files. I rely on nvALT to find stuff in those files and nvALT will only act across a single directory. I have a scratch file in nvALT that I use for all text clipping I do. I use a LaunchBar shortcut to append selected text to that file.
Drafts will append text to a file in Dropbox also. The problem is that it always puts it into /Apps/Drafts/Journal.txt and that appears to be a hardcoded path.
I don’t want two sets of text clippings, and I don’t want one of them outside of my nvALT directory. This sent me on a path to see if I could somehow move all the text into my scratch file in nvALT. I’m a rotten coder but I managed to hack something up that does the job. It’s in two parts. The first part is a bash script that checks to see if the /Apps/Drafts/ directory content has changed and then checks if the Journals.txt file has changed. If it has it appends the contents to scratch file.
The second part for Mac users is a plist that loads the script and keeps it chugging away.
As always, use these scripts at your own risk and I’d love to hear all the ways that you find to make them smarter, better, more elegant and more robust. I told you I’m a crap coder.

#!/bin/bash

#This script is designed to combine two scratch files that I use for holding snippets of text.
#They are both in ~/Documents/Dropbox. The first is in the /nvALT directory and is called ScratchX.txt I use it for clipping text to on Mac
#The second is used by Drafts on iOS and is in the /Apps/Drafts directory and is called Journal.txt
#I create a dummy JournalB.txt and use diff to check whether it and Journal.txt remain the same. If they differ we copy the diff to ScratchX.txt
#It requires a change in the /Apps/Drafts directory to trigger anything further.

#Create a directory listing file
touch ~/tmp/dirb.tmp


while true; do
  #List the directory and put it into dira.tmp 
  ls -l /Users/criticalold/Documents/Dropbox/Apps/Drafts > /tmp/dira.tmp
  #Check whether it differs from the last directory listing and tell us if it does
  diff /tmp/dira.tmp /tmp/dirb.tmp || echo 
  #Then do a diff on the files and send the diff to grep to get rid of some numbers that diff creates then append to ScratchX.txt.
  diff /Users/criticalold/Documents/Dropbox/Apps/Drafts/Journal.txt /Users/criticalold/Documents/Dropbox/Apps/Drafts/JournalB.txt | grep '^<' >> /Users/criticalold/Documents/Dropbox/nvALT/ScratchX.txt
  #Do our housekeeping for next time by making the *b files the same as the *a or the Journal file so that we do a valid comparison.
  cp /Users/criticalold/Documents/Dropbox/Apps/Drafts/Journal.txt /Users/criticalold/Documents/Dropbox/Apps/Drafts/JournalB.txt
  cp /tmp/dira.tmp /tmp/dirb.tmp
  #Wait for 25 seconds before checking again.
  sleep 25 
done
 
Now for the 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>com.critical.dropbox</string>
    <key>Program</key>
    <array>
        <string>/Users/criticalold/Applications/Utilities/Terminal.app</string>
    </array>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/criticalold/Documents/ShellScripts/checkdirectory.sh</string>
    </array>
    <key>KeepAlive</key>
        <true/>
    </dict>
</plist>