2019-10-09 11:10:23 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
## Grimshot: a helper for screenshots within sway
|
|
|
|
## Requirements:
|
|
|
|
## - `grim`: screenshot utility for wayland
|
|
|
|
## - `slurp`: to select an area
|
|
|
|
## - `swaymsg`: to read properties of current window
|
|
|
|
## - `wl-copy`: clipboard utility
|
2020-01-31 11:35:40 +00:00
|
|
|
## - `jq`: json utility to parse swaymsg output
|
2019-10-09 11:10:23 +00:00
|
|
|
## - `notify-send`: to show notifications
|
|
|
|
## - `mktemp`: to create a temporary file
|
|
|
|
## Those are needed to be installed, if unsure, run `grimshot check`
|
|
|
|
##
|
|
|
|
## Examples:
|
|
|
|
## `grimshot copy win` - to copy current window
|
|
|
|
## `grimshot save area` - to select area and save it to default file (Pictures/Grimshot-$datetime.png)
|
|
|
|
## `grimshot save screen ~/screenshot.png` - to save screenshot under ~/screenshot.png
|
2020-01-27 23:09:18 +00:00
|
|
|
## `grimshot save output ~/screenshot.png` - to save screenshot under ~/screenshot.png
|
2019-10-09 11:10:23 +00:00
|
|
|
## `grimshot` - usage
|
|
|
|
## `grimshot check` - verify if tools are installed
|
|
|
|
|
2020-05-01 08:51:18 +00:00
|
|
|
getTargetDirectory() {
|
|
|
|
test -f ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs && \
|
|
|
|
source ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs
|
|
|
|
|
|
|
|
echo ${XDG_SCREENSHOTS_DIR:-${XDG_PICTURES_DIR:-$HOME}}
|
|
|
|
}
|
|
|
|
|
2019-10-09 11:10:23 +00:00
|
|
|
ACTION=${1:-usage}
|
|
|
|
SUBJECT=${2:-screen}
|
2020-05-01 08:55:17 +00:00
|
|
|
FILE=${3:-$(getTargetDirectory)/$(date -Ins).png}
|
2020-05-01 08:51:18 +00:00
|
|
|
|
2020-05-01 09:08:28 +00:00
|
|
|
if [ "$ACTION" != "save" ] && [ "$ACTION" != "copy" ] && [ "$ACTION" != "check" ]; then
|
2019-10-09 11:10:23 +00:00
|
|
|
echo "Usage:"
|
2020-05-01 09:08:28 +00:00
|
|
|
echo " grimshot (copy|save) [win|screen|output|area] [FILE]"
|
2019-10-09 11:10:23 +00:00
|
|
|
echo " grimshot check"
|
2020-05-01 09:08:28 +00:00
|
|
|
echo " grimshot usage"
|
|
|
|
echo ""
|
|
|
|
echo "Commands:"
|
|
|
|
echo " copy: Copy the screenshot data into the clipboard."
|
|
|
|
echo " save: Save the screenshot to a regular file."
|
|
|
|
echo " check: Verify if required tools are installed and exit."
|
|
|
|
echo " usage: Show this message and exit."
|
|
|
|
echo ""
|
|
|
|
echo "Targets:"
|
|
|
|
echo " win: Currently active window."
|
|
|
|
echo " screen: All visible outputs."
|
|
|
|
echo " output: Currently active output."
|
|
|
|
echo " region: Manually select a region."
|
2019-10-09 11:10:23 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
notify() {
|
|
|
|
notify-send -t 3000 -a grimshot "$@"
|
|
|
|
}
|
|
|
|
notifyOk() {
|
|
|
|
TITLE=${2:-"Screenshot"}
|
|
|
|
MESSAGE=${1:-"OK"}
|
|
|
|
notify "$TITLE" "$MESSAGE"
|
|
|
|
}
|
|
|
|
notifyError() {
|
|
|
|
TITLE=${2:-"Screenshot"}
|
|
|
|
MESSAGE=${1:-"Error taking screenshot with grim"}
|
|
|
|
notify -u critical "$TITLE" "$MESSAGE"
|
|
|
|
}
|
|
|
|
|
|
|
|
die() {
|
|
|
|
MSG=${1:-Bye}
|
|
|
|
notifyError "Error: $MSG"
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
|
|
|
COMMAND=$1
|
2019-11-17 15:50:26 +00:00
|
|
|
if command -v "$COMMAND" > /dev/null 2>&1; then
|
2019-10-09 11:10:23 +00:00
|
|
|
RESULT="OK"
|
|
|
|
else
|
|
|
|
RESULT="NOT FOUND"
|
|
|
|
fi
|
|
|
|
echo " $COMMAND: $RESULT"
|
|
|
|
}
|
|
|
|
|
|
|
|
takeScreenshot() {
|
|
|
|
FILE=$1
|
|
|
|
GEOM=$2
|
2020-01-27 23:09:18 +00:00
|
|
|
OUTPUT=$3
|
|
|
|
if [ ! -z "$OUTPUT" ]; then
|
|
|
|
grim -o "$OUTPUT" "$FILE" || die "Unable to invoke grim"
|
2020-01-27 22:44:38 +00:00
|
|
|
elif [ -z "$GEOM" ]; then
|
2019-10-09 11:10:23 +00:00
|
|
|
grim "$FILE" || die "Unable to invoke grim"
|
|
|
|
else
|
|
|
|
grim -g "$GEOM" "$FILE" || die "Unable to invoke grim"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$ACTION" = "check" ] ; then
|
|
|
|
echo "Checking if required tools are installed. If something is missing, install it to your system and make it available in PATH..."
|
|
|
|
check grim
|
|
|
|
check slurp
|
|
|
|
check swaymsg
|
|
|
|
check wl-copy
|
|
|
|
check jq
|
|
|
|
check notify-send
|
|
|
|
check mktemp
|
|
|
|
exit
|
|
|
|
elif [ "$SUBJECT" = "area" ] ; then
|
|
|
|
GEOM=$(slurp -d)
|
|
|
|
WHAT="Area"
|
|
|
|
elif [ "$SUBJECT" = "win" ] ; then
|
|
|
|
FOCUSED=$(swaymsg -t get_tree | jq -r 'recurse(.nodes[]?, .floating_nodes[]?) | select(.focused)')
|
|
|
|
GEOM=$(echo "$FOCUSED" | jq -r '.rect | "\(.x),\(.y) \(.width)x\(.height)"')
|
|
|
|
APP_ID=$(echo "$FOCUSED" | jq -r '.app_id')
|
|
|
|
WHAT="$APP_ID window"
|
|
|
|
elif [ "$SUBJECT" = "screen" ] ; then
|
|
|
|
GEOM=""
|
|
|
|
WHAT="Screen"
|
2020-01-27 23:09:18 +00:00
|
|
|
elif [ "$SUBJECT" = "output" ] ; then
|
2020-01-27 22:44:38 +00:00
|
|
|
GEOM=""
|
2020-01-27 23:09:18 +00:00
|
|
|
OUTPUT=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused)' | jq -r '.name')
|
|
|
|
WHAT="$OUTPUT"
|
2019-10-09 11:10:23 +00:00
|
|
|
else
|
|
|
|
die "Unknown subject to take a screen shot from" "$SUBJECT"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$ACTION" = "copy" ] ; then
|
|
|
|
TMP=$(mktemp) || die "Unable to create temp file: is mktemp installed?"
|
2020-01-27 23:09:18 +00:00
|
|
|
takeScreenshot "$TMP" "$GEOM" "$OUTPUT"
|
2019-10-09 11:10:23 +00:00
|
|
|
wl-copy --type image/png < "$TMP" || die "Clipboard error"
|
|
|
|
rm "$TMP"
|
|
|
|
notifyOk "$WHAT copied to buffer"
|
|
|
|
else
|
2020-01-27 23:09:18 +00:00
|
|
|
if takeScreenshot "$FILE" "$GEOM" "$OUTPUT"; then
|
2019-10-09 11:10:23 +00:00
|
|
|
TITLE="Screenshot of $SUBJECT"
|
|
|
|
MESSAGE=$(basename "$FILE")
|
|
|
|
notifyOk "$MESSAGE" "$TITLE"
|
|
|
|
else
|
|
|
|
notifyError "Error taking screenshot with grim"
|
|
|
|
fi
|
|
|
|
fi
|