Need to copy a file/image to clipboard
Need to copy a file/image to clipboard
Hi,
Since DragFiles() is not working in macOS, I tried to copy the file to the clipboard.
But this is also not available in PB.
With DragImage() it is not possible to insert the image in the filesystem with finder.
I found this:
https://www.alecjacobson.com/weblog/?p=3816
But I'm not able to do it in PB.
Goal:
Copy a file (an image) into the clipboard, so that it is possible to insert it into the finder directory.
Any help is appreciated.
Since DragFiles() is not working in macOS, I tried to copy the file to the clipboard.
But this is also not available in PB.
With DragImage() it is not possible to insert the image in the filesystem with finder.
I found this:
https://www.alecjacobson.com/weblog/?p=3816
But I'm not able to do it in PB.
Goal:
Copy a file (an image) into the clipboard, so that it is possible to insert it into the finder directory.
Any help is appreciated.
Re: Need to copy a file/image to clipboard
To copy an image into the clipboard and read it back from the clipboard is possible even on MacOS without any API (successfully tested on MacOS 11.7.6 'Big Sur' with PB 6.01):infratec wrote: Mon Apr 24, 2023 5:05 pm Goal:
Copy a file (an image) into the clipboard, so that it is possible to insert it into the finder directory.
Code: Select all
EnableExplicit
#TestImage = #PB_Compiler_Home + "examples/sources/Data/Geebee2.bmp"
If LoadImage(0, #TestImage) = 0
MessageRequester("Error",
"Failed to load image " + GetFilePart(#TestImage),
#PB_MessageRequester_Error)
Else
OpenWindow(0, 270, 100, ImageWidth(0) * 2 + 25, ImageHeight(0) + 60,
"Copy image into clipboard")
ImageGadget(0, 10, 10, ImageWidth(0), ImageHeight(0), ImageID(0))
ImageGadget(1, ImageWidth(0) + 15, 8, ImageWidth(0) + 2,
ImageHeight(0) + 4, 0, #PB_Image_Border)
ButtonGadget(2, 10, ImageHeight(0) + 20, WindowWidth(0) - 20, 30,
"Copy image to clipboard and display it")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
If EventGadget() = 2
SetClipboardImage(0)
GetClipboardImage(1)
SetGadgetState(1, ImageID(1))
EndIf
EndSelect
ForEver
EndIf
Re: Need to copy a file/image to clipboard
But I think Infratec needs a way to paste the file on the FileManager (or Finder on Mac).Shardik wrote: Mon Apr 24, 2023 6:48 pm To copy an image into the clipboard and read it back from the clipboard is possible even on MacOS without any API (successfully tested on MacOS 11.7.6 'Big Sur' with PB 6.01):
Re: Need to copy a file/image to clipboard
Maybe I'm wrong but that sentence seems what I mean:
infratec wrote: Mon Apr 24, 2023 5:05 pm ...so that it is possible to insert it into the finder directory.
Re: Need to copy a file/image to clipboard
Drag 'n drop of an image is also working on MacOS without any API (successfully tested on MacOS 11.7.6 'Big Sur' with PB 6.01):
The next step is to find out how to drag an image from PureBasic into the Finder. The opposite way, namely how to drag files from the finder into a PureBasic window and display the filenames in an EditorGadget, I had already demonstrated here.
Code: Select all
EnableExplicit
#TestImage = #PB_Compiler_Home + "examples/sources/Data/PureBasicLogo.bmp"
If LoadImage(0, #TestImage) = 0
MessageRequester("Error",
"Failed to load image " + GetFilePart(#TestImage),
#PB_MessageRequester_Error)
Else
OpenWindow(0, 270, 200, 400, 230, "Drag & Drop", #PB_Window_SystemMenu)
TextGadget(0, 10, 10, 380, 20, "Drag this image", #PB_Text_Center)
ImageGadget(1, 10, 30, 380, 70, ImageID(0), #PB_Image_Border)
TextGadget(2, 10, 120, 380, 25, "and drop it below:", #PB_Text_Center)
ImageGadget(3, 10, 140, 380, 70, 0, #PB_Image_Border)
EnableGadgetDrop(3, #PB_Drop_Image, #PB_Drag_Copy)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
If EventGadget() = 1 And EventType() = #PB_EventType_DragStart
DragImage(ImageID(0))
EndIf
Case #PB_Event_GadgetDrop
SetGadgetState(3, ImageID(0))
EndSelect
ForEver
EndIf
The next step is to find out how to drag an image from PureBasic into the Finder. The opposite way, namely how to drag files from the finder into a PureBasic window and display the filenames in an EditorGadget, I had already demonstrated here.
Re: Need to copy a file/image to clipboard
I think that would be the second step. First infratec wants to copy an image into the clipboard. Then he wants to copy the image from the clipboard into a finder directory.
Re: Need to copy a file/image to clipboard
I have been looking for the solution for a long time.
Many Objective-C examples are not up to date, because many object methods are removed from NSPasteboard.
Update v1.01.4
- Link: Tips & Tricks - SetClipboradFiles and GetClipboardFiles
Many Objective-C examples are not up to date, because many object methods are removed from NSPasteboard.
Update v1.01.4
- Link: Tips & Tricks - SetClipboradFiles and GetClipboardFiles
Code: Select all
; Moved - See link
Last edited by mk-soft on Tue Apr 25, 2023 2:40 pm, edited 4 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Need to copy a file/image to clipboard
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Need to copy a file/image to clipboard
@mk-soft
yes and ... no
The file is copied into the clipboard and I'm able to insert it via the finder.
Unfortunately I can not show the image in 'preview'. The file informations are ok.
Ups ...
It looks like a bug in PB:
I can see the picture a few milliseconds, then I see only the grey background of 'preview'.
Or is it my fault? (iMac, Ventura 13.3.1, PB 6.01 asm and C)
Btw. I can open the image with gimp.
Is it a bug in macOS preview?
And ... I can see the content minimized as icon of the file.
Very strange.
But a normal user clicks on the file and it is opened in 'Preview' which doesn't show it.
Ok, it opens on a M1, also finder shows the picture.
Maybe it's my 'special' old iMac.
yes and ... no
The file is copied into the clipboard and I'm able to insert it via the finder.
Unfortunately I can not show the image in 'preview'. The file informations are ok.
Ups ...
It looks like a bug in PB:
Code: Select all
UsePNGImageEncoder()
If CreateImage(0, 255, 255)
StartDrawing(ImageOutput(0))
For k = 0 To 255
FrontColor(RGB(k, 0, k)) ; a rainbow, from black to pink
Line(0, k, 255, 1)
Next
DrawingMode(#PB_2DDrawing_Transparent)
FrontColor(RGB(255, 255, 255)) ; print the text to white !
DrawText(40, 50, "An image easily created !")
StopDrawing() ; This is absolutely needed when the drawing operations are finished !!! Never forget it !
Filename$ = GetTemporaryDirectory() + "PB_macOS_test.png"
Debug Filename$
SaveImage(0, Filename$, #PB_ImagePlugin_PNG);, 0, 24)
RunProgram("open", Filename$, "")
If MessageRequester("Should I", "delete the file" + #LF$ + Filename$ + #LF$ + "?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
DeleteFile(Filename$)
EndIf
EndIf
Or is it my fault? (iMac, Ventura 13.3.1, PB 6.01 asm and C)
Btw. I can open the image with gimp.
Is it a bug in macOS preview?
And ... I can see the content minimized as icon of the file.
Very strange.
But a normal user clicks on the file and it is opened in 'Preview' which doesn't show it.
Ok, it opens on a M1, also finder shows the picture.
Maybe it's my 'special' old iMac.
Re: Need to copy a file/image to clipboard
I don't know if you need this one.
So in the clipboard as a file and as an image
So in the clipboard as a file and as an image
Code: Select all
;-TOP by mk-soft
Procedure SetClipboardFileEx(FileName.s, withImage = #False)
Protected r1, pool, fileURL, objectsToCopy, pasteboard, id
pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
; Add file
objectsToCopy = CocoaMessage(0, 0, "NSMutableArray arrayWithCapacity:", 2)
fileURL = CocoaMessage(0, 0, "NSURL fileURLWithPath:$", @FileName)
CocoaMessage(0, objectsToCopy, "addObject:", fileURL)
; Add Image
If withImage
id = LoadImage(#PB_Any, FileName)
If id
CocoaMessage(0, objectsToCopy, "addObject:", ImageID(id))
EndIf
EndIf
pasteboard = CocoaMessage(0, 0, "NSPasteboard generalPasteboard")
CocoaMessage(0, pasteboard, "clearContents")
r1 = CocoaMessage(0, pasteboard, "writeObjects:", objectsToCopy)
If id
FreeImage(id)
EndIf
CocoaMessage(0, pool, "release")
ProcedureReturn r1
EndProcedure
UseJPEGImageDecoder()
filename.s = OpenFileRequester("", "", "", 0)
If filename
r1 = SetClipboardFileEx(filename, 1)
If r1
Debug "Ok"
Else
Debug "False"
EndIf
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive