#!/bin/sh set -eu viewer= notify() { if [ -x /usr/bin/notify-send ]; then /usr/bin/notify-send "SimpleImageViewer" "$1" || true fi } types=$(/usr/bin/wl-paste --list-types 2>/dev/null || true) text_type=$( printf '%s\n' "$types" | /usr/bin/awk ' /^text\/plain/ || $0 == "text/uri-list" || $0 == "UTF8_STRING" || $0 == "STRING" { print exit } ' ) path= if [ -n "$text_type" ]; then path=$( /usr/bin/wl-paste --no-newline --type "$text_type" 2>/dev/null | /usr/bin/awk ' { sub(/^[[:space:]]+/, "") sub(/[[:space:]]+$/, "") if ($0 == "" || $0 == "copy" || $0 == "cut" || substr($0, 1, 1) == "#") next sub(/^file:\/\//, "") gsub(/%20/, " ") sub(/^~/, ENVIRON["HOME"]) print exit } ' ) fi if [ -n "$path" ] && [ -f "$path" ]; then exec "$viewer" "$path" fi image_type=$( printf '%s\n' "$types" | /usr/bin/awk ' $0 == "image/png" { found = $0; print; exit } $0 == "image/jpeg" || $0 == "image/jpg" { found = $0; print; exit } $0 == "image/bmp" || $0 == "image/x-bmp" { found = $0; print; exit } $0 == "image/gif" { found = $0; print; exit } $0 == "image/x-tga" || $0 == "image/tga" { found = $0; print; exit } $0 == "image/x-portable-pixmap" || $0 == "image/x-portable-anymap" { found = $0; print; exit } /^image\// && fallback == "" { fallback = $0 } END { if (found == "" && fallback != "") print fallback } ' ) if [ -z "$image_type" ]; then notify "Clipboard contains neither an image path nor image data" exit 1 fi case "$image_type" in image/png) ext=png ;; image/jpeg | image/jpg) ext=jpg ;; image/bmp | image/x-bmp) ext=bmp ;; image/gif) ext=gif ;; image/x-tga | image/tga) ext=tga ;; image/x-portable-pixmap) ext=ppm ;; image/x-portable-anymap) ext=pnm ;; *) ext=img ;; esac tmpdir=$(/usr/bin/mktemp -d "${TMPDIR:-/tmp}/simple-image-viewer.XXXXXXXXXX") cleanup() { /usr/bin/rm -rf "$tmpdir" } trap cleanup EXIT trap 'exit 130' HUP INT TERM image_path=$tmpdir/clipboard.$ext if ! /usr/bin/wl-paste --type "$image_type" >"$image_path"; then notify "Could not read image data from clipboard" exit 1 fi if [ ! -s "$image_path" ]; then notify "Clipboard image data is empty" exit 1 fi "$viewer" "$image_path"