Xamarin.Mac copy a file to NSPasteBoard

Here is a quick code snippet for Xamarin.Mac or MonoMac to copy a file to the NSPasteBoard so that it can be pasted with finder… or presumably any app that can have a file pasted into it.

NSArray objects = NSArray.FromNSObjects(new NSObject[]{new NSString("/path/to/file")});
NSPasteboard pb = NSPasteboard.GeneralPasteboard;
pb.ClearContents();
pb.DeclareTypes(new string[]{NSPasteboard.NSFilenamesType}, null);
pb.SetPropertyListForType(objects, NSPasteboard.NSFilenamesType);

I hope this saves someone some time as I could not find any other resource that gave a good example.

Leave a Reply

Your email address will not be published. Required fields are marked *