0

I know I can take image content from the clipboard and pipe into STDOUT like this:

osascript -e "get the clipboard as «class PNGf»" | sed "s/«data PNGf//; s/»//" | xxd -r -p

Is there a way to do the opposite? in other words to put an image into the clipboard via pipes?

2 Answers 2

1

Here is an example of setting the clipboard to an image. Obviously just edit the image path.

osascript -e 'set the clipboard to (read (POSIX file "/Library/Desktop Pictures/Mojave Day.jpg") as JPEG picture)'


In Terminal.app, you could also create a function named copy(or whatever you want to name it) with this following line of code.

copy(){ osascript -e 'on run{a}' -e 'set the clipboard to posix file a' -e end "$(readlink -f "$1")"; }

Then you would have a new command called copy which would take an argument which would be passed to the osascript.

For example, you could set a variable to the result of a command which returns the path of a file you would like to set your clipboard to. Then you could use that variable as the argument to your copy command.

This following command sets the new variable theFile to the results of the find "/Library/Desktop Pictures" -name "Mojave Day*" command.

theFile="$(find "/Library/Desktop Pictures" -name "Mojave Day*")"

OK so now you have created your copy function. You also now have a variable called theFile. Now all you need to do to copy the file to your clipboard as an actual file and not just the path of the file as text, is to use your new copy command using theFile variable as it's argument... like this

copy "$theFile"

Or maybe you don't want to pass a variable to the copy command. You could just as easily use the full POSIX Path of the file instead as the argument to the copy command like this...

copy "/Library/Desktop Pictures/Mojave Day.jpg"

So maybe my examples are not true "Pipes" but they are a way of passing information to the osascript command.

SIDENOTE: This will only work on single files or folders, not multiple files/folders.

1
  • 2
    That would require writing to a temp file first - it would not work with stdin, would it? (i tried using /dev/stdin and it fails)
    – simone
    Commented Jun 8, 2023 at 16:42
0

Use pbcopy(1).

Takes the standard input and places it in the specified pasteboard. If no pasteboard is specified, the general pasteboard will be used by default.

The input is placed in the pasteboard as ASCII data unless it begins with the Encapsulated PostScript (EPS) file header or the Rich Text Format (RTF) file header, in which case it is placed in the pasteboard as one of those data types.

3
  • thanks, but that does not work with images intio stdin AFAIK. Did you try and succeed?
    – simone
    Commented Jun 8, 2023 at 16:43
  • EPS are images. It doesn't say it supports other image formats. Commented Jun 8, 2023 at 17:58
  • pbcopy only supports 4 formats: general (default), ruler, find, and font. The only way to get an image would be to encode it (base64) put it in the clipboard as general text, then paste it to something that will unencode it.
    – Allan
    Commented Jun 8, 2023 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .