summaryrefslogtreecommitdiff
path: root/ar/.local/bin/maimpick
blob: ea227ae4a4be8d83530ebf763ee201602ba657a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/sh

# This is bound to Shift+PrintScreen by default, requires maim. It lets you
# choose the kind of screenshot to take, including copying the image or even
# highlighting an area to copy. scrotcucks on suicidewatch right now.

# variables
output_dir="${XDG_PICTURES_DIR:-${HOME}/Pictures}/screenshots/"
output="$(date '+%y%m%d-%H%M-%S').png"
xclip_cmd="xclip -sel clip -t image/png"
ocr_cmd="xclip -sel clip"

[ -d "$output_dir" ] || mkdir -p "$output_dir"

# Number of connected monitors (first line of xrandr --listmonitors).
mon_count="$(xrandr --listmonitors | awk 'NR==1{print $2}')"

# Prompt for a monitor and print its maim -g geometry (WxH+X+Y), or nothing if
# the picker is cancelled.
pick_monitor() {
	monitors="$(xrandr --listmonitors | tail -n +2)"
	name="$(printf '%s\n' "$monitors" | awk '{print $NF}' | dmenu -i -p "Which monitor?")" || return 1
	[ -z "$name" ] && return 1
	printf '%s\n' "$monitors" | awk -v n="$name" '$NF==n {gsub(/\/[0-9]+/,"",$3); print $3}'
}

# Build the menu, only offering the monitor entries when more than one is
# connected.
options="a selected area
current window
full screen"
[ "$mon_count" -gt 1 ] 2>/dev/null && options="$options
selected monitor"
options="$options
a selected area (copy)
current window (copy)
full screen (copy)"
[ "$mon_count" -gt 1 ] 2>/dev/null && options="$options
selected monitor (copy)"
options="$options
copy selected image to text"

lines="$(printf '%s\n' "$options" | wc -l)"

case "$(printf '%s\n' "$options" | dmenu -l "$lines" -i -p "Screenshot which area?")" in
"a selected area") maim -u -s "$output_dir"pic-selected-"${output}" ;;
"current window") maim -B -q -d 0.2 -i "$(xdotool getactivewindow)" "$output_dir"pic-window-"${output}" ;;
"full screen") maim -q -d 0.2 "$output_dir"pic-full-"${output}" ;;
"selected monitor") geo="$(pick_monitor)" && [ -n "$geo" ] && maim -q -d 0.2 -g "$geo" "$output_dir"pic-monitor-"${output}" ;;
"a selected area (copy)") maim -u -s | ${xclip_cmd} ;;
"current window (copy)") maim -q -d 0.2 -i "$(xdotool getactivewindow)" | ${xclip_cmd} ;;
"full screen (copy)") maim -q -d 0.2 | ${xclip_cmd} ;;
"selected monitor (copy)") geo="$(pick_monitor)" && [ -n "$geo" ] && maim -q -d 0.2 -g "$geo" | ${xclip_cmd} ;;
"copy selected image to text") tmpfile=$(mktemp /tmp/ocr-XXXXXX.png) && maim -u -s >"$tmpfile" && tesseract "$tmpfile" - -l eng | ${ocr_cmd} && rm "$tmpfile" ;;
esac