image in text view

Hi,

Is it possible to add an image to a text view like this:

set contents of text view "text" of window "main" to "this is the text before the image /folder/image.jpg this text goes after the image" 
--where /folder/image.jpg is the path to a jpeg image

or is there a workaround?

While you can put an image in a text view, I don’t think you can do it by embedding a link like that. I imagine that you need to do something like:


set contents of text view "text" of window "main" to {"this is the text before the image",anImage,"this text goes after the image"}
--where anImage is a variable that contains an image 

If that (or something similar) doesn’t work, I doubt you can do it in code.

I don’t think this is possible with AppleScript.

Here an NSTextView category for inserting images you can call from your script with ‘call method’:

[code]// NSTextViewAddition.m

#import <Cocoa/Cocoa.h>

@interface NSTextView (Addition)

-(BOOL)insertImage:(NSImage *)img atIndex:(int)index; // inserts image data
-(BOOL)insertImageFromPath:(NSString *)path atIndex:(int)index; // inserts a disc based image

@end

@implementation NSTextView (Addition)

-(BOOL)insertImage:(NSImage *)img atIndex:(int)index{
if (index > [[self string] length] || index < 0) return NO;
NSTextAttachmentCell *ic = [[NSTextAttachmentCell alloc] initImageCell:img];
if (ic == nil) return NO;
NSTextAttachment *at = [[NSTextAttachment alloc] init];
[at setAttachmentCell:ic];
[ic release];
if (at == nil) return NO;
NSAttributedString *insertString = [NSAttributedString attributedStringWithAttachment:at];
[[self textStorage] insertAttributedString:insertString atIndex:index];
[at release];
return YES;

}

-(BOOL)insertImageFromPath:(NSString *)path atIndex:(int)index{
if (index > [[self string] length] || index < 0) return NO;
NSFileWrapper *fw = [[NSFileWrapper alloc] initWithPath:path];
if (fw == nil) return NO;
NSTextAttachment *at = [[NSTextAttachment alloc] initWithFileWrapper:fw];
[fw release];
if (at == nil) return NO;
NSAttributedString *insertString = [NSAttributedString attributedStringWithAttachment:at];
[[self textStorage] insertAttributedString:insertString atIndex:index];
[at release];
return YES;
}

@end[/code]
add the above file to your project, then you can call it like this:

set imgPath to (POSIX path of (choose file))
set insertAtIndex to 5
set succeeded to (call method "insertImageFromPath:atIndex:" of myTextView with parameters {imgPath, insertAtIndex})

set img to load image (POSIX path of (choose file))
set insertAtIndex to 5
set succeeded to (call method "insertImage:atIndex:" of myTextView with parameters {img, insertAtIndex})

Very good Dominik, it worked perfectly. I added your code and a button to the plain text editor example and it work. Except that when I try to reopen the document after having saved it, a question mark appears where the picture was. Any ideas on how to save the image?

Thank you,

gecko

Hi gecko,

as far as I remember ‘Plain Text Editor’ writes and reads only plain text documents (the text view’s content is saved as string??). You will have to modify the document’s reading and writing to handle rtfd. It might again be necessary to use some call methods. NSText has two convenient methods for this: “writeRTFDToFile:atomically:” and “readRTFDFromFile:” …

D.