Can drag and drop file URL text, but not actual file - objective-c

I'm able to drag a file URL from a NSTableView cell and drop it in any text editor. According to my understanding of Apple's documentation though, when I drop the URL, the file should be copied to the drop location.
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard {
//for now putting specific file path in just to get it working
NSString *filePath = #"/Users/Jackh/Desktop/Apple.png";
[pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType] owner:self];
[[NSURL URLWithString:filePath] writeToPasteboard:pboard];
return YES;
}
How do I get the file to copy from the filePath to the dropped location? It's just moving filePath as text for now.
Any ideas?
Edit: I am now using this code
-(void)awakeFromNib {
[self.tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
}
...
[self.tableView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSFileContentsPboardType, nil]];
...
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard {
//for now putting specific file path in just to get it working
NSString *filePath = #"/Users/Jackh/Desktop/Apple.png";
[pboard declareTypes:[NSArray arrayWithObject:NSFileContentsPboardType] owner:nil];
[pboard writeFileContents:filePath];
}

If you want to force the green "+" to copy, just use NSDragOperationCopy (only) in the operation mask, disallowing other operations; e.g. in awakeFromNib:
[self.tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
Here is the best way I could find to make your example work with the Finder:
- (BOOL)
tableView:(NSTableView *)tv
writeRowsWithIndexes:(NSIndexSet*)rowIndexes
toPasteboard:(NSPasteboard*)pboard {
NSString *filePath = #"/Users/kevin/Desktop/1.png";
[pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
owner:nil];
[pboard setPropertyList:[NSArray arrayWithObject:filePath]
forType:NSFilenamesPboardType];
return YES;
}
Note that Apple recommends using more modern APIs but I have found in cases like this they have a big side effect: they seem to cause files to be copied twice because URLs are magically copied in multiple forms. Having said that, your original URL example didn't work because you should have used writeObjects: on the pasteboard instead of asking the URL to writeToPasteboard:. An example with URLs:
- (BOOL)
tableView:(NSTableView *)tv
writeRowsWithIndexes:(NSIndexSet*)rowIndexes
toPasteboard:(NSPasteboard*)pboard {
NSString *filePath = #"/Users/kevin/Desktop/1.png";
[pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType]
owner:nil];
[pboard writeObjects:[NSArray arrayWithObject:
[NSURL fileURLWithPath:filePath]]];
return YES;
}
As I noted in other comments, if your drag is targeting a document instead of a file manager such as the Finder then it's better to include the actual data for the file. Raw data can't be misinterpreted by a document (i.e. the document has to insert the data directly, it can't choose to insert a path string instead). On the other hand, raw-data drags don't create files in the Finder for some reason so they're useful mainly as something to add to the pasteboard as an alternative.
Here is a way to use a UTI to declare the file type and call NSData to read a file (I tried this and it works, e.g. I can drag a table view row into a Rich Text document in TextEdit and see an image inserted into the window):
- (BOOL)
tableView:(NSTableView *)tv
writeRowsWithIndexes:(NSIndexSet*)rowIndexes
toPasteboard:(NSPasteboard*)pboard {
NSString *filePath = #"/Users/kevin/Desktop/1.png";
[pboard declareTypes:[NSArray arrayWithObject:#"public.png"]
owner:nil];
[pboard setData:[NSData dataWithContentsOfFile:filePath]
forType:#"public.png"];
return YES;
}
(I don't know exactly why your writeFileContents: doesn't work but the above does work and is basically the same thing.)

Related

WebKit WebView paste: fails for dynamic UTIs

When copying a URL from chrome on OSX and pasting into an editable WebKit webview, nothing gets pasted.
I verified that there are items on the NSPasteboard and that the NSPasteboardItem has the following types:
"dyn.ah62d4rv4gu8zs3pcnzme2641rf4guzdmsv0gn64uqm10c6xenv61a3k",
"dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu",
"public.utf8-plain-text",
"dyn.ah62d4rv4gu8yg55wqzkgc65yqzvg82pwqvdg22p0r73fk8puqyuda8b1gy5xerwdgk2a",
"dyn.ah62d4rv4gu8yg55wqzkgc65yqzvg82pwqvdg22p0r73fk8puqyuda8b1gy5xerwdg3cu"
I understand that these are auto-generated and map to WebURLsWithTitlesPboardType.
On performing the same operation from safari to webview, it works since it only contains
"public.utf8-plain-text"
Is there a known workaround for handling these UTIs better?
Webkit webviews don't seem to support paste operations for dynamic UTIs. I worked around it by recreating the pasteboard items without those UTIs when a paste: was intercepted in webview:doCommandBySelector:
- (void)cleanupPasteboard:(NSPasteboard *)pasteboard {
NSMutableArray *newItems = [[NSMutableArray alloc] init];
for (NSPasteboardItem *item in pasteboard.pasteboardItems) {
NSPasteboardItem *newItem = [[NSPasteboardItem alloc] init];
for (NSString *type in item.types) {
if (![type hasPrefix:#"dyn"]) {
[newItem setData:[item dataForType:type] forType:type];
}
}
[newItems addObject:newItem];
}
[pasteboard clearContents];
[pasteboard writeObjects:newItems];
}

How to get selected text from dragging?

To detect file name from file, I use code below with success.
file .h :
#interface DropView : NSView <NSDraggingDestination>
file .m
init :
[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
and I implement protocol like this
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender {
NSLog(#"%#", [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]);
}
It return Null when I dragged some text. (highlight text and dragged from other app)
I have tried to change code from
propertyListForType:NSFilenamesPboardType to propertyListForType:NSStringPboardType or propertyListForType:NSPasteboardTypeString
but no luck.
Please help me how to get only selected text?
Thank you.
I have found the solution, Just declare NSPasteboard from sender and get string for type NSStringPboardType
NSPasteboard *pboard = [sender draggingPasteboard];
NSString *string = [pboard stringForType:NSStringPboardType];
Cheer!!

It fails to copy a string to pasteboard

I have a string called text, in the main menu xib file I linked the copy menu item with an action called through an observer.I verified with a breakpoint that this method is actually called, but the problem is that the string isn't really copied to the pasteboard:
- (void) copy: (NSNotification*) notification
{
if([[self window]isKeyWindow])
{
// It always enters in this block
NSPasteboard* pb=[NSPasteboard generalPasteboard];
NSPasteboardItem* item=[[NSPasteboardItem alloc]init];
[pb clearContents];
[item setData: [NSKeyedArchiver archivedDataWithRootObject: text] forType: NSPasteboardTypeString];
[pb writeObjects: [NSArray arrayWithObject: item]];
}
}
After entering in the block it clear all the contents of the pasteboard.But if I try to paste the copied content to text edit, it doesn't paste anything (an empty string), but the string is not of length zero.
I also tried to check the return value of writeObjects, and it returns YES.
You can simply use:
[pb setString:text forType:NSPasteboardTypeString];
Alternatively you probably want to use setString:forType on NSPasteboardItem.

Adding QLPreviewController as subview doesn't load PDF

I'm trying to add a QLPreviewController's view as a subview (no--I cannot use a nav controller or modal). It only shows the fabric background of the QLPreviewController.
I create one and add it as a subview:
QLPreviewController* preview = [[[QLPreviewController alloc] init] autorelease];
preview.dataSource = self;
preview.delegate = self;
preview.view.frame = CGRectMake(0, 0, self.pdfPreviewView.frame.size.width, self.pdfPreviewView.frame.size.height);
self.pdfPreviewView.previewController = preview;
[self.pdfPreviewView addSubview:preview.view];
[preview reloadData];
My QLPreviewControllerDataSource methods work fine (viewing 1 pdf at a time):
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
NSString *path = [[ResourceManager defaultManager] pathForPDF:self.currentPDF];
NSURL *url = [NSURL fileURLWithPath:path];
if ([QLPreviewController canPreviewItem:url]) {
return url; // This always returns
}
return nil; // This line is never executed
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
The data source method always returns the file url, and QLPreviewController says it can open the file, but it never actually does. I just get the background. The self.currentPDF is set before I create the QLPreviewController and does contain the correct information (from CoreData).
The delegate methods never get called. But I'm also not using it in a standard way, so that's not totally unexpected.
I've also tried calling [preview setNeedsLayout], [preview setNeedsDisplay'], and [preview refreshCurrentPreviewItem] but those just call the data source methods and don't change anything.
The PDFs are valid. I can open them in both Xcode and Preview, so that's not the problem. I'm kind of stumped as to why this won't work. Any help would be appreciated in getting this to work.
Turns out I was sending QLPreviewController the wrong path. It wasn't finding the PDF in the bundle correctly. I needed to use pathForResource:ofType:inDirectory.

opening files with different extensions with Cocoa

I have an NSDocument based app that already handles *.txt files. I can drag and drop those files in the dock and my app will launch correctly.
Now I want to be able to open *.text files too. So I have added:
<key>CFBundleTypeExtensions</key>
<array>
<string>.txt</string>
<string>.text</string>
</array>
to my *plist
However I can't open *.text files by drag&dropping them in the dock. (I can do MyApp> File>Open> myFile.text )
I can open *.txt as usual, and drag and drop them in the dock.
I have tried implementing these NSApp delegate methods but now I get an error and I can't open any kind of file.
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename{
NSURL *url = [NSURL URLWithString:filename];
NSError *error = nil;
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:url display:YES error:&error];
if (error) {
NSLog(#"error: %#", [error localizedDescription]);
error = nil;
return NO;
}
return YES;
}
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames{
for (NSString *filename in filenames) {
[self application:sender openFile:filename];
}
}
Error:
typeForContentsOfURL:error: must be
overridden for your application to
support non-'file:' URLs.
I am not subclassing NSDocumentController, so do I really need to? or is there a better/easier way of doing this?
Thanks in advance
The file extensions in the Info.plist file should not contain a period character (.). What happens if you do this?:
<key>CFBundleTypeExtensions</key>
<array>
<string>txt</string>
<string>text</string>
</array>