i3wm-screenshot-tool/i3-screenshot-multimonitor.sh

129 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# i3-screenshot-multimonitor.sh - Tool for taking screenshots with multi-monitor support
# This script provides options for selecting a specific monitor or area
# Default save location - user's Pictures directory
SAVE_DIR="$HOME/Pictures/Screenshots"
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
SCREENSHOT_PATH="$SAVE_DIR/screenshot_$TIMESTAMP.png"
# Create the save directory if it doesn't exist
mkdir -p "$SAVE_DIR"
# Function to display notification
notify() {
notify-send "Screenshot Tool" "$1" -i camera-photo
}
# Function to get a list of monitors
get_monitors() {
xrandr --listactivemonitors | grep -v "Monitors" | awk '{print $4}'
}
# Function to get monitor geometry
get_monitor_geometry() {
local monitor=$1
xrandr | grep "$monitor" | grep -oP '\d+x\d+\+\d+\+\d+'
}
# Function to take a screenshot with a delay
take_delayed_screenshot() {
# Add a small delay to ensure any menus are closed
sleep 0.5
# Take the screenshot
if [ -n "$1" ]; then
maim -g "$1" "$SCREENSHOT_PATH"
else
maim "$SCREENSHOT_PATH"
fi
# Check if screenshot was successful
if [ $? -ne 0 ]; then
notify "Screenshot failed"
exit 1
fi
}
# Check if rofi is installed
if ! command -v rofi &> /dev/null; then
notify "Rofi is not installed. Using direct selection mode."
# Use slop to select an area and maim to take the screenshot
notify "Select an area to capture"
SELECTION=$(slop -f "%x,%y,%w,%h")
if [ $? -ne 0 ]; then
notify "Screenshot canceled"
exit 1
fi
# Extract coordinates and dimensions
IFS=',' read -r X Y W H <<< "$SELECTION"
# Take the screenshot of the selected area
take_delayed_screenshot "${W}x${H}+${X}+${Y}"
else
# Get list of monitors
MONITORS=$(get_monitors)
# Add "Select Area" and "Full Screenshot" options
OPTIONS="Select Area\nFull Screenshot"
# Add each monitor to the options
for MONITOR in $MONITORS; do
OPTIONS="$OPTIONS\n$MONITOR"
done
# Show selection menu with rofi
CHOICE=$(echo -e "$OPTIONS" | rofi -dmenu -i -p "Screenshot Mode")
case "$CHOICE" in
"Select Area")
# Use slop to select an area
notify "Select an area to capture"
SELECTION=$(slop -f "%x,%y,%w,%h")
if [ $? -ne 0 ]; then
notify "Screenshot canceled"
exit 1
fi
# Extract coordinates and dimensions
IFS=',' read -r X Y W H <<< "$SELECTION"
# Take the screenshot of the selected area
take_delayed_screenshot "${W}x${H}+${X}+${Y}"
;;
"Full Screenshot")
# Take screenshot of all monitors
notify "Taking full screenshot..."
take_delayed_screenshot
;;
*)
# Take screenshot of selected monitor
if [ -n "$CHOICE" ]; then
GEOMETRY=$(get_monitor_geometry "$CHOICE")
if [ -n "$GEOMETRY" ]; then
notify "Taking screenshot of monitor $CHOICE..."
take_delayed_screenshot "$GEOMETRY"
else
notify "Could not determine monitor geometry"
exit 1
fi
else
notify "Screenshot canceled"
exit 1
fi
;;
esac
fi
# Copy to clipboard
xclip -selection clipboard -t image/png -i "$SCREENSHOT_PATH"
# Notify user
notify "Screenshot saved to $SCREENSHOT_PATH and copied to clipboard"
echo "Screenshot saved to: $SCREENSHOT_PATH"