Oskar
Oskar

Reputation: 329

Get path of file from clipboard in OS X

I would like my app to get the full path of the file copied to the clipboard before.

I tried this:

NSPasteboard *p = [NSPasteboard generalPasteboard];
NSDictionary *options = [NSDictionary dictionary];
NSString *path = [[p readObjectsForClasses:[NSArray arrayWithObjects:[NSString class], nil] options:options] objectAtIndex:0];

This only returns the filename, not the path.

Can you help me?

Upvotes: 3

Views: 2371

Answers (1)

dalton_c
dalton_c

Reputation: 7170

Try this:

NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classes = [NSArray arrayWithObject:[NSURL class]];

NSDictionary *options = [NSDictionary dictionaryWithObject:
                         [NSNumber numberWithBool:YES] forKey:NSPasteboardURLReadingFileURLsOnlyKey];

NSArray *fileURLs =
[pasteboard readObjectsForClasses:classes options:options];

That's straight from Apple's Pasteboard Programming Guide.

Upvotes: 10

Related Questions