Search code examples
applescriptapplescript-objc

How to change color text of Button with AppleScript?


Is there a way to change the color of a button's text when it is selected?

I tried something like:

property myButton : missing value

myButton's setTintColor:(current application's NSColor's cyanColor)

But I get the error below:

-[NSButton setTintColor:]: unrecognized selector sent to instance 0x7fc865f2d000 (error -10000)


Solution

  • To apply attributes such as color to button titles, you can use an NSAttributedString, for example:

        set buttonTitle to myButton's title -- get current title
        set attrString to current application's NSMutableAttributedString's alloc's initWithString:buttonTitle
        attrString's addAttribute:(current application's NSForegroundColorAttributeName) value:(current application's NSColor's cyanColor) range:{0, buttonTitle's |length|}
        set myButton's attributedTitle to attrString