Need direction for dropping text files into text view...

Hi all,

I’m exhausted trying to figure out how to drop a .doc file into a text view. Ican’t even write a droplet to read the text from the file.

I just want to drag a .doc onto my text view and see its contents. I swear I’m not lazy, I just have so many other parts of this app to develop, and I’ve already wasted a day and a half on this. Can anyone help, or just get me started?

Thank you so much!

Hi Doug,

you can add an on drop handler to the text view:

on drop theObject drag info dragInfo
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		set preferred type of pasteboard of dragInfo to "file names"
		set thePaths to contents of pasteboard of dragInfo
		if (count of thePaths) is 1 then
			-- add test for .doc file extension here
			if ((call method "loadContentsFromFile:" of theObject with parameter (item 1 of thePaths as string)) ≠ 1) then
				display dialog "loading the document failed"
			end if
		end if
	end if
	
end drop

and add the method for loading Word/RTF/text etc. in an NSTextView (just add this two files in your project):

file: MyNSTextViewAdditions.h

#import <Cocoa/Cocoa.h>

@interface NSTextView (Additions) 

-(BOOL)loadContentsFromFile:(NSString *)thePath;

@end

file: MyNSTextViewAdditions.m

#import "MyNSTextViewAdditions.h"

@implementation NSTextView (Additions)

-(BOOL)loadContentsFromFile:(NSString *)thePath{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSURL *theURL = [NSURL fileURLWithPath:thePath];
	NSAttributedString *fileContent = [[NSAttributedString alloc] initWithURL:theURL documentAttributes:nil];
	if (fileContent == nil) { 
		[pool release];
		return NO;
	}
	[[self textStorage] setAttributedString:fileContent];
	[pool release];
	return YES;
}

@end

Then your text view should show the textual content of dropped word documents.

D.