Skip to content

Instantly share code, notes, and snippets.

@miyl
Created March 28, 2024 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 e.g. Maim 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 Print Screen key.
#
# Prerequisites:
# rofi, maim, wl-copy (Wayland) or xclip (Xorg)
SCREENSHOT_DIR=$HOME/shots
PROG="maim"
#PROG="scrotm
#PROG="import -window root" # The -s argument below probably wouldn't work with this
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" | rofi -dmenu -p "Save screenshot to file or copy to clipboard? "
}
option_fullscreen_area() {
printf "%s\n" "area" "fullscreen" | rofi -dmenu -p "Fullscreen or select an area? "
}
get_file_name() {
rofi -dmenu -p "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 -s "$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 -s | $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