Change Color of text in NSTextField

Morning all.

I’m new to xcode but not to applescript, so am prepared to wear the newbie hat of shame if need be :slight_smile:

Anyway, I’m struggling with what may be considered a simplistic task: Changing the colour of the text in an NSTextfield. I don’t want to change some of the text, but ALL of it…

I’ve tried combinations of the following, but am guessing that some of them may have come from AppleScript Studio examples:


set the fill color of my TFComPasswordStatus to "green"
set the color of the text contents of TFComPasswordStatus to {60000, 1, 1}
set text color of my TFComPasswordStatus to {65500, 65500, 65500}      

I’m stuck. When in the UI builder, under bindings, There is also text color. If I bind it to a property

property TFCurrentPasswordColor : missing value -- bound property

I get the most hideous of errors: 2012-12-10 09:42:09.450 VerifyAndChangePassword[5160:303] Cannot create NSColor from object <AppDelegate @0x104324210: OSAID(4)> of class AppDelegate

Lastly, this is the top of my current code…


script AppDelegate
    property parent : class "NSObject"
    property TFCurrentPassword : "" -- bound property
    property TFCurrentPasswordColor : missing value -- bound property

I was under the impression that NScolor was inherited from NSObject, and, thus, was not needed to be included…

Help!

AppleScript: 4.5.2
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Hi,

the binding expects a NSColor object, so you have to create one.
In awakeFromNib or applicationWillFinishLaunching assign an initial value


set my TFCurrentPasswordColor to current application's NSColor's blackColor()

or a color with custom values


set my TFCurrentPasswordColor to current application's NSColor's colorWithDeviceRed_green_blue_alpha_(.8, .2, .9, 1.0)

Consider that in Cocoa the color values are in the range from 0.0 to 1.0 (a floating point number)

Stefan,

Many thanks for your swift reply.

As I guessed, it would be simple.

Kind regards,

Paul

You need to create the color first:

set theColor to current application's NSColor's greenColor()

or:

set theColor to current application's NSColor's colorWithDeviceRed_green_blue_alpha_(0.0, 1.0, 0.0, 1.0) 

Then apply it:

theTextField's setTextColor_(theColor)

This works great. However, I’m now having a related issue…

Double clicking the app to start works fine.

From within an AppleScript,

do shell script "open -a /Applications/myapp.app"

works fine

However,

tell application "System Events"
		tell application "VerifyAndChangePassword" to launch
end tell

or just

tell application "VerifyAndChangePassword" to launch

will result in the following error:

Any Ideas? The top of my script has the following:

script AppDelegate
    property parent : class "NSObject"
    property TFCurrentPasswordColor : "" -- bound to current password box

and my applicationWillFinishLaunching is

on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened
        set my TFCurrentPasswordColor to current application's NSColor's blackColor()
end applicationWillFinishLaunching_

as in your first post set the color property’s default value to missing value


property TFCurrentPasswordColor : missing value -- bound property

Stefan,

Many thanks for your reply…

With regards to your suggestion: It already is:

script AppDelegate
    property parent : class "NSObject"
    property TFCurrentPassword : "" -- bound to current password box
    property TFCurrentPasswordColor : missing value -- bound property

In the latest post (#5) it was not.

bound properties should be set in awakeFromNib rather than applicationWillFinishLaunching

Try also


activate application "VerifyAndChangePassword"

instead of launch

Stefan,

Many thanks for your help. All working now.

When doing a

tell application "foo" to activate 

because of having run it from Xcode, it was trying to launch the version stuck in a temporary folder somewhere.

renaming the project and rebuild and it worked!

Again, many thanks for your help

Paul