Skip to content

Commit

Permalink
impaste: pasting images to piped commands on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw authored Apr 4, 2024
1 parent da5611b commit 849297e
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions macos/impaste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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](https://cleanshot.com/)) and running the following produced a 0 byte file:
```bash
pbpaste > /tmp/screenshot.png
```
With some initial clues from [Feraidoon Mehri in a GitHub issue](https://github.com/simonw/llm/issues/331#issuecomment-2038425242) followed by some ChatGPT and [Claude 3 Opus prompting](https://gist.github.com/simonw/736bcc9bcfaef40a55deaa959fd57ca8) 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`:

```zsh
#!/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.

0 comments on commit 849297e

Please sign in to comment.