« On Urinals and Bars | Main | more on being in charge »

Bindings! Bindings! Bindings!

When last we left our intrepid hero, he had gotten his application, sans plotting, into a basically functional state. Then he went to Shane Stanley's awesome AppleScriptObjC class, and learned about bindings.

Things got interesting.

So, leaving the hyperbole behind, Bindings are a different way to well, bind UI controls to the code in your application. They can make your life much easier, in that you can get the same task done with less work and, quite often, less code. But, they do require you to think about things differently. So, we'll cover bindings in multiple passes, with this article talking about text fields. Text fields are a good start because the WiFi Analyzer has a fair amount of them, and they're pretty simple to deal with, once you know a couple things.

So, as you recall, the non-bindings way we use text fields is pretty simple. We set up the field name in the application as a property with a value of "missing value":

property currentSSID : missing value

Then in Interface Builder, we create the text field, and do the ctrl-drag thing to connect the text field to the property. We can then use it in the application like so:

currentSSID's setStringValue_(theCurrentInterface's ssid())

This sets value of the text field's string value to the value of theCurrentInterFace's SSID. To get the contents of that string value:

currentSSID as text

Simple enough. So how do we do this with bindings? Well, the difference in the setup is minor. First, you set your property up to be an empty string, not missing value:

property currentSSID : ""

Then, in Interface Builder, you select the text field, and open up the Inspector. In that, you enable bindings, and pick your application delegate, (in this case, Wi Fi Analyzer App Delegate), and in the Model Key Path, you enter the property you're binding the control to. I highly recommend pasting this in, it avoids really annoying problems:

bindings panel

Save this change, and sweet, we're done, right? No dragging, no other changes!

Welllllll...no. That's what I initially thought, and nothing seemed to work. The text field wasn't set, writing the value failed all over the place, it was a mess. The reason why, once some folks on the core AppleScript team explained it is simple. When you use the non-bindings method, then you're working with a pointer to the data. So, things like setStringValue_() work and you have to specify "as text" when you're using it. With bindings, you're working with the actual data itself, not a pointer to the data, so you have to use more 'traditional' AppleScript methods:

set my currentSSID to theCurrentInterface's ssid()

and

currentSSID

Once you realize this it's pretty simple. Also, I realize that there's not a huge difference in code or convenience between bindings and non-bindings for text fields, at least not in the way we're using them. However, as we'll see in the next iteration of this, when we move to other controls, bindings can simplify our lives rather a lot.

Categories:     AppleScriptObjC
Posted by John C. Welch at 11:37 | Permalink



Comments

Warning for Notes users: The commenting system uses HTML.
I know this will be scary for some of you, especially Notes fans. However, open standards, rah-rah.
If you want to use less-than or greater-than signs, or other similar characters that HTML reserves,
you'll simply have to learn to do it the HTML way. Luckily, HTML is kind of popular, no matter what
your re-educators have told you, and you can easily find help on the intertubes.
digital.forest Where Internet solutions grow

There, a PayPal Button.

Bing
About the Author
How I do stuff on this site
Family
The Artwork of Melissa Findley
Diane Francis @ the National Post Eric Francis @ the Calgary Sun

BUY MY BOOK! BUY MY BOOK!
Non-DRM eBook PDF:
Get it direct from Peachpit!

Kindle Version:


Dead Tree Version:


Apple Amazon Links
Mac OS X Server 10.6 Snow Leopard

Mac OS X 10.6 Snow Leopard

Mac OS X 10.6 Snow Leopard Family Pack (5-User)

Amazon Book Links
Legacy of Ashes: The History of the CIA

The Donnas: Bitchin'

Wizards at War (The Young Wizards, Book 8)

The Demon's Sermon on the Martial Arts

The Collected Stories of Arthur C. Clarke

JavaScript and Ajax for the Web, Sixth Edition

Awakening Warrior: Revolution in the Ethics of Warfare

FOB Links

Mac Web Writers

Techie Links

Review Victims