scripts/printscreen

182 lines
4.9 KiB
Plaintext
Raw Normal View History

2015-05-11 23:41:56 +02:00
#!/bin/sh
2020-12-01 16:27:21 +01:00
## Try to use flameshot to get a screenshot
2020-12-01 18:04:51 +01:00
### Select area work with both NORMAL and DELAYED_MODE.
2020-12-01 16:27:21 +01:00
### See https://github.com/flameshot-org/flameshot
## Or try to use scrot
2020-12-01 18:04:51 +01:00
### Select area work only in normal mode.
### In DELAYED_MODE, a capture of all screens is made.
2020-12-01 16:52:17 +01:00
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly NBARGS="${#}"
## Test if DEBUG is already defined (by parent script,…)
[ -z "${DEBUG}" ] && DEBUG=1
## Colors
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
DELAYED_MODE="1"
2020-12-01 16:52:17 +01:00
# }}}
2020-12-01 18:04:51 +01:00
usage() { # {{{
cat <<- EOF
2020-12-01 18:05:16 +01:00
usage: $PROGNAME [--debug,--help] [--delay INT_DELAY_SECONDS] [INT_DELAY_SECONDS]
2020-12-01 18:04:51 +01:00
Try to get a screen capture with available tool:
1. Flameshot (see: https://github.com/flameshot-org/flameshot)
2. Scrot
EXAMPLES:
- Select the area, on the screen, to capture
${PROGNAME}
- Wait 5 seconds before launching the capture tool
${PROGNAME} 5
${PROGNAME} --delay 5
OPTIONS:
2020-12-01 18:05:16 +01:00
--debug
Enable debug messages.
2020-12-01 18:04:51 +01:00
-d,--delay INT_DELAY_SECONDS
Wait INT_DELAY_SECONDS before launching capture tool
--help
Print this help message.
EOF
}
# }}}
2020-12-01 16:52:17 +01:00
debug_message() { # {{{
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
return 0
}
# }}}
# Manage arguments # {{{
## If there is argument(s)
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option {{{
## Use it to delay screenshot
if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
then
## Use this argument for delay_screenshot (in seconds)
2020-12-01 16:52:17 +01:00
delay_screenshot="${1}"
## Enable DELAYED screenshot mode
DELAYED_MODE="0"
2020-12-01 16:52:17 +01:00
## Switch to the next argument
shift
manage_arg=$((manage_arg+1))
fi
## }}}
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
2020-12-01 18:05:16 +01:00
--debug ) ## Enable debug mode
## Enable DEBUG
DEBUG="0"
;;
2020-12-01 16:52:17 +01:00
-d|--delay ) ## Add a delay
## Enable DELAYED screenshot mode
DELAYED_MODE="0"
2020-12-01 16:52:17 +01:00
## Move to the next argument
shift
## Define the delay with this argument (in seconds)
2020-12-01 16:52:17 +01:00
delay_screenshot="${1}"
;;
2020-12-01 18:04:51 +01:00
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
2020-12-01 16:52:17 +01:00
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
2020-12-01 18:04:51 +01:00
usage
2020-12-01 16:52:17 +01:00
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
## Use Flameshot if available {{{
2020-12-01 16:27:21 +01:00
if [ "$(command -v flameshot)" ]; then
### Take a capture {{{
### --raw: To prevent flameshot from returing immediately
### --path: Default path to store saved image
### --delay: Wait XX milliseconds before launching flameshot
### And immediately send PNG raw to clipboard
if [ "${DELAYED_MODE}" -eq "0" ]; then
2020-12-01 17:20:44 +01:00
debug_message "Flameshot tool \
DELAYED_MODE enabled."
## Convert delay screenshot to milliseconds
delay_screenshot="$(( ${delay_screenshot} * 1000 ))"
## Open flameshot after "${delay_screenshot}" seconds
flameshot gui --raw --path /tmp --delay "${delay_screenshot}" | xclip -selection clipboard -t image/png
else
2020-12-01 17:20:44 +01:00
debug_message "Flameshot tool \
Capture with gui."
## Open flameshot
flameshot gui --raw --path /tmp --delay 50 | xclip -selection clipboard -t image/png
fi
### }}}
### Store the clipboard content to a new image file
xclip -out -selection clipboard -t image/png > /tmp/$(date +%Y%m%d-%H%M%S)."${PROGNAME}".png
### Ensure to kill flameshot once the capture is complete
kill $(pgrep -f -- "/usr/bin/flameshot")
2020-12-01 16:52:17 +01:00
## }}}
else ## Use Scrot {{{
2020-12-01 17:20:44 +01:00
debug_message "Scrot tool \
You might consider installing `flameshot`."
## --delay: Wait NUM seconds before taking a shot.
## --select: Interactively select a window or rectangle with the mouse.
## --multidisp: For multiple heads, grab shot from each and join them together.
## --exec: Directly open the screenshot with Gimp then delete it
if [ "${DELAYED_MODE}" -eq "0" ]; then
debug_message "Scrot tool \
DELAYED_MODE enabled."
## Take a full screenshot because
### mouse couldn't be grab if already used in another app
scrot --delay "${delay_screenshot}" --multidisp --exec 'gimp $f ; mv $f /tmp/'
else
debug_message "Scrot tool \
Capture with select."
scrot --select --multidisp --exec 'gimp $f ; mv $f /tmp/'
fi
2020-12-01 16:27:21 +01:00
fi
2020-12-01 16:52:17 +01:00
## }}}