Skip to content

Instantly share code, notes, and snippets.

@jsherer
Forked from jasonbouffard/screenshot-to-dropbox
Created August 17, 2012 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsherer/3378620 to your computer and use it in GitHub Desktop.
Save jsherer/3378620 to your computer and use it in GitHub Desktop.
OSX Bash script to snap an interactive screenshot into your Dropbox public folder and copy the public url to your clipboard.
#!/bin/bash
#
# Copyright (c) 2012, Jason Bouffard, Jordan Sherer
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
###
#
# Screenshot2Dropbox - Send screenshot to Dropbox via keyboard shortcut OSX and
# copy the public url (shortened) to the clipboard.
#
# It's pretty easy to add a global keyboard shortcut to this in OSX
# 1. Open Automator
# 2. Select Service
# 3. Add shell script
# 4. Put in the path to this script
# 5. Save as something useful "Screen Shot to Dropbox"
# 6. System Preferences -> Keyboard
# 7. Keyboard Shortcuts
# 8. Services
# 9. Find "Screen shot to Dropbox"
# 10. Click to the right of the name
# 11. Add a shortcut. I used Shift+Command+5.
#
###
DATE=$(date +"%Y-%m-%d");
TIME=$(date +"%I.%M.%S");
AMPM=$(date +"%p");
FILENAME=Screen\ shot\ $DATE\ at\ $TIME\ $AMPM.png;
# The number in public urls is the dropbox user id:
# e.g., http://dl.dropbox.com/u/{NUMBERHERE}/
# You can provide your user id by passing it in as the first argument
# or you can export DROPBOX_USER_ID in your .profile or hardcode it
# here by replacing YOURDROPBOXUSERID
DB_USER_ID=${1-${DROPBOX_USER_ID-YOURDROPBOXUSERID}}
# The path to the dropbox directory
DB_BASE_DIR=~/Dropbox;
# The public directory to store screenshots
DB_PUBLIC_DIR=$DB_BASE_DIR/Public/Screenshots;
# The base URL for public screenshots
DB_BASE_URL=http://dl.dropbox.com/u;
# Capture the screenshot and save it to the public directory
screencapture -i "$DB_PUBLIC_DIR/$FILENAME";
# Check to make sure the file exists after screencapture
if [ -f "$DB_PUBLIC_DIR/$FILENAME" ];
then
# Use python to urlencode the public url
URL_FILENAME=$(python -c "import urllib; print urllib.quote('''$FILENAME''')");
URL_LONG="$DB_BASE_URL/$DB_USER_ID/Screenshots/$URL_FILENAME"
# copy long url for use before the shortener returns
echo $URL_LONG | pbcopy;
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 [ -n "$URL_SHORT" ]; then
echo $URL_SHORT | pbcopy;
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment