screenshot-spmenu/screenshot-spmenu

131 lines
4.8 KiB
Bash
Executable file

#!/bin/sh
# screenshot-spmenu
# Take screenshots using wayshot/maim with spmenu
# See LICENSE file for copyright and license details.
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER="spmenu"
[ -z "$PREVIEW_IMAGE" ] && PREVIEW_IMAGE="true"
[ -z "$IMAGE_SIZE" ] && IMAGE_SIZE="500"
[ -z "$CACHE_IMAGE" ] && CACHE_IMAGE="false"
[ -z "$SCREENSHOT_CONFIG_DIR" ] && SCREENSHOT_CONFIG_DIR="$HOME/.config/screenshot-spmenu"
[ -z "$IMAGEHOST" ] && IMAGE_HOST="imgbb"
[ -z "$SHOW_CURSOR" ] && SHOW_CURSOR="true"
[ -z "$DEFAULT_SCREENSHOT_DIRECTORY" ] && DEFAULT_SCREENSHOT_DIRECTORY=$HOME/Screenshots # Default screenshot path
[ -z "$FORMAT" ] && FORMAT="+%T_%D"
[ -z "$PREFIX" ] && PREFIX="screenshot-"
[ -z "$SEND_NOTIF" ] && SEND_NOTIF="true"
take_scr() {
sleep "$2"
if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then
maim -s"${ARG2}"B > "$1" || exit 1
elif [ -x "$(command -v slurp)" ] && [ -x "$(command -v grim)" ]; then
grim -g "$(slurp)" - > "$1" || exit 1
elif [ -x "$(command -v slurp)" ] && [ -x "$(command -v wayshot)" ]; then
wayshot --stdout -s "$(slurp)" > "$1" || exit 1
else
printf "Failed to take screenshot: Appropriate screenshot tools not found.\n" > /dev/stderr
fi
}
take_full() {
sleep "$2"
if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then
maim "${ARG2}"B > "$1" || exit 1
elif [ -x "$(command -v grim)" ]; then
grim - > "$1" || exit 1
elif [ -x "$(command -v wayshot)" ]; then
wayshot --stdout > "$1" || exit 1
else
printf "Failed to take screenshot: Appropriate screenshot tools not found.\n" > /dev/stderr
fi
}
send_notif() {
command -v notify-send > /dev/null && [ "$SEND_NOTIF" = "true" ] && notify-send "$1"
}
upload_image() {
IMAGE="$(curl -s -F source=@"$1" -F "type=file" -F "action=upload" "https://imgbb.com/json" | sed "s/\\\\//g; s/\"/\\n/g" | grep -m 1 -A 2 url | tail -n 1)"
printf "%s\n" "$IMAGE" && return 0 || return 1
}
mkdir -p "$SCREENSHOT_CONFIG_DIR"
[ -e "$SCREENSHOT_CONFIG_DIR/screenshotrc" ] && . "$SCREENSHOT_CONFIG_DIR/screenshotrc" || cat << CONFIG > "$SCREENSHOT_CONFIG_DIR/screenshotrc"
# screenshot-spmenu config file
RUNLAUNCHER="spmenu" # Run launcher to use
SHOW_CURSOR="true" # Show cursor or not (true/false)
DEFAULT_SCREENSHOT_DIRECTORY="$HOME/Screenshots" # Default screenshot path
PREFIX="screenshot-" # Screenshot prefix
FORMAT="+%T_%D" # Screenshot format
PREVIEW_IMAGE=true
IMAGE_SIZE="500"
CACHE_IMAGE="false"
CONFIG
# get display protocol
if [ -n "$WAYLAND_DISPLAY" ]; then
x11=false
elif [ -n "$DISPLAY" ]; then
x11=true
else
printf "no valid display server running, is \$DISPLAY or \$WAYLAND_DISPLAY set?\n" && exit 1
fi
# showcursor
[ "$SHOW_CURSOR" = "true" ] && ARG2="" || ARG2=u
mkdir -p "$DEFAULT_SCREENSHOT_DIRECTORY"
print_help() {
cat << EOF
screenshot-spmenu usage
-t <time> Wait <time> seconds and then take the screenshot.
-f Take a full screen screenshot instead of selecting manually.
-s Take a screenshot, allowing the user to select a section manually.
-t -f Wait <second argument> seconds and then take a full screen screenshot.
-h View this help screen.
No arguments will print this help.
EOF
}
rm -f /tmp/screenshot*
# check arguments
case "$1" in
"") print_help; exit 0 ;;
"-f") take_full "/tmp/screenshot-$DATE" "0" ;;
"-s") take_scr "/tmp/screenshot-$DATE" "0" ;;
*) print_help; exit 0 ;;
esac
[ "$CACHE_IMAGE" = "true" ] && CACHE_IMAGE="-gc" || CACHE_IMAGE="-ngc"
[ "$PREVIEW_IMAGE" = "true" ] && IMGPREFIX="img://" && IMG="/tmp/screenshot-$DATE" && IMGARG="-is $IMAGE_SIZE $CACHE_IMAGE"
# user action
[ ! -s "/tmp/screenshot-$DATE" ] && exit 0
[ -e "/tmp/screenshot-$DATE" ] && u_i="$(printf "${IMGPREFIX}${IMG}\tPNG\n${IMGPREFIX}${IMG}\tURL\n${IMGPREFIX}${IMG}\tSave\n" | $RUNLAUNCHER -l 3 -g 1 $IMGARG -p "Copy to clipboard as:")"
if [ "$x11" = "true" ] && [ -x "$(command -v maim)" ]; then
img_clipboard_cmd="xclip -selection clipboard -t image/png"
clipboard_cmd="xclip -selection clipboard"
else
img_clipboard_cmd="wl-copy"
clipboard_cmd="wl-copy"
fi
# perform actions based on user input
case "$u_i" in
"PNG") $img_clipboard_cmd < "/tmp/screenshot-$DATE" && send_notif "Screenshot copied to clipboard." ;;
"URL") printf "\n" | $clipboard_cmd && upload_image "/tmp/screenshot-$DATE" | $clipboard_cmd && send_notif "Screenshot copied to clipboard." ;;
"Save") SAVEDIR=$(printf "%s/screenshot-%s.png" "$DEFAULT_SCREENSHOT_DIRECTORY" "$(date "$FORMAT" | sed "s|:|-|g" | sed "s|/||g")" | $RUNLAUNCHER -l 1 -g 1 -p "Where do you want to save it? (Including filename)" -im) && cat /tmp/screenshot-"$DATE" > "$SAVEDIR" && send_notif "Screenshot saved to $SAVEDIR" ;;
"") rm -f "/tmp/screenshot*"; exit 0 ;;
*) print_help "$@"; exit 0 ;;
esac