Copy file to clipboard

Mac OSX specific forum
jamirokwai
Addict
Addict
Posts: 802
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Copy file to clipboard

Post 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
Regards,
JamiroKwai
WilliamL
Addict
Addict
Posts: 1259
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Copy file to clipboard

Post 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.
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
jamirokwai
Addict
Addict
Posts: 802
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Copy file to clipboard

Post 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.
Regards,
JamiroKwai
Post Reply