Search code examples
appkitfoundationapplescript-objc

Working with NSWindow and NSTextview view using ApplescriptObjc


I have some ApplescriptObjc code that I have written thanks to red_menance and CJK. I have made some changes, making it into a handler and figuring out how to change fonts and font sizes in the NStextview, but I have run into trouble changing the color of the font and the color of the background via variables.

I am trying to pass a variable value, but it looks like the variable name is being taken as a literal. So how do I get it to accept the variable value? this is just a snippet of the code, if it helps I can post the entire script.

set theColor to "white"
set theBackgroundcolor to ""


        if theColor is "" then
            set its textColor to (current application's NSColor's whiteColor)
        else
            set theColor to (theColor & "Color") as string
            set its textColor to (current application's NSColor's theColor)
        end if
        if theBackgroundcolor is "" then
            set its backgroundColor to (current application's NSColor's grayColor)
        else
set theBackgroundcolor to (theBackgroundcolor & "Color") as string

set its backgroundColor to (current application's NSColor's theBackgroundcolor)

end if

The errors I get are: Can’t set backgroundColor of «class ocid» id «data optr00000000E0F1320080600000» to theBackgroundcolor of NSColor. and Can’t set textColor of «class ocid» id «data optr00000000E029320080600000» to theColor of NSColor.


Solution

  • You can use key value coding to access properties via string parameters, for example:

    set theColor to "red"
    set theColor to (theColor & "Color")
    set its textColor to (current application's NSColor's valueForKey:theColor)