OSX Screenshot to Dropbox Tool

Posted on

My buddy Jordan and I like to hack on small utilities at work. Not too long ago we came up with a simple tool that will quickly allow you to take a screenshot in OSX and put it in Dropbox's public folder. The script will also copy the public shortened URL into your clipboard for quick use in a tweet, IM, or email!

You can clone the latest script here: https://gist.github.com/1051544

#!/bin/bash

DATE=$(date +"%Y-%m-%d");
TIME=$(date +"%I.%M.%S");
AMPM=$(date +"%p");
FILENAME=Screen\ shot\ $DATE\ at\ $TIME\ $AMPM.png;

# You can export you DROPBOX_USER_ID in your .profile
# or hardcode it here by replacing YOURDROPBOXUSERID
DB_USER_ID=${DROPBOX_USER_ID-YOURDROPBOXUSERID};
# Make sure you create the Screenshots directory in
# your public directory
DB_BASE_DIR=~/Dropbox/Public/Screenshots;
DB_BASE_URL=http://dl.dropbox.com/u;

screencapture -i "$DB_BASE_DIR/$FILENAME";

# user may have escaped out of the screencapture
if [ -f "$DB_BASE_DIR/$FILENAME" ];
then
    URL_FILENAME=$(python -c "import urllib; print urllib.quote('''$FILENAME''')");
    URL_LONG="$DB_BASE_URL/$DB_USER_ID/Screenshots/$URL_FILENAME";
    URL_SHORT=$(curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d "{\"longUrl\": \"$URL_LONG\"}" 2>/dev/null | grep goo.gl | cut -f 4 -d '"');

    if [ -z "$URL_SHORT" ]; then
        echo $URL_LONG | pbcopy;
    else
        echo $URL_SHORT | pbcopy;
    fi
fi