61 lines
1.5 KiB
Bash
Executable File
61 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -u
|
|
|
|
screenshot_dir="$HOME/Pictures/Screenshots"
|
|
remote_target="mac-attio:~/screenshot.png"
|
|
file_timeout=8 # seconds to wait for screenshot file to appear
|
|
upload_timeout=10 # seconds
|
|
|
|
notify() {
|
|
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" \
|
|
XDG_RUNTIME_DIR="/run/user/$(id -u)" \
|
|
notify-send "$@"
|
|
}
|
|
|
|
# Record existing files
|
|
shopt -s nullglob
|
|
existing_files=("$screenshot_dir"/*.png)
|
|
existing_count=${#existing_files[@]}
|
|
|
|
# Take screenshot (no timeout here so interactive capture isn't canceled)
|
|
niri msg action screenshot >/dev/null 2>&1
|
|
|
|
# Wait for new file (timeout in 0.1s intervals)
|
|
deadline=$((file_timeout * 10))
|
|
count=0
|
|
files=("$screenshot_dir"/*.png)
|
|
|
|
while (( count < deadline )); do
|
|
files=("$screenshot_dir"/*.png)
|
|
if (( ${#files[@]} > existing_count )); then
|
|
break
|
|
fi
|
|
sleep 0.1
|
|
((count++))
|
|
done
|
|
|
|
# Check if a new file appeared
|
|
if (( ${#files[@]} <= existing_count )); then
|
|
exit 0 # Canceled or failed, silent exit
|
|
fi
|
|
|
|
# Get the new file (most recent)
|
|
latest_file=$(ls -1t -- "${files[@]}" | head -n 1)
|
|
|
|
# Small delay to ensure file is fully written
|
|
sleep 0.1
|
|
|
|
# Upload with strict SSH options so it never blocks waiting for prompts
|
|
if timeout "${upload_timeout}s" scp -q \
|
|
-o BatchMode=yes \
|
|
-o ConnectTimeout=5 \
|
|
-o ConnectionAttempts=1 \
|
|
-o ServerAliveInterval=2 \
|
|
-o ServerAliveCountMax=1 \
|
|
-- "$latest_file" "$remote_target"; then
|
|
notify "Screenshot" "Uploaded to Mac"
|
|
else
|
|
notify "Screenshot" "Upload to Mac failed"
|
|
fi
|