How to add a NSFilePromiseProvider to the NSPasteboard

I have an app that has a finder extension. What I would like to do is add a 'copy' menu item for files in my virtual file system that would allow my app to perform the actual file copy to the paste destination. The NSFilePromiseProvider looks like it would allow me to do just that if I could only get it to work.

I have include my test app code which I have not been able to get to work. The NSFilePromiseProvider method filePromiseProvider:fileNameForType: gets called but the strange thing is that the clipboard viewer show an empty string for the 'com.apple.pasteboard.promised-file-name'.


@interface ClipboardMacPromise : NSFilePromiseProvider<NSFilePromiseProviderDelegate>
@end
@implementation ClipboardMacPromise
- (id)initWithFileType:(NSString*)type
{
	NSLog(@"filePromiseProvider Init");
	return [super initWithFileType:type delegate:self];
}
- (NSString *)filePromiseProvider:(NSFilePromiseProvider*)filePromiseProvider fileNameForType:(NSString *)fileType
{
	NSLog(@"filePromiseProvider fileNameForType");
	return @"adoc.pdf";
}
- (void)filePromiseProvider:(NSFilePromiseProvider*)filePromiseProvider writePromiseToURL:(NSURL *)url completionHandler:(void (^)(NSError * _Nullable errorOrNil))completionHandler
{
	NSLog(@"filePromiseProvider writePromiseToURL");
	// Finder can't paste, so we never get here...
}
@end

@interface AppDelegate ()
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
		NSPasteboard* pboard = [NSPasteboard generalPasteboard];
		[pboard clearContents];
		NSMutableArray* items = [[NSMutableArray alloc] init];
		ClipboardMacPromise* object = [[ClipboardMacPromise alloc] initWithFileType:@"com.adobe.pdf"];
		//NSURL *object = [[NSURL alloc] initFileURLWithPath:@"/Users/me/adoc.pdf"];

		[items addObject:object];
		if (![pboard writeObjects:items])
			NSLog(@"writeObjects failed");
}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
	// Insert code here to tear down your application
}


@end

Replies

I'm struggling with more or less the same problem. Did you ever find a solution?

  • No, I ended up abandoning this approach to my problem.

Add a Comment