nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

screenshot (835B)


      1 #!/bin/bash
      2 
      3 timestamp=$(date +%s)
      4 screenshot_file="$HOME/misc/screenshots/${timestamp}.png"
      5 
      6 take_screenshot() {
      7 	local temp_file="$1"
      8 
      9 	if [ -n "$WAYLAND_DISPLAY" ]; then
     10 		area=$(slurp 2>/dev/null) || return 1
     11 		grim -g "$area" "$temp_file" || return 1
     12 	else
     13 		selection=$(hacksaw -f "-i %i -g %g" 2>/dev/null) || return 1
     14 		shotgun "$selection" "$temp_file" || return 1
     15 	fi
     16 
     17 	return 0
     18 }
     19 
     20 copy_to_clipboard() {
     21 	local file="$1"
     22 
     23 	if [ -n "$WAYLAND_DISPLAY" ]; then
     24 		wl-copy <"$file"
     25 	else
     26 		xclip -t 'image/png' -selection clipboard <"$file"
     27 	fi
     28 }
     29 
     30 if take_screenshot "$screenshot_file"; then
     31 	if [ -f "$screenshot_file" ] && [ -s "$screenshot_file" ]; then
     32 		copy_to_clipboard "$screenshot_file"
     33 	else
     34 		rm -f "$screenshot_file"
     35 		echo "no content captured" >&2
     36 		exit 1
     37 	fi
     38 else
     39 	echo "screenshot cancelled or failed"
     40 	exit 1
     41 fi