From 6505db591d53002b1a013a5e5fd83dbce398fe2b Mon Sep 17 00:00:00 2001 From: foo-dogsquared Date: Wed, 21 Aug 2019 20:34:20 +0800 Subject: [PATCH] Revise the screenshot shell script --- .scripts/maim-screenshot.sh | 83 ++++++++++++++++++++++----- .scripts/maim-selection-screenshot.sh | 49 ---------------- 2 files changed, 70 insertions(+), 62 deletions(-) delete mode 100644 .scripts/maim-selection-screenshot.sh diff --git a/.scripts/maim-screenshot.sh b/.scripts/maim-screenshot.sh index e78c28f..79c040e 100644 --- a/.scripts/maim-screenshot.sh +++ b/.scripts/maim-screenshot.sh @@ -3,7 +3,9 @@ # Simply captures an image with regional selection. # Here are the dependencies needed to successfully -# run this script: +# run this script (as of creating this script): +# * cat (version 8.31; also unlikely to be missing) +# * slop (version 7.4) # * maim (version 5.5.3) # * date (version 8.31) @@ -13,30 +15,85 @@ green="\u001b[32m" reset="\u001b[0m" function error_cleanup() { - rm "$pic_filepath" + # rm "$pic_filepath" printf "$red An error occurred on line $1\n $reset" } -delay=0 +help_section=" +Simply captures a screenshot with maim. -if [[ -n "$1" || "$1" -gt 0 ]]; then - delay=$1 -fi +Usage: $0 [-o/--output ] [-s/--select] +[-d/--delay ] [--help] + +Options: +--help - show the help section +-s, --select - set the screenshot capture to selection mode +-d, --delay - set a delay for the capture + (in seconds) +-o, --output - set the output path for the picture; + when given no output, the picture will + be moved to \$PICTURES_DIRECTORY (or + at \$HOME/Pictures if it's missing) +" # setting up a exit trap in case of error trap 'error_cleanup $LINENO' ERR -pic_directory=${PICTURES_DIRECTORY:-"$HOME/Pictures"} +while [[ $# -gt 0 ]] +do + case $1 in + -h|--help) + echo "$help_section" + exit 0;; + -d|--delay) + DELAY="$2" + shift + shift;; + -s|--select) + SELECT=1 + shift;; + -o|--output) + OUTPUT="$2" + shift + shift;; + *) + shift;; + esac +done -date_format=$(date +%F-%H-%M-%S) +set -- "${POSITIONAL[@]}" # restore positional parameters -pic_filepath="$pic_directory/$date_format.png" +if [[ -n "$OUTPUT" ]]; then + pic_filepath=$OUTPUT +else + pic_directory=${PICTURES_DIRECTORY:-"$HOME/Pictures"} + date_format=$(date +%F-%H-%M-%S) -if [[ $delay -gt 0 ]]; then - notify-send "Delayed screenshot" "A delayed screenshot is about to be taken in $delay seconds." --expire-time=$(( ($delay * 1000) - 1000)) + pic_filepath="$pic_directory/$date_format.png" fi -maim_process=$(maim $pic_filepath --hidecursor --delay=$delay) +echo "$pic_filepath" +maim_command="maim $pic_filepath " + +if [[ $SELECT -eq 1 ]]; then + geometry_coordinates=$(slop) + + if [[ -z $geometry_coordinates ]]; then + exit 1; + fi + + maim_command+="--geometry=$geometry_coordinates " + + if [[ -n $OUTPUT ]]; then + pic_filepath="$pic_directory/$date_format-$geometry_coordinates.png" + fi +fi + +if [[ $DELAY -gt 0 ]]; then + maim_command+="--delay=$DELAY " + notify-send "Delayed screenshot" "A delayed screenshot is about to be taken in $DELAY seconds." --expire-time=$(( ($DELAY * 1000) - 1000 )) +fi + +maim_process=$($maim_command) notify-send "Screenshot taken" "It is saved at $pic_filepath." - diff --git a/.scripts/maim-selection-screenshot.sh b/.scripts/maim-selection-screenshot.sh deleted file mode 100644 index a9303a8..0000000 --- a/.scripts/maim-selection-screenshot.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh - -# Simply captures an image with regional selection. - -# Here are the dependencies needed to successfully -# run this script (as of creating this script): -# * slop (version 7.4) -# * maim (version 5.5.3) -# * date (version 8.31) - -# setting up variables for the color -red="\u001b[31m" -green="\u001b[32m" -reset="\u001b[0m" - -function error_cleanup() { - rm "$pic_filepath" - printf "$red An error occurred on line $1\n $reset" -} - -# setting up a exit trap in case of error -trap 'error_cleanup $LINENO' ERR - -delay=0 - -if [[ -n "$1" || "$1" -gt 0 ]]; then - delay=$1 -fi - -pic_directory=${PICTURES_DIRECTORY:-"$HOME/Pictures"} - -geometry_coordinates=$(slop) - -if [[ -z $geometry_coordinates ]]; then - exit 1; -fi - -date_format=$(date +%F-%H-%M-%S) - -pic_filepath="$pic_directory/$date_format-$geometry_coordinates.png" - -if [[ $delay -gt 0 ]]; then - notify-send "Delayed screenshot" "A delayed screenshot is about to be taken in $delay seconds." --expire-time=$(( ($delay * 1000) - 1000 )) -fi - -maim_process=$(maim $pic_filepath --hidecursor --delay=$delay --geometry=$geometry_coordinates) - -notify-send "Screenshot taken" "It is saved at $pic_filepath." -