mirror of
https://github.com/frebib/dotfiles.git
synced 2024-06-14 12:57:23 +00:00
Joe Groocock
b52496bbb3
--active uses the active window as selection boundary, instead of the root window, or prompting for a window selection --clip adds the image to the clipboard and exits without uploading Signed-off-by: Joe Groocock <me@frebib.net>
84 lines
2.4 KiB
Bash
Executable File
84 lines
2.4 KiB
Bash
Executable File
#!/bin/bash
|
|
set -e
|
|
|
|
NOTIFY_APPNAME="$(basename "$0")"
|
|
NOTIFY_ICONPATH="/usr/share/icons/Xenlism-Wildfire/Apps/screenshot.svg"
|
|
|
|
FILENAME="$(date '+%s%N' | sha256sum | head -c7).png"
|
|
DIRECTORY="$HOME/pictures/screenshots"
|
|
|
|
SCP_HOST=frebib@Poseidon.nerdhouse.io
|
|
SCP_PATH=/services/www/files/frebib.net/s
|
|
URL_PATH=https://frebib.net/s
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
send_notification() {
|
|
title="$1"
|
|
body="$2"
|
|
replace_id="$3"
|
|
|
|
notify-send \
|
|
-a "$NOTIFY_APPNAME" \
|
|
-i "$NOTIFY_ICONPATH" \
|
|
"$title" "$body" \
|
|
$(if [ -n "$replace_id" ]; then printf "%s" "-r $replace_id"; fi) \
|
|
-p
|
|
}
|
|
|
|
# Make the screenshot directory
|
|
mkdir -p "$DIRECTORY"
|
|
|
|
# Take screenshot geometry
|
|
read -r winid geom < <(slop -f "%i %g") || :
|
|
if [ -z "$geom" -o -z "$winid" ]; then
|
|
send_notification "Screenshot error" "Failed to capture screenshot area" > /dev/null
|
|
exit 1
|
|
fi
|
|
|
|
out_path="$DIRECTORY/$FILENAME"
|
|
args=(-g "$geom")
|
|
|
|
# If selection is the root window
|
|
if xwininfo -id "$winid" | grep -qw root; then
|
|
isroot=1
|
|
args+=(-i "$winid")
|
|
elif [ "$1" == "--active" -o "$1" == "-a" ]; then
|
|
args+=(-i "$(xdotool getactivewindow)")
|
|
shift
|
|
fi
|
|
|
|
# Take the screenshot and save it
|
|
if ! error=$(maim -m 10 -u -f png ${args[@]} "$out_path" 2>&1); then
|
|
send_notification "Screenshot error" "Failed to take a screenshot:<br><br>$error" >/dev/null
|
|
exit 1
|
|
fi
|
|
|
|
# If the screenshot is a complete window, add a shadow
|
|
if [ "$isroot" != "1" ]; then
|
|
# but don't worry if it fails
|
|
convert "$out_path" \( +clone -background black -alpha set -channel Alpha -evaluate set 60% -shadow 100x16+0+0 \) +swap -background none -layers merge +repage "$out_path" || :
|
|
fi
|
|
|
|
if [ "$1" == "--clip" -o "$1" == "-c" ]; then
|
|
# Add image to clipboard
|
|
xclip -i "$out_path" -selection clipboard -t image/png
|
|
send_notification "Screenshot captured" "$FILENAME<br><br>The image is available in the clipboard"
|
|
|
|
exit 0
|
|
fi
|
|
|
|
notif_id=$(send_notification "Screenshot uploading" "$FILENAME" || true)
|
|
|
|
# 'Upload' the screenshot
|
|
if ! error=$(scp -C $out_path $SCP_HOST:$SCP_PATH 2>&1); then
|
|
send_notification "Screenshot upload error" "Failed to upload image:<br><br>$error" >/dev/null
|
|
exit 2
|
|
fi
|
|
|
|
# Add URL to clipboard
|
|
out_url="$URL_PATH/$FILENAME"
|
|
echo "$out_url" | xclip -i -selection clipboard
|
|
send_notification "Screenshot uploaded!" "$out_url" $notif_id >/dev/null || true
|
|
|