Friday, January 4, 2013

Clipboard to Google Calendar

I enjoy a good shell script, so here's what I cobbled together. This script requires Mac OS X (every version so far has pbpaste) with the Google CLI (gdata-python-client, I used version 2.0.17) installed. The basic command is google calendar add --cal Calendar "Event 1/4/2013 at 3:00 PM in Location" but I used a script for error checking, and I only have to scroll back to the last error to see all of the problematic entries.

#! /bin/bash

## This script sends the clipboard to Google Calendar one line at a time.
## REQUIRES: Google cli from http://code.google.com/p/gdata-python-client/
##
## Version 1.0  2013-01-04  nethope
##
## 1.0  2013-01-04  initial
VERSION="1.0"
NAME=${0##*/}

###
## Variables
#
# 0 for no DEBUG
typeset -i DEBUG=1
if (( $DEBUG )); then /bin/echo Debugging on...; fi

###
## Parameters
#

## Paths
ECHO=/bin/echo
GOOGLE=/usr/local/bin/google
PBPASTE=/usr/bin/pbpaste

###
## main()
#
if (( $DEBUG )); then $ECHO Script ${NAME} running; fi

FAILURES="" NEWLINE=\\n
typeset -i TALLY=0
$PBPASTE | while read -r LINE
do
  $GOOGLE calendar add --cal Calendar "${LINE}"
  EXIT_STATE=$?
  if (( $EXIT_STATE )); then
    TALLY+=1
    FAILURES+=${LINE}${NEWLINE}
    echo -e "${NEWLINE}!!! Failed on line"
    echo -e "      ${LINE}${NEWLINE}"
    if (( $DEBUG )); then
      echo -e "  Errors add up to ${TALLY}"
      echo -e "  Failed lines are"
      echo -e "${FAILURES}${NEWLINE}"
    fi
  fi
done

# use exit code values of 0 (success) and 64 through 113 (or 125)
exit 0

No comments:

Post a Comment