loop through clipboard content ?

  1. 4 months ago

    Jean-Yves P

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

    Hi there,

    sometimes in a clipboard there are multiples items for representing a single value.
    if you copy styled text, you have the styled text, but also the rtf or even the raw text in the clipboard.

    how do you loop through all the items in the clipboard to choose the one you need ( to paste)
    it seems the clipboard.rawdataavailable only checks the first item type
    also clipboard.pictureavailable seems to returns false if the image type (jpg,png, or tiff) is not in the first position ?

    is there a way to do this, just like you can do it with dragitems.

    thanks.

    Edit: macos (but would like win too) xojo 2019r11

    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
  2. Emile S

    Aug 6 Europe (France, Strasbourg)

    In the example below:

    Dim s As String
    s = TextArea1.Text // contains RTF
    
    Dim c As Clipboard
    c.AddRawData(s,"public.rtf") // UTI
    c.Close

    You replacee public.rtf with the kind of data you want to set…

    For getting Data, you use the “reverse instructions”:

    If c.RawDataAvailable("public.text") Then
      TextField1.Text = c.RawData("public.text")
    End If
    c.Close

    Is it clear ?

  3. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro, Third Party Store GraffitiSuite Developer

    You cannot, as far as I'm aware, loop through all of the clipboard's contents. I wish you could, but that's currently not supported.

  4. Emile S

    Aug 6 Europe (France, Strasbourg)
    Edited 4 months ago

    I do not talked about scanning (as in a loop): I only ask a kind of data (here rtf) from the Clipboard so the OS returned it to me, if available.

    Edit:
    Copy text in the Clipboard (from an application) and scan what there is in the Clipboard with the appropriate tool.

    Example on MacOS El Capitan:
    I copied the text above from Firefox,
    fired the Apple Script Editor,$Typed clipboard info
    and get:

    {{«class utf8», 298}, {«class ut16», 598}, {string, 298}, {Unicode text, 596}}

    First part: class kind of the data («class utf8[ and length of data 298 for example.

    If you ask to get class ut16, you will get what you ask. You do not ask field #2 of the Clipboard contents.

    And this is XPlatform (in the design); you only have to pass the correct type of data (above it was public.text or something else).

  5. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro, Third Party Store GraffitiSuite Developer
    Edited 4 months ago

    @Emile S I do not talked about scanning (as in a loop): I only ask a kind of data (here rtf) from the Clipboard so the OS returned it to me, if available.

    But they specifically asked about looping.

  6. Emile S

    Aug 6 Europe (France, Strasbourg)

    @Anthony C But they specifically asked about looping.

    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)..

  7. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro, Third Party Store GraffitiSuite Developer

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

  8. 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

  9. Emile S

    Aug 6 Europe (France, Strasbourg)
    Edited 4 months ago

    No Brian, standard humor ;)

    Thanks for it !

  10. 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

  11. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro, Third Party Store GraffitiSuite Developer
    Edited 4 months 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.

  12. 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.

  13. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro, Third Party Store GraffitiSuite Developer
    Edited 4 months 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.

  14. 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.

  15. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro, Third Party Store 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.

  16. 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 ?

  17. Sascha S

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

    @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.

  18. 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

  19. Anthony C

    Aug 6 Pre-Release Testers, Xojo Pro, Third Party Store GraffitiSuite Developer
    Edited 4 months 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.

  20. 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 ?

  21. Newer ›

or Sign Up to reply!