Skip to content

Instantly share code, notes, and snippets.

@rpavlik
Created February 28, 2012 17:05
Show Gist options
  • Save rpavlik/1933707 to your computer and use it in GitHub Desktop.
Save rpavlik/1933707 to your computer and use it in GitHub Desktop.
"GUI" version - Move new PDF files out of your downloads folder and into your reference manager's watched folder.
#!/bin/bash
# Requires libinotify-bin (for notify-send) and YAD or Zenity
MYZENITY=$(which yad || which zenity) || (echo "Could not find neither YAD nor Zenity - can't do the gui stuff!"; exit 1)
echo "Using ${MYZENITY} to create notification area icon"
MYNOTIFYSEND=$(which notify-send) || (echo "Could not find notify-send (usually in a package like libnotify-bin) - can't do the gui stuff!"; exit 1)
echo "Using ${MYNOTIFYSEND} to send desktop notifications"
PIPE=$(mktemp -u --tmpdir $(basename ${0}).XXXXXXXX)
# SRC and DEST normalized to be the directory name, with no trailing slash.
SRC=$(cd "${SRCDIR:-${HOME}/Downloads/}" && pwd)
DEST=$(cd "${DESTDIR:-${HOME}/Dropbox/Bibliography/Mendeley Watched Folder/}" && pwd)
function computeDestinationName()
{
srcname="${1}"
destname="${DEST}/${srcname}"
while [ -f "${destname}" ]; do
destname="${DEST}/${RANDOM}${srcname}"
done
echo "${destname}"
}
function movePdf()
{
srcname="${SRC}/${1}"
destname=$(computeDestinationName "$1")
${MYNOTIFYSEND} -u normal "Automove Downloaded Papers" "Moved $1 to ${destname}"
mv "${srcname}" "${destname}"
}
function on_exit () {
kill $WAITPID
rm -f $PIPE
}
mkfifo $PIPE
exec 3<> $PIPE
trap on_exit EXIT
inotifywait -e create --format %f --exclude '[^.][^p][^d][^f]$' -m "${SRCDIR}/" >>$PIPE &
WAITPID=$!
(
while read fn; do
movePdf "${fn}"
done
) <$PIPE &
${MYNOTIFYSEND} -u normal "Automove Downloaded Papers" "Will watch ${SRC} for new PDF files, and move them to ${DEST}. Click notification icon to exit."
${MYZENITY} --notification --text="Watching for PDF files - click to stop watching"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment