A Few New Scripts

Here’s a few nerdy bits I’ve been working on lately in my free time – just a few scripts that I’ve been using to take care of things on my Mac automatically.

The first is a one-line bash script that will automatically delete items from my Downloads folder that are more than a week old:

#!/bin/bash

find /Users/hepplerj/Downloads/* -mtime +7 -exec rm -rf {} \;

The only additional change I want to make to the script is to log what was deleted to a text file, but I’ll work on that on another evening. I automate the script using launchd (the OS X version of cron). You can copy the code to a file called com.yourusername.cleandownloads.plist and save it under ~/Library/LaunchAgents/. You’ll either need to log out and log back in, or run launchctl load ~/Library/LaunchAgents/com.yourusername.cleandownloads.plist to get it started. The code as set up here will run at 11:50pm every Saturday night:

<?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.hepplerj.cleandownloads</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/Users/hepplerj/scripts/cleandownloads.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Weekday</key>
        <integer>7</integer>
        <key>Hour</key>
        <integer>23</integer>
        <key>Minute</key>
        <integer>50</integer>
    </dict>
</dict>
</plist>

Perhaps not as elegant as cron, but it gets the job done.

I don’t know if anyone will find this useful, but it’s a nice method for me. I never remember (nor enjoy) having to comb through my Downloads. This will move everything to the Trash every week for me without having to think about it. It also puts something in the back of my mind that forces me to address a download immediately (e.g., if it’s a PDF of an article I intend to keep it needs to get filed) rather than have it sit in the folder indefinitely. The next step I want to figure out is to have the Trash empty automatically.

The other script I’m loving lately is drawn from the incomparable Brett Terpstra. Borrowing from Brett, I’ve added a command to my .bash_profile that allows me to log git commits to a text file. The script looks like this:

# wrapper for git to log commits to an nvALT note
# just an overall reminder of what I did all night
function ca(){
  note="/Users/hepplerj/Dropbox/notes/universalgitlog.txt"
  msg=$*
  path=$(pwd)
  before=$(cat "$note")
  echo "$(date '+%y-%m-%d %H:%M | ') (${path##*/}) $msg" > "$note"
  echo "$before" >> "$note"
  git commit -am "$msg"
}

I send the log to my notes folder in Dropbox, which is the same directory that is indexed by nvALT. So, whenever I’m doing a new commit to git, I can just write ca 'Here is my commit message', hit return, git will do its business and copy the commit message to the text file. A simple task, but a nice record of things I’ve completed for different projects.

June 07, 2012 @jaheppler