Some useful (or not) scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.9 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. #!/bin/sh
  2. ## Try to use flameshot to get a screenshot
  3. ### Select area work with both NORMAL and DELAYED_MODE.
  4. ### See https://github.com/flameshot-org/flameshot
  5. ## Or try to use scrot
  6. ### Select area work only in normal mode.
  7. ### In DELAYED_MODE, a capture of all screens is made.
  8. # Vars {{{
  9. readonly PROGNAME=$(basename "${0}")
  10. readonly NBARGS="${#}"
  11. ## Test if DEBUG is already defined (by parent script,…)
  12. [ -z "${DEBUG}" ] && DEBUG=1
  13. ## Colors
  14. readonly RED='\033[0;31m'
  15. readonly RESET='\033[0m'
  16. readonly COLOR_DEBUG="${PURPLE}"
  17. DELAYED_MODE="1"
  18. # }}}
  19. usage() { # {{{
  20. cat <<- EOF
  21. usage: $PROGNAME [--debug,--help] [--delay INT_DELAY_SECONDS] [INT_DELAY_SECONDS]
  22. Try to get a screen capture with available tool :
  23. 1. Flameshot (see : https://github.com/flameshot-org/flameshot)
  24. 2. Scrot
  25. EXAMPLES :
  26. - Select the area, on the screen, to capture
  27. ${PROGNAME}
  28. - Wait 5 seconds before launching the capture tool
  29. ${PROGNAME} 5
  30. ${PROGNAME} --delay 5
  31. OPTIONS :
  32. --debug
  33. Enable debug messages.
  34. -d,--delay INT_DELAY_SECONDS
  35. Wait INT_DELAY_SECONDS before launching capture tool
  36. --help
  37. Print this help message.
  38. EOF
  39. }
  40. # }}}
  41. debug_message() { # {{{
  42. local_debug_message="${1}"
  43. ## Print message if DEBUG is enable (=0)
  44. [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_debug_message}"
  45. return 0
  46. }
  47. # }}}
  48. # Manage arguments # {{{
  49. ## If there is argument(s)
  50. if [ ! "${NBARGS}" -eq "0" ]; then
  51. manage_arg="0"
  52. ## If the first argument is not an option {{{
  53. ## Use it to delay screenshot
  54. if ! printf -- '%s' "${1}" | grep -q -E -- "^-+";
  55. then
  56. ## Use this argument for delay_screenshot (in seconds)
  57. delay_screenshot="${1}"
  58. ## Enable DELAYED screenshot mode
  59. DELAYED_MODE="0"
  60. ## Switch to the next argument
  61. shift
  62. manage_arg=$((manage_arg+1))
  63. fi
  64. ## }}}
  65. # Parse all options (start with a "-") one by one
  66. while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
  67. case "${1}" in
  68. --debug ) ## Enable debug mode
  69. ## Enable DEBUG
  70. DEBUG="0"
  71. ;;
  72. -d|--delay ) ## Add a delay
  73. ## Enable DELAYED screenshot mode
  74. DELAYED_MODE="0"
  75. ## Move to the next argument
  76. shift
  77. ## Define the delay with this argument (in seconds)
  78. delay_screenshot="${1}"
  79. ;;
  80. -h|--help ) ## help
  81. usage
  82. ## Exit after help informations
  83. exit 0
  84. ;;
  85. * ) ## unknow option
  86. printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
  87. printf '%b\n' "---"
  88. usage
  89. exit 1
  90. ;;
  91. esac
  92. debug_message "Arguments management − \
  93. ${RED}${1}${COLOR_DEBUG} option managed."
  94. ## Move to the next argument
  95. shift
  96. manage_arg=$((manage_arg+1))
  97. done
  98. debug_message "Arguments management − \
  99. ${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
  100. else
  101. debug_message "Arguments management − \
  102. No arguments/options to manage."
  103. fi
  104. # }}}
  105. ## Use Flameshot if available {{{
  106. if [ "$(command -v flameshot)" ]; then
  107. ### Take a capture {{{
  108. ### --raw : To prevent flameshot from returing immediately
  109. ### --path : Default path to store saved image
  110. ### --delay : Wait XX milliseconds before launching flameshot
  111. ### And immediately send PNG raw to clipboard
  112. if [ "${DELAYED_MODE}" -eq "0" ]; then
  113. debug_message "Flameshot tool − \
  114. DELAYED_MODE enabled."
  115. ## Convert delay screenshot to milliseconds
  116. delay_screenshot="$(( ${delay_screenshot} * 1000 ))"
  117. ## Open flameshot after "${delay_screenshot}" seconds
  118. flameshot gui --raw --path /tmp --delay "${delay_screenshot}" | xclip -selection clipboard -t image/png
  119. else
  120. debug_message "Flameshot tool − \
  121. Capture with gui."
  122. ## Open flameshot
  123. flameshot gui --raw --path /tmp --delay 50 | xclip -selection clipboard -t image/png
  124. fi
  125. ### }}}
  126. ### Store the clipboard content to a new image file
  127. xclip -out -selection clipboard -t image/png > /tmp/$(date +%Y%m%d-%H%M%S)."${PROGNAME}".png
  128. ### Ensure to kill flameshot once the capture is complete
  129. kill $(pgrep -f -- "/usr/bin/flameshot")
  130. ## }}}
  131. else ## Use Scrot {{{
  132. debug_message "Scrot tool − \
  133. You might consider installing `flameshot`."
  134. ## --delay : Wait NUM seconds before taking a shot.
  135. ## --select : Interactively select a window or rectangle with the mouse.
  136. ## --multidisp : For multiple heads, grab shot from each and join them together.
  137. ## --exec : Directly open the screenshot with Gimp then delete it
  138. if [ "${DELAYED_MODE}" -eq "0" ]; then
  139. debug_message "Scrot tool − \
  140. DELAYED_MODE enabled."
  141. ## Take a full screenshot because
  142. ### mouse couldn't be grab if already used in another app
  143. scrot --delay "${delay_screenshot}" --multidisp --exec 'gimp $f ; mv $f /tmp/'
  144. else
  145. debug_message "Scrot tool − \
  146. Capture with select."
  147. scrot --select --multidisp --exec 'gimp $f ; mv $f /tmp/'
  148. fi
  149. fi
  150. ## }}}