Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
impaste: pasting images to piped commands on macOS#
I wanted the ability to paste the image on my clipboard into a command in the macOS terminal.
It turns out pbpaste only works with textual data - so copying a portion of a screenshot to my clipboard (using CleanShot X) and running the following produced a 0 byte file:
1pbpaste > /tmp/screenshot.pngWith some initial clues from Feraidoon Mehri in a GitHub issue followed by some ChatGPT and Claude 3 Opus prompting I got to the following script, saved as ~/.local/bin/impaste on my machine (that folder is on my PATH) and made excutable with chmod 755 ~/.local/bin/impaste:
1#!/bin/zsh2
3# Generate a unique temporary filename4tempfile=$(mktemp -t clipboard.XXXXXXXXXX.png)5
6# Save the clipboard image to the temporary file7osascript -e 'set theImage to the clipboard as «class PNGf»' \8 -e "set theFile to open for access POSIX file \"$tempfile\" with write permission" \9 -e 'write theImage to theFile' \10 -e 'close access theFile'11
12# Output the image data to stdout13cat "$tempfile"14
15# Delete the temporary file16rm "$tempfile"Now I can copy an image to my clipboard and run this:
1impaste > /tmp/image.pngOr pipe impaste into any command that accepts images.