Title property of a NSButton

I’ve written a basic script below in xcode and the interface is working. I have a connection to a NSButton called “btn1” and I can set the title of the button. What I can’t do is GET the title of the button. How would I go about that?


property NSButton : class "NSButton"

script DHAAppDelegate
    property parent : class "NSObject"
    property txtF1 : missing value
    property btn1 : missing value
    property lbl1 : missing value
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
    -- stop app process when last window closes - true quit
    on applicationShouldTerminateAfterLastWindowClosed_(sender)
        return true
    end applicationShouldTerminateAfterLastWindowClosed_
    on buttonTest_(sender)

        set txtF1's stringValue to "foobar"

        set lbl1's stringValue to txtF1's stringValue

        set btn1's Title to txtF1's stringValue
        -- set x to btn1's attributedTitle()
        if sender is btn1 then
            display dialog "Button 1 Pressed"
        end if
    end buttonTest_

end script

it should be:

btn1's setTitle_(txtF1's stringValue())

Its good to know there is an alternative way to SET the title. Thanks for showing me that. I need help to GET the title once I have set it.

it would be:

btn1's |title|()

get and set commands are for setting and getting AppleScript variables, not to change properties in Objective-C objects. Objective-C uses instance methods to get and set property values. The setTitle: and Title methods are also described in the Cocoa NSButton class documentation. So to be clear, it’s not an alternative :wink:

 display dialog btn1's |Title|() 

gets me the following error

title is not capped :wink:

I actually capitalized it on purpose. The lowercase didn’t work either. Same problem.

If you payed attention you would see two different errors :slight_smile:

you should use “as string” because the object you want to show in the display dialog can’t be of type NSString. With the “as string” at the end the NSString object will be coerced into an AppleScript string object.

display dialog btn1's |title|() as string

Thanks for all your help, but I still get the same error.

Post the error you get when you use title() (lowercase).

 display dialog btn1's |title|() as string

gets me :


That message shows an uppercase T, not a lowercase t.

Are you editing the script in an external editor?

yes, it does, but that doesn’t appear to be an error on my part. I have recompiled and rerun to produce the error.

I am editing in Xcode.

Regardless of whether I use

display dialog btn1's |title|() as string


display dialog btn1's |Title|() as string

I get:


where are you recompiling?

I am doing everything in Xcode if that is what you are asking.

Which version?

I suggest you close the document in Xcode, open the script in a script editor or text editor, makes sure the t is lowercase everywhere, save, and then reopen the project.

xcode version = 5.0.2

I found the problem and I have it working now. The question is why?


        set btn1's title to txtF1's stringValue
        display dialog btn1's |title|() as string

doesn’t work:

        set btn1's Title to txtF1's stringValue
        display dialog btn1's |title|() as string

The “set” line always works. The display dialog line doesn’t work when I use “Title” in the “set” line.



You can’t have a variable spelled Title in one place and title in another – they have to match, and that happens when the script is compiled.

You should also use the parentheses when you call a method: stringValue(), not stringValue. The latter will mostly work, but the results can sometimes be subtly different.

The set is like I said used for setting AppleScript variables. It’s working because AppleScript is going to use the bridge (AppleScriptObjC.framework) here and tries to execute setTitle: method instead of setting the variable title of script object appDelegate. Because you’re not addressing the method yourself, AppleScript(ObjC) is less strict on this part. However, I prefer to use setTitle: method and title method like I said in previous posts.

So why isn’t the second command working then? That’s because the code in Xcode 4 and above is using a plain AppleScript editor will compile when the application is built. Unlike xcode 3 and AppleScript-editor, that you’re not able to see the auto corrected code. It will all be fixed when you start compiling and running the application, but won’t be returned to your editor. The auto correct in AppleScript will use the first occurrence (case style) of the variable and then use it to correct the rest of the script. So when you have an variable (could also be a propery method or handler name) Title first then when you want to call the method title later, AppleScript have already convert the method title into Title for you even if you see in Xcode title.

It’s one of the drawbacks of using an plain text editor for AppleScript files. You could open the plain AppleScript in an external editor (opens in AppleScript-editor by default) and you’re able to see the auto corrected code.