applescript-objc

ApplescriptObjC: text view not updating from bound property


I'm having trouble updating a text view in my AppleScript Xcode (12.5.1) project.

I have:

However, when I update the "msg" property the change is not displayed in my text view.

Although my msg property has been updated with the "initializing..." string (as evidenced by the alert message) my text view is not updating.

bindings inspector

connections inspector here

App running with "msg" property displayed in text view (indicating successful binding)

inside appInit function

myAppDelegate code:

    property parent : class "NSObject"
    property msg : "zig" -- the message
    
    -- IBOutlets
    property theWindow : missing value
    
    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened

        activate
        display alert msg -- "zig" property successfully displayed in text view. Binding is apparently set.

        set msg to ("initializing…" as string) -- text view not updated.
        
        appInit()

    end applicationWillFinishLaunching_
    
    on appInit()
        
        activate
    
        display alert msg -- "initializing"; msg property updated, just not being updated in text view...

    end appInit```


Solution

  • Bindings to properties use key-value observing (KVO), which is a mechanism that allows objects to be notified of changes to properties of other objects. AppleScriptObjC properties are set up for this when bound via Xcode, but in order to use this mechanism when changing the property in your script, you need to use a setter to trigger the notification - otherwise the property itself will be changed, but the object will not be notified of the change.

    Examples would be:

    set my msg to "initializing…" -- using the `my` keyword 
    setMsg_("initializing…") -- also my setMsg:("initializing…") -- using the built-in setter method
    

    Note that the property will be a Cocoa value, so it will need to be coerced to a string when using it (later in the appInit dialog, for example).