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?
Thanks.
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
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
If you payed attention you would see two different errors
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.
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.
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.