192 words
1 minute
impaste: pasting images to piped commands on macOS
Anubhav Gain
2024-02-13

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:

Terminal window
pbpaste > /tmp/screenshot.png

With 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:

#!/bin/zsh
# Generate a unique temporary filename
tempfile=$(mktemp -t clipboard.XXXXXXXXXX.png)
# Save the clipboard image to the temporary file
osascript -e 'set theImage to the clipboard as «class PNGf»' \
-e "set theFile to open for access POSIX file \"$tempfile\" with write permission" \
-e 'write theImage to theFile' \
-e 'close access theFile'
# Output the image data to stdout
cat "$tempfile"
# Delete the temporary file
rm "$tempfile"

Now I can copy an image to my clipboard and run this:

impaste > /tmp/image.png

Or pipe impaste into any command that accepts images.

impaste: pasting images to piped commands on macOS
https://mranv.pages.dev/posts/impaste-pasting-images-to-piped-commands-on-macos/
Author
Anubhav Gain
Published at
2024-02-13
License
CC BY-NC-SA 4.0