No way to copy files to the Pasteboard

IvanIcinIvanIcin USMember ✭✭

One good way that works in Objective C would be this one: https://stackoverflow.com/questions/9454638/copy-to-pasteboard-file-paths-and-urls-on-snow-leopard-or-above#12055954

Unfortunately it seems not to be supported as it requires use of NSArray, and Xamarin isn't NSArray-friendly (as alternatives are preferable but in this case there are no alternatives that work). The biggest problem is that NSPasteBoard.WriteObjects requires NSPasteboardReading, and Xcode considers that NSArray that contains only NSUrl conforms to this, while Xamarin doesn't think so, and unfortunately NSUrl[] doesn't work (no exceptions but simply it doesn't get the job done).

One interesting thing is that I could make this work by using NSPasteboard.SetPropertyListType for the dragging pasteboard, but the same code doesn't do anything for the general pasteboard.

Also there is this article on how to do this in Xamarin: http://blog.onobytes.com/2014/07/01/xamarin-mac-copy-a-file-to-nspasteboard/ but that neither works neither it is the proper way to do that (NSUrls are recommended by Apple for this case).

Posts

  • ChrisHamonsChrisHamons USXamarin Team Xamurai

    Xamarin isn't NSArray-friendly (as alternatives are preferable but in this case there are no alternatives that work)

    So we have a sample doing this - https://github.com/xamarin/mac-samples/tree/master/MacCopyPaste and I don't know of many outstanding issues, can you explain your issue, maybe with code.

    Are you having issues just returning an NSUrl []? You have to copy it to an NSArray via NSArray.From () I believe.

  • IvanIcinIvanIcin USMember ✭✭
    edited November 6

    @ChrisHamons, thanks for your assistance.

    I'll try to provide a more detailed explanation. I can create NSArray, it is not a problem. The problem is that if I create NSArray from NSUrl[], I can't use it in NSPasteBoard.WriteObjects as Xamarin detects this as a syntax error (in XCode it is fine). I can use NSUrl[] in NSPasteBoard.WriteObjects, but it doesn't work (my best guess is that you have to use NSArray, but that you can't).

    I have tried many dozens of variants of the code, I can post one of them, but it is just one of dozens that don't do the job. Here is the code:

        var urls = new NSUrl[1];
        urls[0] = NSUrl.FromFileName("filepath");
        var array = NSArray.FromNSObject(urls);
        NSPasteboard.GeneralPasteboard.ClearContents();
        NSPasteboard.GeneralPasteboard.WriteObjects(urls);
        //the above doesn't do anything, and when I put array it can't compile.
    
  • ChrisHamonsChrisHamons USXamarin Team Xamurai

    So I do this in a new XM App:

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
    
            NSButton button = new NSButton (new CGRect (0, 0, 200, 100));
            button.Activated += (sender, e) => {
                var urls = new NSUrl [1];
                urls [0] = NSUrl.FromFilename ("filepath");
                var array = NSArray.FromNSObjects (urls);
                NSPasteboard.GeneralPasteboard.ClearContents ();
                NSPasteboard.GeneralPasteboard.WriteObjects (urls);
            };
            View.AddSubview (button);
        }
    

    And when i push the button, I get this output:

    2017-11-06 14:57:23.840 XMCopyButton[81951:8034590] NSURLs written to the pasteboard via NSPasteboardWriting must be absolute URLs. NSURL 'filepath -- file:///Users/donblas/Projects/XMCopyButton/XMCopyButton/bin/Debug/XMCopyButton.app/Contents/Resources/' is not an absolute URL.

    When i changed it to this:

                var urls = new NSUrl [] { new NSUrl ("/usr/bin/less", false) };
    

    it worked fine.

    NSUrl, along with a number of other bound types, have these interfaces:

    INSPasteboardReading, INSPasteboardWriting

  • IvanIcinIvanIcin USMember ✭✭

    @ChrisHamons , thanks that was what I needed.

Sign In or Register to comment.