Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Is it possible to set the colour of text with Applescript

I have script with searches text with a search argument and if found I would like to highlight that text by changing it's colour. Have tried the following (and other variants) but nothing works, any help greatly appreciated.


set searchItem to "04/07/2014 Mrs Brenda Bird 27 Somewhere Street Somewhere Town"


set the color of every word of text 23 thru 26 of searchItem to {0, 0, 65535}


error "Can’t set color of {\"Bird\"} to {0, 0, 65535}." number -10006 from color of {"Bird"}


Running OS X 10.9.4

Posted on Jul 15, 2014 9:32 AM

Reply
25 replies

Jul 17, 2014 8:05 AM in response to Frank Caggiano

Had a quick poke around in the documentation, still means nothing to me, have posted script, hope it makes sense. The first window accepts a search argument which is then used to select any line that finds a match and then displays all matching lines in the second window. What I would like to do is highlight the matched field on each displayed line.


script AppDelegate


propertyparent : class"NSObject"



-- IBOutlets

property firstWindow : missing value

property secondWindow : missing value

property theArrayController : missing value

property theData : {}

property searchArgument : missing value

property searchChars : missing value

property searchItems : {}

--

-- Use search to extract matching calls

--

on findCalls_(sender) -- input search string

set searchChars to searchArgument's stringValue() as text -- Coerce Cocoa text to Applescript text

tellapplication"Numbers"

tell table 1 of sheet 1 of document 1

repeat with i from 2 to row count -- search all rows for match

set searchItem to (value of cell 2 of row i) & " " & (value of cell 3 of row i) ¬

& " " & (value of cell 4 of row i) & " " & (value of cell 5 of row i) -- cocatenate all required cells

--

if searchItem contains searchChars then -- look for match search argument

set end of searchItems to searchItem -- save found lines

end if

--

end repeat

end tell

end tell

--

set my theData to searchItems

tell current application's NSArray to set theArray to arrayWithArray_(theData)

firstWindow's orderOut_(me)

secondWindow's makeKeyAndOrderFront_(me)

end findCalls_

Jul 17, 2014 8:46 AM in response to Frinton Fogey

Sorry I don't see an easy way to do what you are looking to do given the code you currently have.


By just looking to see if the search term is contained in the source string and then copying the whole thing it makes it difficult to change the attributes of specific text in the output.


Definite ways to do this but, at least as I see it, it would require a change in the way you are doing the search to begin with. Someone else might have other ideas.


regards

Jul 20, 2014 8:41 AM in response to Frinton Fogey

Poked around the web a bit and found this MacScripter / Change Color of text in NSTextFieldIt is possible that this does what you are looking for. You'll need to play around with it a bit and see if it fits.

As for using NSAttributedStrings while that would be possible, at least by my understanding of it, you would need to create a custom view and implement the drawing method yourself. Again that is how I read it, if Phil Stokes knows of a another way to use it I'd love to hear, always hate having to do thar low level stuff myself 😀

Jul 20, 2014 10:01 AM in response to Frank Caggiano

You'd need NSAttributedString for drawing text in an NSTextView, but if the display is using NSTextField there are two methods that can be called:


[NSTextFieldCell setTextColor: ]


and (probably the one the OP wants)


[NSTextFieldCell setBackgroundColor: ]


Unfortunately, these are Cocoa/Obj-C syntax, and I don't know the ASObjC translations (I once wrote an app in ASObjC, but shortly after switched to Cocoa/Obj-C and I've long since pretty much forgotten everything I knew about the former), which is why I didn't get any more descriptive in my earlier post.

Jul 20, 2014 10:16 AM in response to Frinton Fogey

You can use an attributed string with any text field, you just need to define the attribute to use and the range of the text you want to use it on. The new forum formatting bites even worse than before, so hopefully this survives Jive - the following example makes an attributable string from a regular AppleScript string by setting the words to one of the colors in an array:


# example - 'textField' would be a property connected to a text field

textField's setAttributedStringValue:rainbowize("red orange yellow green blue purple gray - colors start again")


# make an attributed string from an AppleScript string

# words of the string parameter are set to an array of colors

on rainbowize(someString)

set colorArray to {¬

current application's NSColor's redColor(), ¬

current application's NSColor's orangeColor(), ¬

current application's NSColor's yellowColor(), ¬

current application's NSColor's greenColor(), ¬

current application's NSColor's blueColor, ¬

current application's NSColor's purpleColor(), ¬

current application's NSColor's grayColor()}


set attributedString to current application's NSMutableAttributedString's alloc's initWithString:someString

set stringWords to words of someString -- just deal with the words

set wordColor to 1 -- current color index

set pointer to 1 -- current spot in the string to get word offsets


repeat with aWord in stringWords

set here to (offset of aWord in (text pointer thru -1 of someString)) - 1

set there to length of aWord

set pointer to pointer + here -- update to current word position

attributedString's addAttribute:(current application's NSForegroundColorAttributeName) value:(item wordColor of colorArray) range:{pointer - 1, there}

set pointer to pointer + (offset of " " in someString) -- update to get next word in the string

set wordColor to wordColor + 1

if wordColor > 7 then set wordColor to 1

end repeat


return attributedString

end rainbowize


EDIT: ack - it's even worse than I thought!




MacBook Pro / OS X Mavericks (10.9.3) / Xcode 5.1.1 / RubyMotion FTW

Jul 20, 2014 11:15 AM in response to Phil Stokes

While that would work if you wanted to make the whole textfield some color, I'm not sure how you would use that to color individual chars or words in the text field.


I suppose you could string together separate textfields and color the separate text fields but managing that will be a female dog.


As red_menance has show selectively coloring words in a text stream is not trivial, especially when the code hasn;t been designed with that in mind from the start.


To the OP another possibility for this might be to have an editor display the search results and to script the editor to highlight the search term(s). Not sure this is doable but it might be worth looking into.


regards

Is it possible to set the colour of text with Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.