pekwm_screenshot.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/sh
  2. #
  3. # Copyright © 2013 the pekwm development team
  4. #
  5. screenshot_scrot()
  6. {
  7. scrot -z "$1"
  8. }
  9. screenshot_xwd_netpbm()
  10. {
  11. xwd -root | xwdtopnm 2>/dev/null | pnmtopng > "$1"
  12. }
  13. screenshot_imagemagick()
  14. {
  15. import -window root "$1"
  16. }
  17. is_in_path()
  18. {
  19. which $1 >/dev/null 2>&1
  20. return $?
  21. }
  22. detect_command()
  23. {
  24. is_in_path "scrot"
  25. if test $? -eq 0; then
  26. command="scrot"
  27. return
  28. fi
  29. is_in_path "import"
  30. if test $? -eq 0; then
  31. command="magick"
  32. return
  33. fi
  34. is_in_path "xwd" && is_in_path "xwdtopnm" && is_in_path "pnmtopng"
  35. if test $? -eq 0; then
  36. command="netpbm"
  37. return
  38. fi
  39. }
  40. usage()
  41. {
  42. echo "usage: pekwm_screenshot.sh [-c scrot|netpbm|magick] [-d delay] [-o output.png]"
  43. echo ""
  44. echo " -c scrot|netpbm|magick (defaults to autodetect)"
  45. echo " Command to use when creating screenshot."
  46. echo " -d seconds (defaults to 0)"
  47. echo " Number of seconds to wait before taking screenshot"
  48. echo " -h"
  49. echo " Display this information"
  50. echo " -o output.png (defaults to ~/screenshot_YYYYMMDD_HHmmSS.png)"
  51. echo " Name of screenshot output file."
  52. echo ""
  53. exit 0
  54. }
  55. usage_command()
  56. {
  57. if test -z "$command"; then
  58. echo "Unable to find any supported commands for taking screenshots"
  59. else
  60. echo "Unsupported screenshot command $command"
  61. fi
  62. echo ""
  63. echo "Supported screenshot commands are:"
  64. echo ""
  65. echo " * scrot, http://linuxbrit.co.uk/software/"
  66. echo " * imagemagick, http://www.imagemagick.org/"
  67. echo " * netpbm (+ xwd), http://netpbm.sourceforge.net/"
  68. echo ""
  69. exit 1
  70. }
  71. main()
  72. {
  73. # Initialize for strict mode
  74. command=""
  75. delay="0"
  76. output=""
  77. # Parse options
  78. eval set -- "$OPTIONS"
  79. while true; do
  80. case "$1" in
  81. -c)
  82. command="$2"
  83. shift 2;;
  84. -d)
  85. delay=$((0 + $2))
  86. shift 2;;
  87. -h)
  88. usage;;
  89. -o)
  90. output="$2"
  91. shift 2;;
  92. --)
  93. break;;
  94. esac
  95. done
  96. # No command specified, try autodetect
  97. if test -z "$command"; then
  98. detect_command
  99. if test -z "$command"; then
  100. usage_command
  101. fi
  102. fi
  103. # No output specified, format
  104. if test -z "$output"; then
  105. output="$HOME/screenshot_$(date '+%Y%m%d_%H%M%S').png"
  106. fi
  107. # Wait for N seconds if specified
  108. if test "$delay" -ne "0"; then
  109. echo "Taking screenshot in $delay seconds..."
  110. sleep $delay
  111. fi
  112. # Grab screenshot
  113. case "$command" in
  114. scrot)
  115. screenshot_scrot $output
  116. ;;
  117. magick)
  118. screenshot_imagemagick $output
  119. ;;
  120. netpbm)
  121. screenshot_xwd_netpbm $output
  122. ;;
  123. *)
  124. usage_command
  125. ;;
  126. esac
  127. if test $? -eq 0; then
  128. echo "Successfully captured screen to $output"
  129. else
  130. echo "Failed to capture screen to $output!"
  131. fi
  132. exit 0
  133. }
  134. OPTIONS=$(getopt -o c:d:ho: -n 'pekwm_screenshot.sh' -- "$@")
  135. main