Created
August 9, 2026 12:44
-
-
Save eip/1985ac216eb1bac26a57d30425b2e7c6 to your computer and use it in GitHub Desktop.
Copy macOS retina screenshot as 72 dpi low-resolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env zsh | |
| setopt ERR_EXIT NO_UNSET PIPE_FAIL | |
| if (( $# > 1 )); then | |
| print -u2 "Usage: ${0:t} [png-file]" | |
| exit 2 | |
| fi | |
| if (( $# == 1 )); then | |
| if [[ ! -f $1 ]]; then | |
| print -u2 "File not found: $1" | |
| exit 2 | |
| fi | |
| input=$1 | |
| else | |
| screenshotDir=~/Pictures/Screenshots | |
| screenshots=("$screenshotDir"/*.[pP][nN][gG](N.om[1])) | |
| if (( ${#screenshots} == 0 )); then | |
| print -u2 "No PNG screenshots found in $screenshotDir" | |
| exit 1 | |
| fi | |
| input=${screenshots[1]} | |
| # print "Using latest screenshot: $input" | |
| fi | |
| clipboardDelay=${COPY_SCREENSHOT_DELAY:-1} | |
| if [[ $clipboardDelay != <-> && $clipboardDelay != <->.<-> ]]; then | |
| print -u2 "COPY_SCREENSHOT_DELAY must be a non-negative number" | |
| exit 2 | |
| fi | |
| metadata=$( | |
| sips \ | |
| -g dpiWidth \ | |
| -g dpiHeight \ | |
| -g pixelWidth \ | |
| -g pixelHeight \ | |
| -g format \ | |
| "$input" | |
| ) | |
| parsedMetadata=$( | |
| awk ' | |
| /dpiWidth:/ { dpiWidth = int($2) } | |
| /dpiHeight:/ { dpiHeight = int($2) } | |
| /pixelWidth:/ { pixelWidth = $2 } | |
| /pixelHeight:/{ pixelHeight = $2 } | |
| /format:/ { format = $2 } | |
| END { | |
| printf "%s|%s|%s|%s|%s", \ | |
| dpiWidth, dpiHeight, pixelWidth, pixelHeight, format | |
| } | |
| ' <<< "$metadata" | |
| ) | |
| IFS='|' read -r dpiWidth dpiHeight pixelWidth pixelHeight imageFormat \ | |
| <<< "$parsedMetadata" | |
| if [[ $dpiWidth != <-> || $dpiHeight != <-> || | |
| $pixelWidth != <-> || $pixelHeight != <-> ]] || | |
| (( dpiWidth == 0 || dpiHeight == 0 || | |
| pixelWidth == 0 || pixelHeight == 0 )); then | |
| print -u2 "Invalid image metadata: $input" | |
| exit 1 | |
| fi | |
| if [[ $imageFormat != png ]]; then | |
| print -u2 "Only PNG images are supported (found: ${imageFormat:-unknown})" | |
| exit 1 | |
| fi | |
| if (( dpiWidth != dpiHeight )); then | |
| print -u2 "$dpiWidth x $dpiHeight dpi images are not supported" | |
| exit 1 | |
| fi | |
| copy_png() { | |
| osascript \ | |
| -e 'on run argv' \ | |
| -e 'set the clipboard to (read POSIX file (item 1 of argv) as «class PNGf»)' \ | |
| -e 'end run' \ | |
| "$1" | |
| } | |
| copy_width_then_png() { | |
| local width=$1 | |
| local image=$2 | |
| print -n "|width=$width" | pbcopy | |
| sleep "$clipboardDelay" | |
| copy_png "$image" | |
| } | |
| case $dpiWidth in | |
| 72) | |
| documentWidth=$((pixelWidth * 2 / 3)) | |
| print "Copying \"${input:t}\" as-is" | |
| print "Confluence doc width: $documentWidth px" | |
| copy_width_then_png "$documentWidth" "$input" | |
| ;; | |
| 144) | |
| documentWidth=$((pixelWidth / 3)) | |
| maxSize=$((pixelWidth > pixelHeight ? pixelWidth : pixelHeight)) | |
| tempDir=$(mktemp -d "${TMPDIR%/}/copy-screenshot.XXXXXX") | |
| trap 'rm -rf -- "$tempDir"' EXIT | |
| output="$tempDir/image.png" | |
| print "Resizing \"${input:t}\" from $maxSize px to $((maxSize / 2)) px" | |
| print "Confluence doc width: $documentWidth px" | |
| sips \ | |
| -s dpiWidth 72 \ | |
| -s dpiHeight 72 \ | |
| -Z "$((maxSize / 2))" \ | |
| --out "$output" \ | |
| "$input" >/dev/null | |
| copy_width_then_png "$documentWidth" "$output" | |
| ;; | |
| *) | |
| print -u2 "$dpiWidth x $dpiHeight dpi images are not supported" | |
| exit 1 | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment