Benches macOS

8

Change text color

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I am creating a program with XCode (Applescript Studio), that uses applescript. In that program, I am creating nested lists of strings to become the data set for the NSBrowser object. What I would like is for some strings to be displayed in one color and others in a different color.

Using Applescript, how can I create text of different colors? I have been searching extensively; the manuals mention things like styled text, etc. but say nothing about how to associate colors with the text.

8 replies

Sign in or join Beachball Help to reply.

  1. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    --You might be able to make some variation of this work for you:

    --

    --To try the script below, open up a blank TextEdit window and type in something like: "You can change the color of any word or character of this string to red, or yellow, or green, or, in fact, any color you want."

    tell application "TextEdit"

    activate

    tell text of document 1 --(or front document)

    set the color of every word where it is "color" to {60000, 1, 1} --(where this is an RGB value that yields red, as an example)

    end tell

    end tell

    -- After running the script, all instances of "color" should be red.

    -- You can save the revised document in a normal way, and it will re-open with the changes intact. You may be able then to use the contents of the file for your purposes.

    -- The limitation is that there is no standard direct way of doing the same thing with a string of words, with spaces between them. Coincidentally, I'm just now finishing up a brief AS application that will do just that -- no question that it will be successful with both plain text and rich text -- and it may work with documents written in applications other than TextEdit. It also allows changing the font and size of the string text.

    -- If you want to try the application, I'll email it to you (may be a few days). My only request would be that you send me feedback on its strengths and weaknesses in your use of it. You can ask for it in a response to this post, or by emailing me at [email protected]

  2. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I should modify one statement as follows (changes in BOLD):

    You can save the revised document in a normal way, and *IF IT IS SAVED AS AN ".rtf" document* it will re-open with the changes intact. You may be able then to use the contents of the file for your purposes.

  3. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello Jason Belnap,

    If you are adventurous, you may try something like this.

    NOT tested, for I don't use OSX by myself.

    According to official documents, NSBrowserCell inherits NSCell which has an instance method "setAttributedStringValue:", that is not exposed through AppleScript Studio's terminology. So in theory, I think, something like the following code MAY set a coloured string, that is an attributed string, of a browser cell.

    (NSForegroundColorAttributeName is a constant defined in NSAttributedString AppKit Additions. I'm not sure how to refer to it in AppleScript Studio, so I used it literally, hoping AS Studio knows the constant.)

    cf.

    StudioReference_index.html

    FoundationObjC_index.html

    AppKitObjC_index.html

    AttributedStrings_index.html

    Good luck,

    H

    --SNIPPET (NOT TESTED)

    (* The variable 'browserCell' is assumed to be a reference to a browser cell object *)

    set str to "test string"

    set rgba to {1.0, 0.0, 0.0, 1.0} -- red (opacity 100%)

    set attrStr to mkAttributedString(str, rgba)

    call method "setAttributedStringValue:" of browserCell with parameter attrStr

    on mkAttributedString(str, rgba)

    (*

    string str : string value

    list rgba : list of RGBA values in [0.0, 1.0], i.e. {red, green, blue, alpha}

    e.g.

    rgba = {1.0, 0.0, 0.0, 1.0} -- red (opacity 100%)

    *)

    set aColr to call method "colorWithCalibratedRed:green:blue:alpha:" of class "NSColor" with parameters rgba

    --set aColr to call method "colorWithDeviceRed:green:blue:alpha:" of class "NSColor" with parameters rgba

    set attrsDict to call method "dictionaryWithObject:forKey:" of class "NSDictionary" with parameters {aColr, NSForegroundColorAttributeName}

    --set str to call method "stringWithString:" of class "NSString" with parameter str

    set attrStr to call method "alloc" of class "NSAttributedString"

    set attrStr to call method "initWithString:attributes:" of attrStr with parameters {str, attrsDict}

    return attrStr

    end mkAttributedString

    --END OF SNIPPET

  4. 0

    Jason Belnap

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I tried inserting what you added here, but when I ran it, I got the error message:

    "The variable NSForegroundColorAttributeName is not defined. (-2753)"

    Does it look like there was some mistake in the code snippet you wrote?

  5. 0

    Jason Belnap

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks for your reply. I'd be glad to test out your AS application. What is it exactly that it is supposed to do and how would you suggest that I incorporate it?

  6. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello Jason Belnap,

    Ahh, yes. It is the mistake in my snippet which I was somehow afraid of.

    I really don't know how to refer to the constant NSForegroundColorAttributeName in AppleScript Studio application.

    Possibly, to use "NSForegroundColorAttributeName" (with double quotations) instead of bare NSForegroundColorAttributeName in the posted code might help?

    I'd recommend you to browse your project in XCode and look into (probably):

    AppKit.framework

    NSAttributedString Application Kit Additions

    Consntants

    Standard Attributes

    OR:

    Cocoa.framework

    idem. (?)

    and check what the correct way to refer to the constant NSForegroundColorAttributeName is, if possible.

    cf.

    StudioBuildingApps.pdf

    pp.44-46

    AppleScript Studio Components

    XCode Features For AppleScript Studio

    Default Project Contents

    AppleScript Application Template

    Ultimately, if you define your own Objective-C method in your project to make NSAttributedString object from given string and colour and invoke it via 'call method' command, the step to obtain coloured string (that is supposed to be done by mkAttributedString() handler in the posted code) would surely be done.

    However, it's not yet certain whether you can actually set attributed string value of a browser cell by the posted code...

    After all, it is such a guesswork. Sorry for that.

    Hope you may find solution,

    H

    Message was edited by: Hiroto

  7. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Well, I couldn't resist this last attempt...

    Thought you might want to try this.

    Sorry, NOT tested again, though I did my best to go by the book.

    (Please copy the code from this web page, not from subscribed email text.)

    (1) In your XCode project, define a class "MyAttributedString" (without quotes) as a subclass of NSAttributedString and have two definition files being saved as "MyAttributedString.h" and "MyAttributedString.m" with the following contents:

    // ===========================

    // MyAttributedString.h

    #import <Foundation/Foundation.h>

    #import <AppKit/AppKit.h>

    @interface MyAttributedString : NSAttributedString

    {

    }

    + (id)attributedStringWithString:(NSString *)str withColorRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a;

    @end

    // ===========================

    // MyAttributedString.m

    #import "MyAttributedString.h"

    @implementation MyAttributedString

    + (id)attributedStringWithString:(NSString *)str withColorRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a

    {

    id aColr = [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];

    id attrsDict = [NSDictionary dictionaryWithObject:aColr forKey:NSForegroundColorAttributeName];

    return [[[super alloc] initWithString:str attributes:attrsDict] autorelease];

    }

    @end

    (2) In your AppleScript code, use it as follows.

    --SNIPPET

    set str to "test string"

    set rgba to {1.0, 0.0, 0.0, 1.0} -- red (opacity 100%)

    set params to {} & str & rgba -- i.e. {string, r, g, b, a}

    set attrStr to call method "attributedStringWithString:withColorRed:green:blue:alpha:" of class "MyAttributedString" with parameters params

    call method "setAttributedStringValue:" of browserCell with parameter attrStr

    (* The variable 'browserCell' is assumed to be a reference to a browser cell object *)

    --END OF SNIPPET

    Good luck,

    Hiroto

  8. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    1) I've been unavoidably distracted from this, including resolving some issues with the application I've been working on. However, the application does work to change text and/or change some text-formatting characteristics (including color) of
    open TextEdit windows. It may be adaptable to other applications, one of things I'm pursuing -- and also may be adaptable to an Applescript Studio program that uses Applescript.

    2) First, I think you should pursue Hiroto's suggestions, as his responses are usually insightful and fundamentally correct -- and may mesh better with your Applescript Studio project.

    3) If those suggestions ultimately don't work out, then (since you're using applescript) I might be able to extract code from my application that deals solely with the color issue in your situation -- but I'm not certain, as I've done virtually nothing with AS Studio.

    Could you post a brief example of the AS code leading up to the "nested list of strings"? The code I have is likely able to color code those strings and save the results in a document file -- but whether that file can eventually be displayed in the format you want is at issue. All this may be futile, but I'd like to look at it a bit more.