Page 1 of 1

Copy file to clipboard

Posted: Sun Jun 10, 2012 1:37 pm
by jamirokwai
Hi board,

I found out how to copy (at least) JPGs to the clipboard on Mac OS X. This should work for every type of file...
The SetClipboardImage()-command will copy the image to Clipboard, but will use some kind of TIF-Format, which e.g. Preview.app cannot use.
So I used the AppleScript-Wrapper by wilbert from http://www.purebasic.fr/english/viewtop ... 19&t=40684 to accomplish this.

Code: Select all

script$ = "tell application " + Chr(34) + "Finder" + Chr(34) + " to set the clipboard to (POSIX file " + Chr(34) + "/tmp/file.jpg" + Chr(34) + ")"
COCOA_AppleScript(script$)
You have to copy your image to /tmp/file.jpg before calling the script. You can remove /tmp/file.jpg safely after copying.
It should work with every file type, e.g. MP3, WAV, PNG and such.

Edit: William asked, what is wrong with the PureBasic-way. This: If I set some image (format does not matter) to the Clipboard using SetClipboardimage, the resulting TIF can be viewed using "Show Clipboard" in Finder-Menu (if it is called this way). But neither Preview.app nor Finder can paste the Clipboard to a new image or image-file. Using the above way, you copy an object to Clipboard, with can be used with Preview.app

Re: Copy file to clipboard

Posted: Sun Jun 10, 2012 8:52 pm
by WilliamL

Code: Select all

If CreateImage(0, 26, 20)
    StartDrawing(ImageOutput(0))
      Box(0, 0, 26, 20, RGB(255, 255, 255))
      Circle(13, 10, 5, RGB(255, 0, 0))
    StopDrawing()
    SetClipboardImage(0)
  EndIf
This example from the Help folder works ok for me. I was able to paste the clipboard into Preview and into another drawing program. I'm thinking the problem might be with systems newer than Snow Leopard.

Maybe someone else can try it and say if it is working for them.

Re: Copy file to clipboard

Posted: Sun Jun 10, 2012 10:18 pm
by jamirokwai
WilliamL wrote:This example from the Help folder works ok for me. I was able to paste the clipboard into Preview and into another drawing program. I'm thinking the problem might be with systems newer than Snow Leopard.
Didn't think of this before. On Lion, the example will definitely NOT work.