rofi/scripts/rofi-screenshot 2.2 K raw
1
#!/bin/sh
2
# Requires: rofi, maim, xclip, libnotify
3
# Screenshot menu. Every shot lands in the clipboard as image/png and, unless
4
# it was a clipboard-only pick, on disk as well — the clipboard copy is what
5
# gets pasted into a chat, the file is what survives the next copy.
6
# Mod+S still takes a plain region shot without going through this menu.
7
set -u
8
9
DIR=${ROFI_SCREENSHOT_DIR:-$HOME/Pictures/screenshots}
10
NOTIFY_ID=7304
11
12
mkdir -p "$DIR"
13
14
notify() {
15
    if command -v dunstify >/dev/null 2>&1; then
16
        dunstify -r "$NOTIFY_ID" ${2:+-i "$2"} "Screenshot" "$1"
17
    else
18
        notify-send "Screenshot" "$1"
19
    fi
20
}
21
22
# -u hides the cursor from the capture; without it the pointer shows up in
23
# the middle of whatever was being selected.
24
shoot() {
25
    file="$DIR/$(date +%Y%m%d-%H%M%S).png"
26
27
    # maim writes nothing and exits non-zero when a selection is cancelled
28
    # with Escape, so a cancelled shot must not leave an empty file behind.
29
    if ! maim -u "$@" "$file" 2>/dev/null || [ ! -s "$file" ]; then
30
        rm -f "$file"
31
        exit 0
32
    fi
33
34
    xclip -selection clipboard -t image/png -i "$file"
35
36
    if [ "$KEEP" -eq 0 ]; then
37
        rm -f "$file"
38
        notify "Copied to clipboard"
39
    else
40
        notify "Copied — saved to ${file#"$HOME"/}" "$file"
41
    fi
42
}
43
44
CHOICE=$(printf '%s\n' \
45
    "󰩭  region → clipboard + file" \
46
    "󰩭  region → clipboard only" \
47
    "󰖯  window → clipboard + file" \
48
    "󰍹  full screen → clipboard + file" \
49
    "󰔛  full screen after 5s" \
50
    "󰉖  open screenshots folder" |
51
    rofi -dmenu -i -no-custom -l 6 -p "screenshot" -mesg "Saving to ${DIR#"$HOME"/}") || exit 0
52
[ -n "$CHOICE" ] || exit 0
53
54
KEEP=1
55
case "$CHOICE" in
56
    *"region → clipboard only")  KEEP=0; shoot -s ;;
57
    *"region"*)                  shoot -s ;;
58
    # A huge -t makes maim wait for a click instead of a drag, which selects
59
    # the clicked window whole — the substitute for `maim -i $(xdotool
60
    # getactivewindow)`, since xdotool is not installed.
61
    *"window"*)                  shoot -st 9999999 ;;
62
    *"after 5s")                 notify "Shooting in 5s..."; sleep 5; shoot ;;
63
    *"full screen"*)             shoot ;;
64
    *"folder")                   exec xdg-open "$DIR" ;;
65
esac