Skip to content

Instantly share code, notes, and snippets.

@miyl
Last active June 15, 2024 19:37
Show Gist options
  • Save miyl/320c16141caf2813a72ad39e2acd4a3b to your computer and use it in GitHub Desktop.
Save miyl/320c16141caf2813a72ad39e2acd4a3b to your computer and use it in GitHub Desktop.
Screenshot utility script for binding to PrintScreen, using grim and slurp under the hood
#! /usr/bin/env sh
# Screenshot program wrapper, pre-setting path.
# Allows you to select whether to save to disk or copy to clipboard, and additionally whether to select an area or
# capture the full screen.
#
# Suggested usage:
# Make a keybind for it, such as the PrintScreen key.
#
# Prerequisites:
# - grim: For taking screenshots
# - slurp: For selecting an area to screenshot
# - fuzzel, wofi or rofi: For user interaction
# - wl-clipboard or xclip: To copy the screenshot to clipboard, if that option is selected
#
# ...and for now potentially imgur.sh for the automatic imgur uploading option
SCREENSHOT_DIR=$HOME/shots
PROG="grim"
# Create the screenshot dir if it doesn't already exist
[ ! -f "$SCREENSHOT_DIR" ] && mkdir --parents "$SCREENSHOT_DIR"
if [ -n "$WAYLAND_DISPLAY" ]; then
#PROMPT_PROG="wofi -dmenu -p"
PROMPT_PROG="fuzzel --dmenu --prompt"
else
PROMPT_PROG="rofi -dmenu -p"
fi
success_msg_file() {
notify-send "mim" "Screenshot saved to $1"
}
success_msg_clipboard() {
notify-send "mim" "The image has been copied to the clipboard"
}
option_clipboard_file() {
printf "%s\n" "yank" "save" | $PROMPT_PROG "Save screenshot to file or copy to clipboard? "
}
option_fullscreen_area() {
printf "%s\n" "area" "fullscreen" | $PROMPT_PROG "Fullscreen or select an area? "
}
get_file_name() {
$PROMPT_PROG "Please enter a screenshot file name, without extension: "
}
# Determine which type of screenshot is wanted
OPTION_CLIPBOARD_FILE=$(option_clipboard_file)
OPTION_FULLSCREEN_AREA=$(option_fullscreen_area)
[ -z "$OPTION_CLIPBOARD_FILE" ] || [ -z "$OPTION_FULLSCREEN_AREA" ] && exit 1
sleep 0.2 # Sleep a bit so the rofi windows themselves are gone
if [ "save" = "$OPTION_CLIPBOARD_FILE" ]; then # Save to file
[ "$OPTION_CLIPBOARD_FILE" = "save" ] && SHOT_NAME=$(get_file_name).png
PTH=$SCREENSHOT_DIR/${SHOT_NAME}
sleep 0.2 # Sleep a bit so the rofi windows themselves are gone
if [ "fullscreen" = "$OPTION_FULLSCREEN_AREA" ]; then # Take screenshot of the full screen
$PROG "$PTH" && success_msg_file "$PTH"
else # Take screenshot of a region
$PROG -g "$(slurp)" "$PTH" && success_msg_file "$PTH"
fi
else # Yank to clipboard
if [ -n "$WAYLAND_DISPLAY" ]; then
YANK_CMD='wl-copy -t image/png'
else
YANK_CMD='xclip -selection clipboard -t image/png'
fi
if [ "fullscreen" = "$OPTION_FULLSCREEN_AREA" ]; then # Take screenshot of the full screen
$PROG - | $YANK_CMD && success_msg_clipboard "$PTH"
else # Take screenshot of a region, passing no additional arguments
$PROG -g "$(slurp)" - | $YANK_CMD && success_msg_clipboard "$PTH"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment