Strings mangled on generalPasteboard
Strings mangled on generalPasteboard
- Subject: Strings mangled on generalPasteboard
- From: Kevin Walzer <email@hidden>
- Date: Mon, 23 Nov 2009 08:06:27 -0500
- Organization: WordTech Communications LLC
I'm writing a tool that wants to make NSString data available after a
drag operation to the general clipboard: the idea is to extract the data
from the draggingPasteboard and then write the string data to the
generalClipboard.
If the dragtype is NSString or NSURL, this works as expected. The same
data that is dragged comes out intact on the clipboard. However, file
paths are inexplicably converted to file URL's, so that instead of
"/Users/kevin/Desktop/foo.txt," I get
"file://localhost/Users/kevin/Desktop/foo.txt." I want to retain the
path string instead of it being converted to a URL, and since I can't
find a convenient method to convert file URL's back to simple path
strings, I'm a bit stuck.
Additionally, when I drag several files to the drag destination, only a
single file URL is returned. Given that my code converts the array of
file names to a single NSString separated by tabs via
componentsJoinedByString, this is especially unexpected. When I see this
output from NSLog in the drag data:
/Users/kevin/tk85-patched/htmlwidget-hv.diff
/Users/kevin/tk85-patched/htmlwidget-new.diff
/Users/kevin/tk85-patched/htmlwidget.diff
/Users/kevin/tk85-patched/htmlwidget.tar.bz2.tar
I get this output from the general clipboard:
file://localhost/Users/kevin/tk85-patched/htmlwidget.tar.bz2.tar
My performDragOperation code is below. Any advice is appreciated.
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
// NSArray types;
dragpasteboard = [sender draggingPasteboard];
//retrieve string data from clipboard
NSArray *types = [dragpasteboard types];
NSString *pasteboardvalue = nil;
for (NSString *type in types) {
//string type
if ([type isEqualToString:NSStringPboardType]) {
pasteboardvalue = [dragpasteboard stringForType:NSStringPboardType];
//URL; convert to string
} else if ([type isEqualToString:NSURLPboardType]) {
pasteboardvalue = [[NSURL URLFromPasteboard:dragpasteboard]
absoluteString];
//file array, convert to string
} else if ([type isEqualToString:NSFilenamesPboardType]) {
NSArray *files = [dragpasteboard
propertyListForType:NSFilenamesPboardType];
NSString *filename;
filename = [files componentsJoinedByString:@"\t"];
pasteboardvalue = filename;
NSLog(pasteboardvalue);
}
//get the string from the drag pasteboard to the general pasteboard
NSPasteboard *generalpasteboard = [NSPasteboard generalPasteboard];
NSArray *pasteboardtypes = [NSArray
arrayWithObjects:NSStringPboardType, nil];
[generalpasteboard declareTypes:pasteboardtypes owner:self];
[generalpasteboard setString:pasteboardvalue forType:NSStringPboardType];
}
return YES;
}
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden