pasting file (not filename or filecontent) to text view via pasteboard

I’ve been looking for a solution for pasting a file (not the content of the file, not the filename/filepath) to a text view for quite a while.

I would like to start an application with a text view and paste a file to it, just as the user would have dropped it onto the text view.

Afterwards the user can add text to the text view or drop another file onto the text view.

By quitting the app I’d like to use the command

call method "writeRTFDToFile:atomically:" of object obj with parameters {DokumentPathFilename, true}

to write the contents of the text view as a .rtfd file.

In the .rtfd file the pasted file (text file, image etc.) is included and may be opened just by clicking it in the .rtfd file.

The text view is enabled for graphics in the IB. And a “user file drop” is no problem.

I’ve been trying to set the contents of a pasteboard to a file, but without success.

Thought it must be possible because the Applescript Srudio Reference said a pasteboard “general” can have the format type “file”.

http://developer.apple.com/documentation/AppleScript/Reference/StudioReference/sr3_app_suite/sr_app.html#//apple_ref/doc/uid/20011217-BAJFJJEF

Here is my (not working) code:

set preferred type of pasteboard "general" to "file"
set contents of pasteboard "general" to (POSIX path of ("Macintosh HD:Users:stephanbansemer:Desktop:test.jpg" as alias))
set preferred type of pasteboard "general" to "file"
set contents of text view "Content" of scroll view "Content" of window "main" to contents of pasteboard "general"

Any help would be appreciated !

System 10.5.5
IB 3.1.2
XCode 3.1.2

I believe my first explanation was confusing…

OK, what I want to do is write an application with an text view. By passing a path to this application at program start, I would like to load (or paste via a pasteboard) a file to the text view, just like the user would have dropped this file onto the text view.

In other words: At program start I would like to simulate a file drop on the text view - which would be done by the pasteboard of draginfo, if it would be a ‘user file drop’.

I think I should explain the whole routine to get things clearer:

I have a filemaker database which contains filepath to files. I want to pass one or more of these filepaths to
my app and attach the file(s) (not the content/filepath) to the text view.
Afterwards the user can add some text or drop another file onto the text view.
If the user push the button “save” in my app I’ll write the contents of the Text view (including the file(s)) to
a new rtfd file.

The attached file(s) should look like they were dropped onto the text view (the icon of the file(s) show in
the text view).

I thought I can do this by setting the content of the pasteboard “general” to the file or something like this, because
the “general” pasteboard can be used without drop handler, but I can’t set the content of pasteboard “general” to
the file nor paste the content of the pasteboard to the text view.

Maybe this is only possible with an “call method” command…

Stephan

Unfortunately I still haven’t managed to solve the problem that I’ve been trying to describe.

What I need is an application that automatically pastes a file into a text-view. Up until now I have managed without any problem to automatically paste the file-path and also the file content but not the file itself. My question is simply: how do I do this?

When you drag a file into a text-view, the file itself is pasted and NOT the file path or the file contents.

Again,

any help would be appreciated !

Stephan

Just will give it one more try:

What I want to do is paste a file to a text view as an attachment.

Alternative, I could write an rtfd file from the text-view and attach the file to the rtfd document.

Anyone ever done something like this ?

I wrote you an object-c class which may help. If you pass it a path and a text view then it will set the image of the file for the path into the text view. So my suggestion would be that when you drop a file onto your text view that you get the path to the dropped file (as a posix path) then pass that and a reference to the text view to my custom class.

Add a new file to your project and make it an objective-c class. I called my new class “HMTextView”.
====== make the “.h” file look like this =========
#import <Cocoa/Cocoa.h>

@interface HMTextView : NSObject {

}
+(void)putIconForPath:(NSString*)path inTextView:(NSTextView*)textView;
@end

===== make the .m file look like this ===========
#import “HMTextView.h”

@implementation HMTextView

+(void)putIconForPath:(NSString*)path inTextView:(NSTextView*)textView {
NSFileWrapper *filew = [[[NSFileWrapper alloc] initRegularFileWithContents:[NSData dataWithContentsOfFile:path]] autorelease];
[filew setPreferredFilename:[path lastPathComponent]];

NSTextAttachment *fileAttatchment = [[[NSTextAttachment alloc] initWithFileWrapper:filew] autorelease];
NSAttributedString *fileAttString = [NSAttributedString attributedStringWithAttachment:fileAttatchment];

[textView insertText:fileAttString];

}

@end

===== then in your applescript code you can use it like this =============

call method "putIconForPath:inTextView:" of class "HMTextView" with parameters {POSIX path of filePath, myTextView}

I hope that helps.

Hi regulus6633,

this is EXACTLY what I was looking for !
Pass it a filepath and a text-view and you get the file attached to the text-view.

Many thanks!

Very impressive to see it done by a few lines of code ! I’ll have to learn Objective-C !

One small question about the class:
Is it not possible to pass a rtfd file as attachment to the class ?
The file seems to be attached in the text view, but if you write a new document by using

call method "writeRTFDToFile:atomically:" of object obj with parameters {DokumentPathFilename, true}

the attached rtfd is an empty file…

Anyway, thanks again for this class !

Stephan

I’m glad it works like you wanted. Objective-C can do a lot more things than applescript (and it’s much faster), but it takes a lot of effort to learn it if you’re coming from an applescript-only background like I did. But if you stick with it eventually it will make sense. Now I write all my programs in obj-c and add applescript code as needed.

I couldn’t get the call method to work for writing the file either, but it does work in my class. So make the class look like this…

=========== .h ================
#import <Cocoa/Cocoa.h>

@interface HMTextView : NSObject {

}
+(void)putIconForPath:(NSString*)path inTextView:(NSTextView*)textView;
+(void)writeRTFDAtPath:(NSString*)path atomically:(BOOL)isAtomically forTextView:(NSTextView*)textView;
@end

=========== .m ================
#import “HMTextView.h”

@implementation HMTextView

+(void)putIconForPath:(NSString*)path inTextView:(NSTextView*)textView {
NSFileWrapper filew = [[[NSFileWrapper alloc] initRegularFileWithContents:[NSData dataWithContentsOfFile:path]] autorelease];
NSString
fileName = [path lastPathComponent];
[filew setFilename];
[filew setPreferredFilename];

NSTextAttachment *fileAttatchment = [[[NSTextAttachment alloc] initWithFileWrapper:filew] autorelease];
NSAttributedString *fileAttString = [NSAttributedString attributedStringWithAttachment:fileAttatchment];

[textView insertText:fileAttString];

}

+(void)writeRTFDAtPath:(NSString*)path atomically:(BOOL)isAtomically forTextView:(NSTextView*)textView {
[textView writeRTFDToFile:path atomically:isAtomically];
}

@end

========== applescript code ==============

call method "writeRTFDAtPath:atomically:forTextView:" of class "HMTextView" with parameters {POSIX path of filePath, true, myTextView}