Previous 1 2 Next 25 Replies Latest reply: Jul 22, 2014 9:29 AM by Frinton Fogey
Frinton Fogey Level 1 Level 1 (0 points)

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

  • Niel Level 10 Level 10 (271,650 points)

    It isn't possible to do this within the AppleScript Editor itself. If you'll be putting the text into another application, check that application's AppleScript dictionary.

     

    (109457)

  • Chris CA Level 9 Level 9 (77,475 points)

    What app is the document in that you trying to change the text color? (does the app allow different color text?)

  • Frinton Fogey Level 1 Level 1 (0 points)

    Sorry, as usual I didn't give all the details! I am using ApplescriptObjC in Xcode to write a project that displays all matching records that have been merged together from a Numbers spreadsheet. Hope that explains it better.

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    Are you displaying the result in an NSTextView? if so you can set the color of the text in that.

  • Frinton Fogey Level 1 Level 1 (0 points)

    Thanks for reply and suggestion of using NSTextView, have been trying to find out how to use this in an Applescript/Xcode environment but cannot work out how to make it work. I am using a table with an Array Controller to display the text, could you point me in the right direction please.

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    Not sure I follow, are you using a NSTAbleView?  In any case if you are displaying text  there should be ways to manipulate the color of the text and/or the background.

     

    Either poke around in the class documentation or else post an example elf how your are displaying the text.

  • Frinton Fogey Level 1 Level 1 (0 points)

    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

      property parent : 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

            tell application "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_

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    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

  • Frinton Fogey Level 1 Level 1 (0 points)

    Thanks anyway for looking.

  • Phil Stokes Level 2 Level 2 (360 points)

    You'll need to have your searchItems as NSAttributedStrings to do what you want to do (that's exactly what AppleScript uses to change uncompiled text into 'pretty text' (i.e, rich text)).

  • Frinton Fogey Level 1 Level 1 (0 points)

    Thanks for reply, still not sure how I would use NSAttributedStrings in my script, still very much a learner in ApplescriptObjC. Looked at documentation but cannot work out how to apply it, any more help gratefully received.

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    Poked around the web a bit and found this MacScripter / Change Color of text in NSTextField It 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

  • Phil Stokes Level 2 Level 2 (360 points)

    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.

  • red_menace Level 6 Level 6 (14,780 points)

    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

Previous 1 2 Next