WWDC2004 Session 430
Transcript
Kind: captions
Language: en
good afternoon my name is Mark Luttrell
engineer in the cocoa framework screw
Tiger is going to include a lot of new
Koko scripting features and the focus of
all these is to make it as easy as
possible for you to implement great
script ability the prerequisites for the
stock if you've already done a lot of
basic cocoa programming you'll find out
how to add script ability to your
existing applications if you've been
working with cocoa scripting already
you'll find out what's new and tiger
there's a bunch and if so far you've
been programming in carbon land but you
know a bunch of out script ability this
will be a good opportunity for you to
get a sort of a feel for what how Co Co
differs today I'll be specifically
talking about the new SF file format
that cocoa supports for you to declare
your script ability I'll go into a good
bit of detail about how scripting uses
key value coding a fundamental mechanism
and foundation that's being reused for a
lot of things now in cocoa I'll talk
about error handling because it's so
important I'll go into some detail about
script ability and apps that use cocoa
bindings and I'll leave you with some
debugging tins so today sample app if
you're on the cocoa dev mailing lists
you know that Scott anguish and Malcolm
Crawford have been doing a tremendous
job of answering questions about cocoa
bindings and Malcolm has actually gone
to the trouble of creating a pretty nice
little sample of how to do hook up a
custom view to bindings so on Isaac
casting about for what should i make
scriptable this seemed like a pretty
obvious choice because one of the things
I want to show you is how well binding
some scripting interact now the first
thing you do when you add script ability
to an application is that you have to
declare the script ability so that
applications like script editor that
will drive the scripting can get the
scripting dictionary can show it to the
user can compile strips and compiled
scripts and things like that and then
tiger we're adding a new mechanism to do
that new file format actually called the
SF file format
so what is there to declare for an
application script ability well as you
can see in the screenshot which is from
script editor there's scriptable classes
and their scriptable commands this is
the scripting dictionary for the
graphics bindings that i'll be showing
you and it has a document class it's
very simple it just has circles the
circles you saw on the screen there and
there is a circle class also with a
variety of properties associated with it
like the position and the color and the
radius of the circle and some shadow
aspects so there are three main
categories of things you declare in
script ability the classes that the
scripture sees the commands that the
descriptor you know the person who's
writing the script can use and then
types that are used by class properties
and command prep parameters and we have
a pretty big variety of types available
now it expanded a lot actually and tiger
but just some of the more common ones
will be using our enumerations and
records which are new and support for
some custom types too so the old way in
panther and earlier for declaring your
application script ability it was done
by providing a pair of files in your
application a script sweet file and a
script terminology file the scripts we
file with fold implementation details
and the script terminology file is full
of human readable strings to show to the
user the always these always lived in
your applications Resources directory
and there are kind of hard to work with
mostly because they're split across two
different files that you always had to
keep in sync it was a feature had
something to with internationalization
it's not being really really being used
that much and actually there are other
mechanisms in cocoa for getting roughly
the same thing so but when I say that
was hard to work with don't let me talk
you out of you know trying script
ability right now they're not that hard
to work with in in you know you don't
have to wait for Tiger to get a good
head start on adding script ability to
your application and also some of the
things we're doing while adding support
for this new scripting declaration file
format are kind of carrying over into
the script Street world so you know
warnings about things that you do wrong
and stuff like
are also being applied to the old file
formats just serendipitously so working
with them is also getting easier while
we're doing this and of course if you an
script ability to your application now
because it's such a valuable feature you
know users want to do workflow things
like that you know of course we won't
break compatibility for your application
and tiger even though we are doing some
pretty big changes so that was the old
way of doing it I won't go into too much
detail about that I actually went into
quite a bit of detail about it like two
years ago the new way which I will
discuss in detail is s def files the FF
file format itself is not new is
introduced way back in Mac OS 10.2 in
the form of some documentation and a
command-line tool called sgp that was
pretty handy you could start with an s
Steph file and it would parse that an
output carbon AE te resources as well as
Coco script sweet and script terminology
file so even though Coco didn't have
built-in support for this more
convenient file format more powerful
file format it was still useful for you
to build your script ability and I think
some applications like at Apple like
mail for instance actually they write
all their script ability support in SF
and then they use sgp during a build
phase to output the scription script
terminology files that Coco needs at
runtime but that won't be necessary
anymore what's new and Coco is that it
parses use SF files natively the still
in general will probably live in your
applications Resources directory but
Coco is not going to just load them
automatically every a Stephanie
Resources directory like it did with
scripting script terminology file
because that was pretty inconvenient in
the past scripts sweet declarations
would get loaded into every application
whether they were applicable or not and
it wasn't exactly the perfect way to do
things so now what we're going to let
you do is you specifically specify in an
entry in your applications info.plist
what the top-level scripting definition
file is in our example it's going to be
graphics findings SF one thing that
hasn't changed is a still need an entry
in your info.plist NS applescript
enabled equals yes
turn on scripting in the first place so
the access file format is xml-based
nonetheless it is easier to edit than
the old scripts we can script
terminology formats were a lot of that
use comes from the fact that it's just
one file now programmatic information
and human readable information are all
in the same file it lends itself well to
attractive informative scripting
dictionaries it's it was really designed
to represent easily the things that
users need seeds be able to write
scripts easily and if you want to get
every last bit of detail about this file
format just go to terminal and type man
s Steph there's a pretty large manual
page that declares everything in big
detail so the first category of things
that you declare in SF files or class
declarations and you're declaring your
application scripting classes which very
often have a very tight coupling with
your implementation classes you know the
ones that came about just as the result
of your design your object model but not
always sometimes there are places where
you'll have classes that are just for
scripting we need to clear classes you
typically declare the scriptable
properties of those classes and
something that's a little rarer you
declare the commands that instances
those classes can handle and I say it's
rare because the the Apple script
standard suite includes a pretty
complete set of fundamental operations
so and customizing that is very often
not necessary the scripting class
declaration has a human readable name
and description a four character code
that identifies it and if you're hoping
that I'd be saying there are no more for
character codes for people have worked
with this in the past I won't be they're
still there but we know that people hate
to work with them so we make make some
progress in that in the future so also
has implementation details like the name
of the objective c or java class and
property declarations and just a
terminology mismatch for historical
reasons we call properties and
applescript are usually called
attributes and two and relationships by
coco programmers and element class
declarations and apple script are
usually called too many relationships by
coco programmers
so here's here zooming in on that on the
circle coil of the document and the
circle class that graphics binding app
so you can see the document class is
just one element class circle so the
elements of one of these documents are
circles and there is a circle class with
the properties i mentioned earlier
here's what you do to make that sort of
thing appear in your scripting
dictionary this is part of the deck Lord
this is most of the declaration for the
circle class has a name and a human
readable description and a four
character code when I talk about four
character codes by the way I'll be
pointing you later to some documentation
the new scripting interface guidelines
it give advice about how to pick those
implementation details like the cocoa
class we have an objective c class in
our project also called circle whenever
in a nest f file you see a cocoa element
with an attribute like class after it
that's always an implementation detail
these esta files are being are made to
be read by a number of different clients
like script editor will be reading them
as well as cocoa reading them for its
own purposes so all the implementation
details are are kept separate that way
properties also have names and
descriptions and for character codes but
they also something else they have a
type so in our simple example here the
types of the exposition of the wide
positions are real you know floats and
the type of the color is a color that's
built into our system called color an
implementation detail that you get to
provide when you're declaring the class
has property properties is the key for
the property and I'm going to be talking
about key value coding key value
observing a lot today these these keys
are the strings that identify the
properties for use by these fundamental
systems and foundation cocoa command
declarations which I won't go into in
too much detail because declaring custom
commands hopefully very rare look very
similar in SF same sort of shape cocoa
aren't includes declarations of the
commands in the standard suite Get Set
move duplicate open print clothes quit
save things like that
a new feature in SF something that
people have been asking for is the
ability to in rare situations replace
our command declarations with customized
ones that have more parameters things
like that so you're going to be able to
do that now with this new file format
we've adopted and you can as always add
your own custom commands so a command
declaration has human readable name
description 24 character codes instead
of just one and then an implementation
detail like the name of the objective c
or java class that encapsulate instances
of those commands that come over as a
result of running the script and
commands have direct parameters and they
also have other kinds of parameters
these are mentioned pretty much in the
apple script language guide but we do
let you declare them with great fidelity
so you're able to do things that match
exactly what people who've read the
apple script language guide would expect
and if a command has a return code you
get to declare the type of that too just
a quick example the send command from
males script ability declared as an f
stuff is very simple as a name of
description parent for character codes
and this one has a direct parameter that
represents the receiver of the command
in this case the type of the receiver
will be a message message is another
class it's also declared in mail script
ability so basic types that you get to
use the set is a is pretty clean the
names that you use in the SF are pretty
much the human readable names that users
see in scripting dictionaries when
they're opening up and script editor or
something like that so we provide the
set that people are expecting things
like boolean color date file integer
real so on each of these maps to a Coco
class so that your script ability code
will get an instance of the Coco class
and ever one of those fundamental types
appears in the script so bullion's map
to NS numbers integers also mapped to NS
numbers colors map to NS colors and so
on
another type that you'll see in SF is
the specifier type corresponds to an
applescript reference what's called an
apple script language guide a reference
and in cocoa the class that represents
this is ana Script object specifier and
a specifier identifies one or more
scriptable objects like words four
through eight of text of you know a text
at a document or something like that and
here's the example use of that just the
clothes command has a direct parameter
that represents the receivers of the
clothes command and the type is
specifier which means that you send a
clothe command to a set of objects that
are specified like for example you know
documents 1 through 3 or something like
that another type that you'll see in FF
is a location specifier which is a lot
like a specifier but not quite it
corresponds to an apple script language
guide insertion point and in cocoa the
classes used to represent this is an ass
traditional specifier and it identifies
an object location which will very often
be the same as the location of an actual
object sometimes it'll be the position
before or after a specified object and
here's an example use the standard
sweets move command has a two parameter
very popular to name parameters with
prepositions so in this case this very
fundamental command the two parameter
its type of location specifier so you're
specifying the location to which things
should be moved
some more types that you get to use in
SF another one is enumerations if you've
been programming in C this is a very old
news it's the same concept an apple
script there there named values here's
an example it's the declaration for the
printing error handling enumeration
which is used by the new enhanced print
apple event about which a tech note
appeared on our web about a year ago so
when you when you send a print command
to an application that supports
everything properly you get to specify
what kind of post script error handling
gets done detailed or or standard so and
that's specified just with an
enumeration so scripters don't have to
type in integers or things like that of
ancient applescript concept is the list
type but Coco hasn't really supported it
properly until now in tiger it will you
can preface just about any type name
with list of and that indicates that it
should be one or more of those things
your implementation in this case will
always be passed in this example for
list of file an NS array of NS URLs so
even when the script specifies for
example just one file we want to make it
very easy for your implementation code
not to have to to check multiple types
you know is it a Baroness URL is in an
NS array of URLs if you declared list of
file you'll always get an NS array of NS
URLs a new feature an F staff that we
think will turn out to be pretty useful
as complex types and i don't mean has a
real and imaginary part i mean that the
type is is one of a variety of different
types alternative types so this is this
will be useful in situations like
specifying the address book label in
address book scripting for a phone
number or an email message you can say
home or work or something like that you
can just do this with a string if you
want but there's a little bit more
checking that can go on it compile time
if you support
either a string for custom labels or one
of an enumeration for predefined labels
so and this not only lets scriptures
write what they want to write it lets
them know that they can write it because
information about this actually appears
in the scripting dictionary so scripting
dictionaries and esta are going to be
much richer filled with a lot more hints
descriptors about what they can actually
do what's worth trying and here's the
sample from the standard enhanced print
apple event the type of the direct
parameter the receiver of this command
is list of file or specifier which means
you can either print a list of files or
one file that is being picked picked
with the standard file chooser dialog or
something like that or a specifier for
example a list of documents that are
already open you can print those two
so something that s Steph has that the
older mechanisms didn't have is a way to
take real control over the reuse of
cocos standard declarations as I
mentioned cocoa doesn't automatically
just read all its own files anymore it
makes you import them so you'll have
much better control this is done with an
import tag and here's a sample of what
it will look like import name equals
foundation standard commands esta will
be bundling up all of our standard
command declarations in a nest f and you
can import them if you want to save
space and your own nest step and also to
take advantage of when we add new
standard command should that ever happen
or if you're doing heavy customization
you can just you know copy and paste
that all into your own half step and
make sure that you have complete control
over what goes on import can specify
more than just s def files that are in
one of the frameworks that the
application is linked against it can
specify esta files that are in other
bundles for instance or even anywhere at
all there's their forms of it the
specify for instance even absolute path
so this will be useful for example you
are going to be given the opportunity to
build your top level s step on the fly
if for example you have an application
that's heavily plug-in based and the
script abilities spread among many
different plugins potentially you're
going to be able to do that pretty
easily the mechanism how this appears in
cocoa is a little little undefined right
now there's probably going to be an
application delegate or something like
that but the the info.plist declarations
are already defined to support this
dynamic s def concept and and you'll
definitely be able to do that something
else that cocoa has been lacking but we
now have because we've adopted a stuff
is called the class extension tag and if
you've been working with Objective C and
you know all about categories it's
pretty much the same thing so actually
you can do a few of things that you
wouldn't expect to be able to do with
categories
so we're putting this here for one
reason it's going to be it's going to
make putting script ability and bundles
eat it easier so if you have a bundle or
a plug-in that adds properties to the
top level application class this is how
you'll do it you'll extend the
application class in the past people
have had to do strange things like
subclassing the application class even
when there wasn't really much reason to
do that except we didn't provide a
better way so now we are providing a
better way other features if you've been
working with cocoa scripting and you've
seen the scripting dictionaries that
result where things are a little bit
randomized that's because the script
sweet format is property list based and
what's more is using property lists
dictionaries which of course have no
inherent order so things get get moved
around a little bit and you can't really
control how things are presented to the
user that the writer of the scripts s
steph has no such problem a big feature
of a staff is that it's because it's
going to be a file format that's shared
outside of just cocoa there's actually
API in the OS a framework for reading
these they're going to be recognized
natively by script editor and presumably
hopefully other scripting tools too and
the result of that is that when script
editor or some similar application needs
to get at the scripting dictionary so
that it can compile a script or just
show it to the user they're not going to
have to launch your application to do it
it just has to look in your info.plist
and yes very old news in the carbon
world and Coco is finally catching up in
this regard so yeah there won't be any
more application watching script editor
we'll just dig around in the info.plist
and and you know we'll be able to to you
know follow the imports and things like
that one caveat for that with that by
the way is that if you're doing the
dynamic SF feature well just as in you
know old carbon tradition well then your
application will still have to be
launched so that you can actually build
it right then
other features SF includes help for
letting you provide backwards script
compatibility when you're rearranging
your scripting and seems that you know
nobody ever gets it right on the first
try you want to remove things you want
to rename things but you don't want to
break you know people's existing scripts
gratuitously that's kind of rude so
estephe has a couple features like the
hidden tag which means if you have you
know an element class or a command or
something like that that you're
deprecating is not you know you'll want
it on anymore it's just crossed you know
you can leave that in there so that
existing scripts keep running but new
users who open up your scripting
dictionary and look at it won't ever
know that those ever existed there
hidden so and the synonym tag fixes
similar problems when you're renaming
things or you know getting the fourth
character codes just write or something
like that one thing that's been a source
of big confusion in cocoa is how we
handle the scription of files and with s
def we're really going to be cleaning it
up there is a single type it's a public
type it's called file and it's public
unlike the old nsstring file path that
some of you are using in your scripts
weed files this one is genuinely public
and of course you can use list of file
and when you do your code will always
get an NS array of URLs and scriptures
you know the magic of Apple event
coercion gets use files aliases HFS pass
POSIX pass whatever they're most
comfortable with and your code will
never be able to tell the difference it
always just gets URL
so implementing your application script
ability you've declared it to the
outside world and now you actually want
to hook it up and make it work and this
requires you to know all about key value
coding and this isn't a change in Tiger
cocoa scripting has always been built on
top of key value coding now what that
means you have to make your scriptable
classes key value coding compliant and
just to do the same old terminology
review properties or attributes and
element classes are too many
relationships what does key value coding
do well first of all it's worth
mentioning that's the same mechanism
that cocoa bindings and now a core data
uses it gives access to object
properties using a very generic API that
identifies them by key and keys in this
case or just strings like in today's
example of the graphics bindings
application the circles of a graphics
binding document or the color or the
radius of one of those circles so the
same cheese that appear in SF files and
are actually also used in i beez
bindings inspector are used throughout
this API so it'll be kb c compliant the
short turfs answer is you have to
implement enough methods or whatever to
make these two methods in nsobject work
value for key and set value for key so
the quick and easy way to do that is for
each one of the keys implement a method
that has the exact same name as the key
so that when Coco scripting calls are
invoked value for key value for key is
smart enough to root around and your
classes method list and find that key
method and invoke it if the property is
mutable is read right then you also
implement a set key method and these are
just as simple looking as they sound for
our radius property where the KBC key is
radius there's method name radius and
there's a method names named set radius
creech to many relationship for each
collection of pointers to other objects
that you're scriptable has you have a
choice of how you implement that
typically you would again implement a
method named key that would return an NS
array a collection of the related
objects but sometimes for implementation
reasons complexities arise and you don't
have the pointers to the other objects
stored in an array if that's the case
and instead of implementing key just
implement count of key and object in key
at index and again key is replaced by
the actual key that we're talking about
and key value coding is smart enough it
will find those methods and use them
when it needs to so how do you decide
between the two of them just whichever
is easiest to implement is the general
rule if you change your mind later
perhaps as a result of measuring and you
know doing performance analysis it
really it makes no difference there's
nothing outside of key value coding
that's really going to see this unless
of course you declare these methods in a
header and show them to other
programmers on your project for each
mutable to many relationship where
objects can be inserted or removed you
know via scripting via cocoa bindings
via core data you would also implement
insert and remove methods whose names
fall the patterns you see here so a
quick sample the graphics binding
applications document class as a circles
relationship all the accessories that
key value coding needs are right here so
circles return as a pointer returns an
I've our insert object and circles that
index remove objects from circles that
index just invoke the regular NS mutable
array methods so that was just the tip
of the iceberg as far as what key value
coding lets you do you know writing
writing having you write accessor
methods is the simple way to introduce
it and is what people are most
comfortable with right off the bat
key-value coating has a couple other
different ways to grub in your objects
it even has direct Ivar access
so you don't even have to write any
accessor methods at all you just have to
give your I've are the same name as the
key and key value coatings value for key
and set value for key methods will poke
in it poke at it directly this is kind
of nice because of course it's lets less
code and it's also kind of nice because
if you do have to add code you actually
have to do something when somebody sets
the value you have to you know make
something else consistent or something
like that you can add the fed accessor
method and key value coating will simply
start using it it'll stop drubbing at
the I've are directly and use your set
accessor method so it's we have
different layers of functionality for
you to use depending on how much
customization you need to do how much
code you're willing to write you know it
goes all the way from virtually no code
to not that much code so so see the
documentation where this is all now
described in great detail for even
greater details see the turgid pros and
the NFC value coding dot H header file
which explains the default
implementations of methods like value
for key and set value for key and just
excruciating detail so great error
handling is worth singling out because
it's something that cocoa scripting
hasn't done so well in the past and it
is tremendously important error handling
is important because it helps scriptures
learn the language you know they learn
by doing that the scripting language
they learn by experimenting making
mistakes they need feedback about what
their mistake was so I'm as a nice side
benefit when you're testing your script
ability good error handling you know
helps you debug too we've made big
improvements to our hair and error
handling if you've our scripted akoukou
app and gotten it wrong or works with
things like apple scripts to do and
gotten it wrong you've seen these
incredibly unfriendly error messages
like NS cannot create command script
error entire messages like those will be
just completely disappearing we're just
going to do
we're just going to be doing the normal
apple event error messages that we
should have been doing all on so let me
show you what I mean so this is the
graphics binding project that I was just
telling you about let me just point you
at the eff stuff real quick here are
some import directives in the shipping
version of tiger these will actually be
a little more complicated because I'll
actually point to things in cocoa but
right now I just copy these standard s
steps into the project to demonstrate it
here so some imports of the standard
types and item and commands and classes
that are built into cocoa here's that
circle class I was telling you about
very simple implementation details are
tagged with the coco key element and
this is interesting down here you see
these have no coco key and it's because
there's a default value for those in
this case it's just color or radius or
shadow angle so it tries to save you a
little bit of typing but if you want to
be explicit that's okay too
so the class the actual source code is
pretty simple here is the the accessors
for the radius methods and really these
aren't even actually necessary either
but if you get things get a little
complicated you do have to start writing
accessor methods this is what they look
like
so let's launch graphics bindings so I
can show it to you here it is it's
pretty neat it's like super dot view
something like that so you can do things
like select different dots and this is
all done with cocoa bindings with very
little code there's a good bit of a good
bit of code in the actual view the
graphics view that you're seeing here
but that's meant to be highly reusable
the rest of it is is nearly nothing so
you can move things around you can
select a bunch of them there's this neat
little joystick control for moving the
shadows around of course the the
controls take effect on you know the
entire selection at the time so to
script that a little bit and here we are
so in an even simpler document
that does not actually work quite yet so
and plus this was already compiled
anyway so but its standard apple script
you know you get to do things like
setting the radius and apple event
coercion xand things like that work so
you get to set the radius to an actual
text string and if you tether test set
it to something that's complete nonsense
finally it actually says so hopefully
you can all read it it says cannot make
gobbledygook into type real which is
what it should have said all along and
we'll be doing more than just type
errors checking for indexes and things
like that graphics binding cannot get an
error can't get documents seven invalid
index because there's only one of them
open
that's it it's not supposed to be
anything earth-shattering so it just
works now so the kind of things that
cocoa is going to be check be able to
check for you are when the scripture
uses the wrong types nonsense object
specifiers you know document for of word
aight might not really be what they
intended to type we're going to catch
stuff like that things like out of range
index specifiers we're going to be able
to catch pretty much everything except
invalid property values pretty much so
when you want to clamp the values of
numbers or something like that cocoa
can't catch that for you everything else
it will what you're going to be doing
and this is not implemented in the seed
right here is use the standard key value
coding compliant or the standard key
value coding mechanism there's a method
on nsobject called validate value for
key error and what you'll be doing is
just implementing the method whose name
is validate key error to return an error
so validate radius colon ericone so and
what's really great about this is it's
tremendous code reviews because the
exact same mechanism that cocoa bindings
uses in more complicated situations if
you end up implementing your own custom
commands you should know about these
methods I think these are all added in
Panther actually an S script command
current command they weren't all added
current command misamis will get you
they can be the script command that's
being executed right now so if your
accessor method is being called and you
know you want to set an error number and
enter something like that this is how
you find out number one whether or not
your code is being called because of
scripting it's all meant to be highly
reusable like being being called because
of cocoa bindings or core data but if
it's being called because of scripting
there will be a current command and one
of the things you can do if something
goes wrong is to send that current
command set script error number now you
shouldn't just you know pick a number
out of the ether
for you you know or you may end up have
to having to but before you do that have
a look at the Apple script language
guide we're a bunch of error codes the
people who write scripts are expecting
to see are documented so and they're all
declared as constants in carbon core mac
types H which is typically import by
with the Umbrella headers so core
services core services done H there's
another method on ms script command
called set script error string don't use
this gratuitous ly like we were that's
how we ended up with things like NS
cannot create command script error if
you're using one of the numbers that
Apple script recognizes and it
recognizes a bunch don't set any air
string just set the number an apple
script will pick the right string for
you and when talking about error
handling it is worth focusing on the
specific brand of error handling type
checking because in addition to all the
other things where you want to help
scriptures experiment there's a big
reliability issue here that you might
not have realized I'll implementing your
own script ability if you haven't if
you've been doing so is that it's
possible for scriptures to crash your
app by arranging for objects of just the
wrong time to be passed to your code so
we're going to be closing that up
because FF has a very good support for
declaring exactly the types that your
implementation can handle cocos guard is
going to make sure that your
implementation is only past objects of
the exact right type too so and as a
result of the good type declaration the
user is going to get good hints about
what their scripts can do so sometimes
in in when in programming systems when
there's a lot of type checking that can
you know impinge flexibility but in this
case we don't think it will we think
there'll be more flexibility now because
Apple events upon which all the
scripting stuff is built has always had
a pretty good app levent coercion scheme
for coercing things of one type to
another and when cocoa has good type
information it's going to it uses that
proper
so the user will be able to you know do
things that are close enough typing in
text in you know taking in the number
and textual form instead of actually
getting the number right script ability
and cocoa bindings cocoa bindings is
hopefully all know by now is a big new
technology that was introduced last
year's WWDC for creating user interfaces
with not a lot of glue code we are
making improvements to how nicely script
ability interacts with that so both the
script ability and cocoa beans and now
core data to use key value coding very
heavily so when you make your classes
key value coding compliant you're
killing two or three birds with one
stone we also introduced last year
something that goes on top of key value
coating called key value observing now
script ability is not interested in key
value observing there's no way to write
a script that you know observes objects
in another application though that would
be very interesting it just sends I key
value coding but the same classes are
very often implemented or are very often
reused between script ability and
bindings so for bindings you have to be
Keeley observing compliant and we've
we've made some improvements to how
scripting uses KBC to make that easier
for you so Coco scripting now uses set
value for key which is a new method that
was introduced last year this is because
this makes it easier for you to use the
automatic kvo notification feature
you'll take value for key method which
scripting was using is now deprecated if
you have existing code and you're
overriding it for for some reason will
still invoke it in that case because we
don't want to break your application but
we encourage you to move to the newer
method because it works properly with
kbl something else that was added to key
value coding as part of the Coco
bindings feature is a new method called
mutable array value for key when I was
talking about those insert and remove
methods that you have to implement on
your too many relationships
this is the method that has the smarts
to go find those and again it takes
advantage of this automatic kvo
notification feature where key value
observers are notified merely because
you know a set accessor or an insertion
method to remove methodism is a invoked
and just as in the simpler case old
methods like insert valued index and
property with key will continue to be
invoked in Tiger for well for the
foreseeable future for backwards
compatibility reasons so automatic kvo
means because you have to be key value
coding compliant anyway for all your
properties that you're making scriptable
means that you're probably already KVL
compliant for those same properties so
you can do things like binding to them
from the user interface so for example
how this works when your scripting
object its end set value for key because
the user descriptor ran a script like
set radius of circle one a front
document something like that set value
for key will call and find your set key
method but then it also goes ahead and
does and notifies observers like bound
UI objects of that change too so the UI
will update automatically without you
having to write anything into your
accessory for too many relationships
mutable array value for key is used by
Coco scripting now how that works is
Coco scripting calls mutable array value
for key and what's returned is actually
collection proxy that represents the
relationship that your class has to
those other objects like an NS graphics
bindings documents relationship to
circles so it's returned looks just like
an NS mutable array to the client Coco
scripting themself in this case and as
NS mutable array API is used to insert
and remove or replace objects the
container objects insert object and key
to index and remove methods are invoked
automatically so the the client of Qi
decoding has a tremendous amount of
flexibility about what they can do
anything that an NS mutable array can do
including things add in categories but
the the owner of that relationship only
has to implement a pair of methods and
as all this happens bound UI objects get
updated automatically I've been talking
about automatic kvo notion notification
so i guess i should mention manual kto
notification just so you know what why
we bother to call it automatic at all
because there's a there's another system
too and they coexist very well you can
have kbo automatic kbo notification
turned on for class and then also use
manual kvo notification for more
complicated situations shouldn't be
needed that often for scriptable
properties but you can use it for
example when your your class is changing
its own ivars directly outside of a que
vc compliance method like set key and
it's pretty easy to use you just do it
will change value for key before the
change and it did change value for key
after the change so I'd like to leave
you some with some hints about debugging
your application script ability there's
a lot of logging available that cocoa
scripting will do to help you figure out
what's going wrong and also every time
we touch this stuff we seem to add or
clean up a description method in any of
the cocoa scripting classes so logging
cocoa as we wrote in the support for a
staff we made sure that all the errors
and warnings that it spits out are
useful so I'll demonstrate that in a
little bit so mistakes that you make are
easy to pin down instead of things just
going mysteriously wrong and we also
added this support for warnings and
errors improvements serendipitously to
the old file formats to scripts we in
the script terminology so when you see
these fix the warnings they all actually
mean something
and if it's not descriptive enough you
know send an email to one of the public
lists or two you know Coco feedback or
something like that you can have Coco
log information about every scriptable
command as it happens just with a simple
default defaults write and its global
domain and a scripting debug log level
one and if you only want to turn on
logging for one application substitute
and its global domain with the bundle
identifier for that application I
mentioned that we added a bunch of
description methods to classes one of
them is NS script sweet registry and
then all the classes like in this class
description and a script command
description things like that so if you
want to see what Coco pulled out of your
f Steph file or your old scripts read
file just break the application anywhere
and type what you see here p 0 NS
classroom string in the scripts
eregistry shared scripts free registry
will send a description message to the
scripts free registry and just an
enormous amount of information will be
presented to you so you can see exactly
what Coco thinks you said about your
application script ability there's some
other description methods to and a
script command has one I don't know if
if everybody's found it yet if you're in
the middle of one of your script ability
accessories and you're wondering why was
this called you know you're watching
script editor send events but it's still
not obvious then you can print a
description of the current command so
and if you are an old Apple events hand
and you want to see what Apple event
resulted in the creation of that NS
script command you can get the apple
event from the current command and print
its description too so demo
so let's very quickly just do something
stupid in our F def file
[Music]
and I just you know fat fingered one of
the type names here and cause the
dictionary to be loaded and it now gives
you a super informative message that
will help people debug SF warning for
attribute display name of class document
and sweet graphics bindings sweet Strang
is not a valid typing so we're trying to
help it make it easier you can debug
this stuff so I won't leave that broken
and just read launch graphics binding
again and let's look and all the
information that's available to help you
bug this stuff
so a tremendous amount of stuff so and
this is important because people had you
know a good bit of difficulty in
diagnosing their mistakes so now we're
going to you know make it easy for you
to see exactly what's going on so in
this circle class there's pretty much
every information that can be everything
that's worth knowing this here and while
I'm here let me show you another
debugging trick that I think you'll like
let me see here let me put a breakpoint
on one of our simple accessor methods so
just set radius and continue that
let's just cause it to be called with a
simple script so set radius there we go
so let me show you what Coco scripting
is doing right here by the way if you
follow the stack trace you know that the
apple event manager is getting the apple
event this Coco's apple event manager
making a script command out of it
executing the script command and this is
the set command it eventually boils down
to a call to the key value coding set
value for key method which is smart
enough to call your set radius method
now this one is interesting if you look
at this if you take a peek at one of
these private function names and a float
value and notify what is n notify mean
well it means it means that their key
value observers registered with this
object as a result of the bindings that
are in the UI of this program and it's
going to tell them that the value
changed even though this method does
almost nothing so let me put a good
break point at the method that does that
and the way automatic kvo works by the
way is it causes indications of will
change value for key and did change
value for key around the invocation of
your set method so let's put a
breakpoint around did change value for
key and continue and Xcode wants my
attention but I don't want to talk to so
set float value and notify just finished
calling your set accessor now it's
calling bid change value for key so it's
going to send did change value for key
is where notifications are sent to any
observers so if you've been working with
cocoa bindings by the way you might have
had some trouble figuring out who's
observing who let me show you this
little trick right now
p 0 self-observation info observation
info is declared as a void starts an
opaque type I don't want you to digging
around in there too much I don't want
you to write dependencies on the
internal structure of that but it's okay
to take peace so here's what it outputs
this object is one of the circles and
graphics binding and there's observers
of its shadow offset property color
radius shadow angle X lock why lock it's
because the graphics view is observing
all this stuff so that it knows whether
or not to redraw the circle so great
deal of information there for you so
that's it for demos so so one thing
we've learned over the past year since
releasing the cocoa bindings
functionality is that we achieved of our
goals and helping you to write not much
not much code but i think we ramped up
the the learning curve a little bit for
cocoa so and a big part of that is is
debug ability and if you are at the
cocoa binding session a couple of days
ago you know we pointed you to a new
default that we're adding for binding to
get give you more logging of what goes
on in bindings we're going to be doing
more stuff like that we want to make it
as easy as possible for you to find your
mistakes while you're learning to use
this stuff quickly so you'll you'll see
more you know description stuff more
logging special defaults that you know
you can turn on to do sources trickery
things like that so before I go I want
you to point point you to this very
important that i can make sure
absolutely everybody is reddit the
scripting interface guidelines which are
pretty new but they're not that nude i
think about four months old now there's
there since last WC though and i want
you to read this because consistency
across apps is tremendously important in
script ability just like as it is in
regular graphical user interfaces people
you know want to learn stuff once and
real
client over and over again following the
guidelines helps them do that it makes
your scriptable application look better
it makes other scriptable applications
look better too when everybody's looking
the same so Coco itself of course either
follow these rules scrupulously right
now or is about to start following these
rules scrupulously or we might just
change some of the rules a little bit so
but in general what Coco does is going
to be perfectly consistent with what we
suggest you do so and you know we're
making it easy for you to follow these
guidelines yourself and now that you
know about this read this if you're
going to buy about to start work on a
new application and you haven't read
this yet read it now because it actually
has some design advice about how you can
save yourself a little bit work later
and and things like that so it's a
developer apple com tech notes it's
number it's number 2 106 so for more
information you can go to our website
and just click click click away or you
can send an email to Matt Formica