need help setting color of text please

G’day scripters

I’ve spent 3 hours trying to set the color of a single word in a text field, to no avail.

Please tell me what I’m doing wrong!

Regards

Santa



if theObjectName = "InitialPrinterList1" then
		set temp to "This printer will print the rtfd's, Excel & Word documents. REMEMBER! For all the printers set in this application, corrosponding print presets must be set in each printing application."
		set content of text field "Help" of theWindow to temp
		set textRef to a reference to (text view "Help" of text field "Help" of theWindow)
		set text color of word 11 of textRef to {60000, 1, 1}
	end if
	

EDIT. This doesn’t crash, but doesn’t do anything either. And how do I set it to change color?


if theObjectName = "InitialPrinterList1" then
		set temp to "This printer will print the rtfd's, Excel & Word documents. REMEMBER! For all the printers set in this application, corrosponding print presets must be set in each printing application."
		set content of text field "Help" of theWindow to temp
		set fontDone to {"Times", 18}
		set positionOfWord to 11
		set excerciseTextView to (contents of text field "Help" of theWindow)
		call method "setFont:" of (excerciseTextView's word positionOfWord) with parameter (call method "fontWithName:size:" of class "NSFont" with parameters fontDone)
	end if
	

Hi Santa,

changing the color of a part of a string can only be done in Objective-C with NSAttributedString.
But it’s quite easy:

¢ Create a new ObjC class in Xcode with Menu File > New File. > Objective-C Class and name it SKText
¢ replace the contents of SKText.h with

[code]#import <Cocoa/Cocoa.h>

@interface SKText : NSObject {}

  • (void)setTextColor:(NSArray *)colorArray ofString:(NSString *)string inTextField:(NSTextField *)field;

@end[/code]
¢ replace the contents of SKText.m with

[code]#import “SKText.h”

@implementation SKText

  • (void)setTextColor:(NSArray *)colorArray ofString:(NSString *)string inTextField:(NSTextField *)field
    {
    NSColor *color = [NSColor colorWithDeviceRed:[[colorArray objectAtIndex:0] floatValue] green:[[colorArray objectAtIndex:1] floatValue] blue:[[colorArray objectAtIndex:2] floatValue] alpha:1.0];
    NSString *text = [field stringValue];
    NSRange range = [text rangeOfString:string];
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:text];
    [attributed addAttribute:NSForegroundColorAttributeName
    value:color
    range:range];
    [field setAttributedStringValue:attributed];
    [attributed release];

}

@end[/code]
¢ in your AppleScript code use


if theObjectName = "InitialPrinterList1" then
    set temp to "This printer will print the rtfd's, Excel & Word documents. REMEMBER! For all the printers set in this application, corresponding print presets must be set in each printing application."
    set string value of text field "Help" of theWindow to temp
    set textRef to a reference to text field "Help" of theWindow
    set positionOfWord to 11
    call method "setTextColor:ofString:inTextField:" of class "SKText" with parameters {{1.0, 0.0, 0.0}, word positionOfWord of temp, textRef}
end if

Note:
the three parameters of the method are the color (a list of three float values between 0.0 and 1.0 in order R G B), the string to be colored, and the reference to the text field

G’day Stefan

I don’t have Objective-C listed, only C and C++. Should I be using one of these?

Regards

Santa

No, Objective-C is category Cocoa

G’day Stefan.

Thank you VERY much, that works a treat.

Regards

Santa

I recommend to add a little error handling.
Insert this line in SKText.m after the NSRange line:

if (range.location == NSNotFound) return;

If the string doesn’t match the value of the text field, nothing happens

This might just be what I am looking for, finally!

I am trying to modify it to fit my needs. I want to have every certain words such as “Pass” and “Fail” have a different color. In my AppleScript I did this:
set textRef to a reference to (text view “detailed results” of scroll view 1 of drawer “drawer” of window “main”)
call method “setTextColor:ofString:inTextField:” of class “SKText” with parameters {{0.0, 1.0, 0.0}, “Pass”, textRef}
call method “setTextColor:ofString:inTextField:” of class “SKText” with parameters {{1.0, 0.0, 0.0}, “Fail”, textRef}

However, I seem to be getting a handler error. Any ideas what I may be doing wrong?

Thanks so much StefanK! I really appreciate all your posts on this forum as I have seen many other useful ones come from you.

-Brett

the method expects a reference to a text field, not a text view

Looks, like i just didn’t read it quite close enough, do you know of a way(such as modifying that code) to do this for Text Views?

Thanks for your help,
Brett

this is a modified method to change colors of several strings in a text view

[code]+ (void)setTextColors:(NSArray *)colors OfStrings:(NSArray *)strings inTextView:(NSTextView *)view
{
int i;
NSArray *colorArray;
NSColor *color;
NSRange range;
NSString *text;
NSMutableAttributedString *attributed;

text = [[view textStorage] string];
attributed = [[NSMutableAttributedString alloc] initWithString:text];
for (i=0;	i< [colors count]; ++i) {
	colorArray = [colors objectAtIndex:i];
	color = [NSColor colorWithDeviceRed:[[colorArray objectAtIndex:0] floatValue] green:[[colorArray objectAtIndex:1] floatValue] blue:[[colorArray objectAtIndex:2] floatValue] alpha:1.0];
	range = [text rangeOfString:[strings objectAtIndex:i]];
	if (range.location != NSNotFound) {
		[attributed addAttribute:NSForegroundColorAttributeName
						   value:color
						   range:range];
	}
	
}
[[view textStorage] setAttributedString:attributed];
[view display];
[attributed release];

}[/code]
using your example keywords, you can call the method with


set viewRef to a reference to text view "textView" of scroll view "scrollView" of window "main"
call method "setTextColors:OfStrings:inTextView:" of class "SKText" with parameters {{{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}}, {"fail", "pass"}, viewRef}