dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

maimpick (1373B)


      1 #!/bin/sh
      2 set -e
      3 
      4 #selection="primary"
      5 selection="clipboard"
      6 
      7 die()
      8 {
      9 	printf '%s: %s\n' "${0##*/}" "$1" >&2
     10 	exit 1
     11 }
     12 
     13 choose_mode()
     14 {
     15 	printf 'area\nwindow\nscreen' | dmenu -i
     16 }
     17 
     18 choose_output_target()
     19 {
     20 	printf 'file\nclipboard' | dmenu -i -p "output:"
     21 }
     22 
     23 name_outfile()
     24 {
     25 	out="$(printf 'tmp/pic-%s-%s.png' "$1" "$(date '+%y-%m-%d_%H:%M:%S')" | \
     26 		dmenu -l 1 -p "outfile:")"
     27 	if [ ! "${out##*.}" = "png" ]; then
     28 		out="${out}.png"
     29 	fi
     30 	printf '%s/%s' "$HOME" "$out"
     31 }
     32 
     33 take_screenshot()
     34 {
     35 	case "$1" in
     36 		area)
     37 			maim --hidecursor -s "$2";;
     38 		window)
     39 			maim --hidecursor -i "$(xdotool getactivewindow)" "$2";;
     40 		screen)
     41 			maim "$2";;
     42 		*)
     43 			die "mode $1 not understood";;
     44 	esac
     45 }
     46 
     47 post_process_output()
     48 {
     49 	out="$1"
     50 	out_jpeg="${out%.png}.jpg"
     51 	format="png"
     52 	convert "${out}" "${out_jpeg}"
     53 	if [ "$(stat -f "%z" "${out_jpeg}")" -lt "$(stat -f "%z" "${out}")" ]; then
     54 		format="jpeg"
     55 		rm "${out}"
     56 		out="${out_jpeg}"
     57 	else
     58 		rm "${out_jpeg}"
     59 	fi
     60 	if [ "$target" = "clipboard" ]; then
     61 		xclip -t "image/${format}" -selection "$selection" "${out}"
     62 		rm "${out}"
     63 	else
     64 		printf '%s' "${out}" | xclip -i -selection "$selection"
     65 		printf '%s\n' "${out}"
     66 	fi
     67 }
     68 
     69 mode="$(choose_mode)"
     70 target="$(choose_output_target)"
     71 if [ "$target" = "file" ]; then
     72 	f="$(name_outfile "$mode")"
     73 else
     74 	f="$(mktemp).png"
     75 fi
     76 take_screenshot "$mode" "$f" 2>/dev/null
     77 post_process_output "$f"