loop through clipboard content ?

  1. ‹ Older
  2. 2 weeks ago

    Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro GraffitiSuite Developer

    I'm not saying you're wrong, just that I'm not wrong, either. ;)

  3. brian f

    Aug 6 Pre-Release Testers, Xojo Pro Chilly California

    @Anthony C I'm not saying you're wrong, just that I'm not wrong, either. ;)

    2 not wrong's don't make a right :P

    (Sorry morning humor) :D

  4. Emile S

    Aug 6 Europe (France, Strasbourg)
    Edited 2 weeks ago

    No Brian, standard humor ;)

    Thanks for it !

  5. Ivan T

    Aug 6 Pre-Release Testers

    @Emile S When I do not know how to ask, I ask what I think I have to do. In this case the question is wong and have this answer (to be refined with the kind of data you want to get back and it depends n the Platform).

    I once get a totally different answer (regarding what I asked), and that was the correct answer, my question was not right (plain wrong, but the answer guy understoud what I wanted)..

    lol

    @Jean-YvesPochez how do you loop through all the items in the clipboard to choose the one you need ( to paste)

    The clipboard class in xojo is too basic to do something like that. You have to use Declares:

    https://developer.apple.com/documentation/appkit/nspasteboard

    https://docs.microsoft.com/en-us/windows/win32/dataxchg/clipboard

  6. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro GraffitiSuite Developer
    Edited 2 weeks ago

    To summarize, there is no way to loop through all of the RawData stored in the clipboard with basic Xojo support. You need to get a list of the identifiers and check them using RawDataAvailable(Identifier), preferably in the order in which you would like to use them, until you find a suitable match. Then you'll use RawData(Identifier) to retrieve the content for that identifier. Note that these identifiers will be different by platform. Just as an example:

    On Windows, HTML is under the identifier "text/html"
    On macOS, HTML is under the identifier "public.html"

    @Jean-YvesPochez also clipboard.pictureavailable seems to returns false if the image type (jpg,png, or tiff) is not in the first position ?

    I don't think I've ever seen this issue. I'd open a Feedback Report.

  7. Emile S

    Aug 6 Europe (France, Strasbourg)

    Picture:
    Same answer: the developer (Xojo developer) ask the OS the kind of object (Text or Picture) he want and the OS returns it if it exists in the Clipboard.

  8. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro GraffitiSuite Developer
    Edited 2 weeks ago

    @Emile S Picture:
    Same answer: the developer (Xojo developer) ask the OS the kind of object (Text or Picture) he want and the OS returns it if it exists in the Clipboard.

    I do believe they're saying that there's a picture object in the clipboard, but PictureAvailable is reporting false if something else is stored in the clipboard under a different identifier as well. This would be a bug.

  9. Emile S

    Aug 6 Europe (France, Strasbourg)

    @ Anthony:

    You can add in the CLipboard a Text, a Picture, A Sound, (etc.) all at once. When you are not in charge of the development: say you use a text editor, the Text contents of the Clipboard will bepasted in the window.
    Same apply for the Picture, Sound, etc.

    There is no “I return the first object I have in the Clipboard” here.

    If your application is in charge of the Clipboard and can deal with all of the above and there is all of the above: I do not knowwhat will be pasted, but I never get that in real life.

    Now,
    a after testing, if you can prove what you wrote, of course you can write a bug report,

    b this is my experience based on more than 40 years of computing use. But I can be wrong, as some already understand using Cataline (as recent Conversations here shows).

    c I only want to help, nothing else.

  10. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro GraffitiSuite Developer

    I'm going off of his statement that having multiple types of data stored, one of them being a picture, PictureAvailable returns False. I'm not sure what you're driving at, but I'm just going to see myself out. Have fun.

  11. Jean-Yves P

    Aug 6 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    to be clear.
    I have a clipboard with "com.company.whatever", then "public.tiff", then "Next TIFF v4"
    I want the tiff
    if I ask c.pictureavailable I get false
    if I ask rawdataavailable("public.tiff") I get false ...
    so I was hoping to be able to loop through the content of the clipboard.

    or is it a bug ?

  12. Sascha S

    Aug 6 Pre-Release Testers, Xojo Pro Germany, Lower Saxonary

    @Emile S You can add in the CLipboard a Text, a Picture, A Sound, (etc.) all at once.

    AFAIK, not really. But you can f.e. "store" RTF Text in the Clipboard and some OS will offer to "retrieve" it as RTF and as Plain Text f.e. There may be more cases which behave like this. But you App can only "store" 1 kind of object at once and multiple in multiple "store" tasks.

    If the OS supports it or if an App is tied to the System Clipboard and stores each new item in a "cache", the OS or the App can offer more then just the latests stored item.

  13. Jean-Yves P

    Aug 6 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    @Anthony C I do believe they're saying that there's a picture object in the clipboard, but PictureAvailable is reporting false if something else is stored in the clipboard under a different identifier as well. This would be a bug.

    this is precisely the problem I have

  14. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro GraffitiSuite Developer
    Edited 2 weeks ago

    @Jean-YvesPochez this is precisely the problem I have

    I would build a small example project and report it. This is definitely not a bug I'd like to bump up against.

  15. Jean-Yves P

    Aug 6 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    @Ivan T The clipboard class in xojo is too basic to do something like that. You have to use Declares:
    https://developer.apple.com/documentation/appkit/nspasteboard
    https://docs.microsoft.com/en-us/windows/win32/dataxchg/clipboard

    nice.
    do you have one also for linux ? does even linux handle multiple clipboard content ?

  16. Jean-Yves P

    Aug 6 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    ok, first draft.
    given

    Protected Structure NSRange
      location as Integer
      length as Integer
    End Structure

    then this methods returns all the actual types in a clipboard (on macos only for now)

    Public Function ItemTypes(extends c As Clipboard) as String()
      #If Target64Bit
        Dim SizeOfPointer As Integer = 8
      #Else
        Dim SizeOfPointer As Integer = 4
      #EndIf
      
      #If targetMacOS
        ' NSObject
        Declare Function alloc Lib CocoaLib selector "alloc" (class_id As Ptr) As Ptr
        ' NSArray
        Declare Function initWithArray Lib CocoaLib selector "initWithArray:copyItems:" (obj_id As Ptr, anArray As Ptr, flag As Boolean) As Ptr
        Declare Function m_count Lib CocoaLib selector "count" ( obj As Ptr ) As UInteger
        Declare Sub getObjects Lib CocoaLib selector "getObjects:range:" (obj_id As Ptr, aBuffer As Ptr, aRange As NSRange)
        ' NSPasteBoard
        Declare Function NSClassFromString Lib CocoaLib (aClassName As CFStringRef) As Ptr
        Declare Function generalPasteboard Lib CocoaLib selector "generalPasteboard" (obj_id As Ptr) As Ptr
        Declare Function types Lib CocoaLib selector "types" (obj_id As Ptr) As Ptr
        ' NSString
        Declare Function CFRetain Lib CarbonLib (cf As Ptr) As CFStringRef
        
        Dim pasteboardRef As Ptr = generalPasteboard(NSClassFromString("NSPasteboard")) ' get actual general pasteboard
        If pasteboardRef <> Nil Then
          Dim anArrayRef,nsarray_id As Ptr
          anArrayRef = types(pasteboardRef)
          nsarray_id = initWithArray(alloc(NSClassFromString("NSArray")), anArrayRef, False)
          
          Dim retArray() As String
          
          Dim arrayRange As NSRange
          arrayRange.location = 0
          arrayRange.length = m_count( nsarray_id)
          Dim m As New MemoryBlock(SizeOfPointer*arrayRange.length)
          getObjects nsarray_id, m, arrayRange
          Dim n As Integer = arrayRange.length-1
          For i As Integer = 0 To n
            retArray.append CFRetain((m.Ptr(i*SizeOfPointer)))
          Next
          
          Return retArray
          
        End If
      #EndIf
    End Function
  17. Emile S

    Aug 6 Europe (France, Strasbourg)

    I was silent because I was creating an example (MacOS’ El Capitan) of what I wrote earlier.

    Temporary result: I was able to store a RTF Text and a Sound (MP3) in the Clipboard.

    I do not know why the Text and Picture are not there.

    Returned data from the Script Editor:

    {{«class RTF », 3174}, {«class MPG3», 34056}}

    Now, it is time for me to dinner, then watch TV. With a bit of luck, I will finish that simple project by tomorrow.

    Nota: I stored in a Folder four files; Text, RTF, MP3 and PNG. I read then in a button sequencially (by file name) and place the data in the CLipboard using Clip.AddRawData(Sound_MB, "public.mp3") for example.

    I think I do not use the correct public.<kind-name> entries for Text (I use the LR value: "public.text") and Picture (I used: "public.image").

  18. Jean-Yves P

    Aug 6 Pre-Release Testers, Xojo Pro Answer Europe (France, Besançon)
    Edited 2 weeks ago

    this second method still extends the clipboard, and let you get the data you want as a string, given the datatype present in the clipboard you provide. still only for macos, will do other platforms as I need it. if any of you add this windows or linux feature, please contribute to this thread ! thanks.

    Public Function ItemData(extends c as Clipboard, aClipboardDataType as String) as String
      
      #If targetMacOS
        ' NSData
        Declare Function m_length Lib CocoaLib selector "length" (id As Ptr) As Integer
        Declare Sub getBytes Lib CocoaLib selector "getBytes:length:" ( id As Ptr, buffer As Ptr, length As UInteger )
        ' NSPasteBoard
        Declare Function NSClassFromString Lib CocoaLib (aClassName As CFStringRef) As Ptr
        Declare Function generalPasteboard Lib CocoaLib selector "generalPasteboard" (obj_id As Ptr) As Ptr
        Declare Function dataForType Lib CocoaLib selector "dataForType:" (obj_id As Ptr, type As CFStringRef) As Ptr
        
        Dim pasteboardRef As Ptr = generalPasteboard(NSClassFromString("NSPasteboard")) ' get actual general pasteboard
        If pasteboardRef <> Nil Then
          
          Dim p As Ptr = dataForType ( pasteboardRef, aClipboardDataType)
          Dim sLen As UInteger = m_length( p)
          If sLen>0 Then
            Dim m As New MemoryBlock( sLen )
            getBytes   p, m, sLen
            Return m.StringValue( 0, sLen)
          Else
            Return ""
          End If
          
        End If
      #EndIf
      
      #If TargetWindows
        Raise New PlatformNotSupportedException
      #EndIf
      #If TargetLinux
        Raise New PlatformNotSupportedException
      #EndIf
      
    End Function
  19. Jean-Yves P

    Aug 6 Pre-Release Testers, Xojo Pro Europe (France, Besançon)
    Edited 2 weeks ago

    this above method now gives me the tiff data I want, just had to cast it to a picture using picture.fromdata.

  20. Jean-Yves P

    Aug 6 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    and another small useful method for the same price ...

    Public Function ItemTypesCount(extends c As Clipboard) as Integer
      Return c.ItemTypes.Ubound+1
      
    End Function
  21. Emile S

    Aug 6 Europe (France, Strasbourg)

    Hi all,

    the code to load RTF, TEXT, MP3 and PNG are meaningless, so are the code to store some data as String in MemoryBlock and so I do not copied them here.

    I only shared the code to place these different resources in the Clipboard. The used UTI seems to be correct. The order of the lines below (lines that starts with: Clip.AddRawData) change nothing (exept the order of appearance of the class in the Clipboard.

    Only MP3 and RTF appears in the Clipboard and I do not know why / I was not able to change the result. Probably my error (or Xojo ?). I checked in the debugger, the used variables (below) hold the data, but for some unknow reason, they are lost.

    // Fills the Clipboard
    Dim Clip As  New Clipboard
    
    Clip.AddRawData(Pict_MB,  "public.image") // Pict_MB is a MemoryBlock where I stored an image as String
    Clip.AddRawData(Sound_MB, "public.mp3")   // Sound_MB is a MemoryBlock where I stored a sound as String
    Clip.AddRawData(Text_RTF, "public.rtf")   // Text_RTF is a MemoryBlock where I stored a text RTF source as String
    Clip.AddRawData(Text_Raw, "public.text")  // Text_Raw is a MemoryBlock where I stored a Raw text as String
    
    // Export the Clipboard
    Clip.Close

or Sign Up to reply!