Skip to content

Instantly share code, notes, and snippets.

@Mrtenz
Last active May 20, 2018 10:34
Show Gist options
  • Save Mrtenz/e5ee34947f44df5336526d5d51c4af3e to your computer and use it in GitHub Desktop.
Save Mrtenz/e5ee34947f44df5336526d5d51c4af3e to your computer and use it in GitHub Desktop.
#!/bin/sh
# This screenshot uploading script is made for X and requires maim, xdotool, curl, xdg-open, xclip and probably some other stuff.
# Note that this is probably the first 'big' Bash script I have ever written. Feel free to fork it and make it better.
SCREENSHOT_URL="https://example.com/"
SCREENSHOT_KEY="CHANGE THIS"
SCREENSHOT_FILE="temp.png"
SCREENSHOT_OPEN_BROWSER=true
function usage {
echo ""
echo "Usage:"
echo " screenshot [all/capture/window]"
echo ""
exit
}
if [ $# -lt 1 ]; then
usage
fi
if [ $1 = "all" ]; then
# Thanks to naelstrof - https://gist.github.com/naelstrof/f9b74b5221cdc324c0911c89a47b8d97
MONITORS=$(xrandr | grep -o '[0-9]*x[0-9]*[+-][0-9]*[+-][0-9]*')
XMOUSE=$(xdotool getmouselocation | awk -F "[: ]" '{print $2}')
YMOUSE=$(xdotool getmouselocation | awk -F "[: ]" '{print $4}')
for mon in ${MONITORS}; do
MONW=$(echo ${mon} | awk -F "[x+]" '{print $1}')
MONH=$(echo ${mon} | awk -F "[x+]" '{print $2}')
MONX=$(echo ${mon} | awk -F "[x+]" '{print $3}')
MONY=$(echo ${mon} | awk -F "[x+]" '{print $4}')
if (( ${XMOUSE} >= ${MONX} )); then
if (( ${XMOUSE} <= ${MONX}+${MONW} )); then
if (( ${YMOUSE} >= ${MONY} )); then
if (( ${YMOUSE} <= ${MONY}+${MONH} )); then
maim -u -g "${MONW}x${MONH}+${MONX}+${MONY}" > ${SCREENSHOT_FILE}
fi
fi
fi
fi
done
elif [ $1 = "window" ]; then
maim -ui $(xdotool getactivewindow) | convert - \( +clone -background black -shadow 5x10+0+10 \) +swap -background none -layers merge -fuzz 1% -trim +repage ${SCREENSHOT_FILE}
elif [ $1 = "capture" ]; then
maim -su > ${SCREENSHOT_FILE}
else
usage
fi
if [ ! -f ${SCREENSHOT_FILE} ]; then
exit
fi
SCREENSHOT_OUTPUT=$(curl -sb -H --form "screenshot=@${SCREENSHOT_FILE}" --form "key=${SCREENSHOT_KEY}" ${SCREENSHOT_URL})
echo -e '\a'
# rm ${SCREENSHOT_FILE}
if [ $SCREENSHOT_OPEN_BROWSER = true ]; then
xdg-open $SCREENSHOT_OUTPUT &>/dev/null
fi
notify-send "Screenshot has been uploaded." "${SCREENSHOT_OUTPUT}"
echo -n ${SCREENSHOT_OUTPUT} | xclip -sel clip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment