Introducing AppleScript Libraries
Session 416WWDC 2013
In a major update and enhancement to its native scripting language, OS X delivers a new plugin architecture for AppleScript, enabling easy development and access to custom sets of functions and methods. Each individual AppleScript Library, saved as an AppleScript script file, can publish its own scripting terminology, and contain dozens of routines written in native AppleScript or the incredibly powerful AppleScriptObjective-C. Scripting just got a whole lot better!
[ Silence ]
Hello, welcome to the World Wide Developer Conference,this is the second automation session today, how many wentto first session this morning…okay.
So our little presentation about Libraries kind of gotto you a little bit huh, you’re interested in this.
This session we go in depth in discussing AppleScript Librariesand we’ll examine not only simple but complex onesand we’ll walk you through the processof how you create those includingfor the first time ever anywhere an example of howto create a scripting dictionary.
I think once you see how it’s done,it’s not so frightening it’s pretty easyto do it’s just an XML file.
So today we’re going to be talkingabout AppleScript Libraries and what they can do for you.
Let’s try click, welcome, today it’s AppleScript Libraries,we covered that and we’re going to be learningabout what they are, how you incorporate theminto your environment, how you call them, how use them,how you create them and how you can deploy them.
So begin with, why Libraries…those who wereat the first session understand that I’m one of those kindof people that is a code maven, I have sampled routinesthat do just about everything and I have them allover the place on my drives and I’m very thankful nowthat we have iCloud support so I can keep themin one central location but I like to use routinesand handlers in my scripts quite a bit instead of writing codeover and over, I like just getting the routinesand handlers and pasting them into my scripts.
So if you’re like me and you use the same routine overand over you’re going to like Libraries,they’re going to be useful for you.
Again if you are a personthat uses third party scripting additions to find that commandthat was missing in AppleScript you’re goingto like Libraries as well.
If you just want to be someone that can simplify your scriptsif you want to take all this complexity and routinesand handlers out of your scripts and just have an easier wayto manage them and to be ableto call them globally throughout your scripts you’re goingto like Libraries.
It all started because you know once I learned howto create a subroutine or handler I started using themand I would put them in my scripts and it worked very welland then I would create another one and then I would putin in some of my scripts and not in some of the other onesand I would update one but I didn’t update the otherand then I added more routinesand then the next thing you know it getsto be this real big jumble that you try to manage.
Well we’re solving that issue and those problems nowin Mavericks with the introductionof AppleScript Libraries.
They are a new plug in architecture for AppleScriptthat allows you to create powerful collections of handlersand tools yourself and deploy them across your machinery.
In addition, they differ from the standard scripting additionthat we’re used to using all the time in a couple ways.
First of all they can be written in AppleScript, you don’t haveto know C or C ++ in order to create an AppleScript Library.
Because Libraries are loaded by the script,the script is controlling how they useand that will help you avoid terminology conflictsor resource problems, all of that kind of goes awaybecause the script is loading the Libraryand the Libraries live within the spaceof the application that’s executing the script.
Now AppleScript Libraries have some extra abilities to help youout and the first one that’s really important isthat they can take advantage of AppleScript objective C.
AppleScript objective C exposes the Cocoa classes to AppleScriptso things like NSString become available to youand all the methods from that and the instants methodsfrom that now become available for you to usein your AppleScripts scripts and your AppleScript Libraries.
In addition you can also have your libraries publish their ownterminology which means instead of you havingto remember a specific handler name and a specific orderof indicators or data pass throughsor whatever you can actually use English like termsthat you create that work for you and have those be the waythat you call the AppleScript Libraryso the handlers within the Library.
So it’s very powerful and in supportof that we’ve created a couple new constructs,the first is a Script Library reference, a way to easily referto a Script Library that you have installed on your computer.
You’ll see how that differs from the old load Scriptthat we all used to use as a substitute for this.
Then finally we’ve created a new constructthat is called the use clause and it’s a wayto import terminology and to bring in dictionariesand libraries into your scriptand have them be available globally throughout.
So let’s take a look at this,now in the old days before this many of us have triedto use the old script storing techniques that we had availablethrough the scripting addition of load script.
Now if you go to your library pallet in the AppleScript editorand you select standard editions and you openup the dictionary window for the standard editions you have ninesuites in the dictionary and one of those suites dealswith scripting commands and one of those commands is load scriptand here is the definition for that, let’s zoom that in.
Now this command was designed for you to be able to pointat a specific script file, load its contents into memoryand then talk to that script file in memoryand have it do things for you.
But there are a couple issues with using thisand here’s an example scriptthat uses the load script scripting edition.
You can see that the first thing is you have to knowwhere the script that you are using as a library exists,where is it, you have to have its exact locationand then once you’ve identifiedwhere it is then you perform this load script and store itinto a variable that represents the contents of that script.
Then elsewhere in your script you talk to thatand you call its handlers or whatever handlers are availablewithin it and have it work but this is a problem because firstof all it requires that you know exactly where it is and if it’son a different machine then you have to develop wayto locate folders locally,regardless of how drives are named and you haveto have error handlers to make sure that they’re thereand those kind of thingsand then it requires explicit loading, explicit loading,explicit loading of the targeted script file, you have to do thatand it also requires that you address the handlersfrom within the loaded script file directly and passthrough information to them.
This means that there’s no custom terminology,you have to remember handler names,you have to remember the order of the various componentsof the handler and there’s no access to the powerof AppleScript objective C and the wonderful Cocoa Librariesthat are available too on the computer.
So there are some issueswith using the load script scripting editionand we’ve addressed a lot of thosewith our AppleScript Libraries so let’s take a look at it.
We’re going to start very simply;I always like starting exploring a new conceptin the most fundamental ways.
So we’re going to look at simple AppleScript Libraries,you might never write something this simple yourselfbut by examining this we’re goingto learn the principles behind this whole architecture,is that okay, we’ll start simple…good.
Specifically we’re going to look at a problemthat exists all the timeof solving the missing command problem, I want to be ableto do something but there’s no AppleScript command for that.
So by examining this little issue we’re going to learn howto create and use AppleScript Libraries.
Now for our example of a missing command here is the new notesapplication in Mavericks, I’m going to select some textin there and then right click the selection so it bringsup the contextual menu.
Now some of you might knowthat for a couple system releases now we implemented somenew text handling optionswithin the standard text contextual menu and oneof them is transformations where you have the optionto change the case of selected text to upper case, lower case,capitalize or that’s called word case sometimesand you might find it called that as well.
But this is a command that’s available on the contextual menubut it’s not available from within AppleScript itself.
So what I want to do is use this as an examplewhen we’re creating our simple library to begin withand we’re going to look at being able to convertto upper case transformation, so we’ll have something like how,now brown cow in lower case transforms to how now brown cowin upper case, we’re going to have a lower case transformationand instead of capitalization I’m just goingto call it a word case transformationwhere the first letter of each word is capitalizedand this is what we’re going to use as our sample command.
We’re going to look at this in two typesof simple script libraries,one where the library is written entirely in native AppleScriptand two, where the library is written using AppleScriptobjective C.
So just to review here we’re goingto take this missing command of transformed text and we’re goingto use that and create two simple librariesto address the fact that’s it’s not native in AppleScript.
We’re going to create one in AppleScript,one in AppleScript objective C, let’s begin.
An AppleScript script library, so it beingswith having a handler some kind of AppleScript handlerand here I have one, you’ll notice that the nameof the handler is change case of text,we’ll use this handler throughout hereand it has two parameters, one is the source textor the what you want to transform,the text that you want to transformand the second is a case indicator and this caseof this handler it’s going to a numeric value zeroor 1 that indicates whether you want upper or lower case.
It’s a simple AppleScript based upon your case indicator,it creates two comparison lists and then iteratethrough all the characters in the past textand matches whether it’s an iteration listand then finally returns the resulting string backto the script itself.
Something simple right, now we’re going to take thisand make this and make this into a libraryand it’s simplest doing the following.
We’re just going to take a blank script in the AppleScript editorand we’re going to paste the handler into this,we’re going to compile it and then just click saveand we’re going to save it as a script fileand you’ll notice we named it AppleScript text transform isthe name of this little library.
Now libraries can contain multiple handlersbut for our example they’re just goingto contain one handler, okay.
And we simply name the library and then save it.
Next we install the script library,and the way that you install it is you goto your home library folder and withinthat library folder you create a new folder called ScriptLibraries and then you take your newly saved AppleScript fileand you drag that into the Script Libraries foldersand it becomes part of the AppleScript systemon your computer, that’s it, it’s now installed for you.
So how do you use the AppleScript script library?
Now that we’ve created this simple library with one handlerfor transforming the case of text here’s how you use it.
We have a new construct in AppleScript and it’s a wayto identify easily AppleScript Libraries.
So you no longer need to point to a specific filethrough a file reference like you did with LoadScriptand you no longer need to explicitly load the fileand all you need to do now is you go into a blank script,you type the word script followed by the nameof your library and you notice I left the name extension off,you don’t need to have the name extension there.
So script, AppleScript transform and thenwhen I compile this you’ll notice that it formatsand this is now a AppleScript Library reference,it’s a way to point to that library,it was automatically located on your system and it’s preparedfor use, it’s ready to go all you have to doto address it is you can place the verb tell before itor in this case you could enclose it within a tell blockand then refer to one of the handlerswithin that particular library.
So here I have the name of our handler is change case of text,I’m passing into the handler how now brown cowand I have a case indicator of one meaning I want upper case.
So I run my script, it automatically gets processedby your AppleScript libraryand the results are returned back to the script.
So that’s the basic processof creating a very simple basic AppleScript Library.
So here’s the routine that we used,it’s a little subroutine now it does convert case of some textfrom upper to lower and lower to upperbut it doesn’t handle special characters and it doesn’t workwith multiple languages.
In order to have an AppleScript handler to do allthat it might be as long as I am tallor a couple times past that, it would be very complex to do allthat in native AppleScript.
So in this case we need to turn to somethingwith a little bit more power and we’re goingto create our second library example using the powerof AppleScript objective C.
So this will be a simple library writtenin AppleScript objective C, everybody with me so far?
Not so hard right, okay good.
Okay here’s the same handler, this time it’s change caseof text and it gets two things passed to it, numeric indicator,I mean the source text that we’re going to transformand a numeric indicator indicating text.
But this time the body of the handler is writtenin AppleScript objective C and the first thingthat the handler does is convert the pastinto a Cocoa string using NS string method called stringwith string, then based upon the numeric indicator that’s passedthrough the numeric case indicator it executes a methodon that instance, either upper case string, lower case stringor capitalized string.
Once that transforms been applied and copiedinto the variable nextto it adjusted string the handler returns the adjustedstring back to the script as text by just sayingas text in your coercion.
So this is the handler that we’re going to usein our second example library.
We take a blank script and we paste our handler,the AppleScript objective C handler into our script, we saveand in the sheet to save a little bit of time I’m goingto save it into the script libraries folderthat I already created instead of dragging it in the finder.
You can if you…you can drag if you want tobut for our purposes here I’m just going to save itinto the newly created script libraries folder.
Next I’m going to give it a different name,I’m going to call this one ASOC text transform and ASOC standsfor AppleScript objective C.
Finally at the bottom under format insteadof the standard script file format we’re going to chooseto save this as a script bundleand you’ll see why in just a moment.
So we save our script and then you’ll notice once you’ve savedit in a bundle format at the top menu bar there’s a new buttonthat becomes available and what that button does iswhen you toggle it, it will show you the contentsof the script bundle so if I click that a drawer is exposedand this drawer contains a lotof valuable information about this bundle.
First of all it contains the name of the script library,it contains its bundle identifierwhere you can put your developer domainand name it whatever you like,it contains a short version number, a bundle version number,a copyright string, the terminology file nameand a check box indicating whether this library usesAppleScript objective C, a scroll area down at the bottomthat will display the contents of your resource folderand a little magic pop up menu that does particular actionswith things that you’ve selected in the resource folder.
So in this particular instance there’s only four thingsthat we’re interested in.
They are…we’re going to start by adding the nameof our library, ASOC text transform, in the name field,we’re going to give it our developer domain identifierso here my company is nighthawk productions dot asoc dot textdot transform and then we’re going to give a copyright stringand most importantly we’re going to check the check boxthat says this library uses AppleScript objective C.
Those are the four things we want to dosince we’re creating a librarythat uses AppleScript objective C.
Once we’ve done that then I can close the drawer,save the script file, now I’ve already installed this,remember I’ve saved it into the script libraries folder,if you didn’t you can drag itin yourself before doing the next step.
Now that I’ve had this libraryand it’s installed how do you use it, well the same waythat you used the earlier library,the first thing you do is you type the word script,followed by the name of the library,in this case it’s ASOC text transform, you compile itand you get your AppleScript library reference,identifier so this indicates to me because it’s compliedthat the script has located the library, prepared it for use,it’s ready to go, I can encase it within a tell blocklike I did before, I can refer to the handler within,I can pass text into the handlerand my first indicator is the…case indicator is thenumber zero.
So I run it and the result will be all upper case how nowbrown cow.
Let’s iterate that case indicator to the number one,run it again wa-la [phonetic] I have all lower case,let’s iterate it again to the number twoand I will get word case where the first characterof every word is capitalized.
So this handler is much better than the earlier handlerthat was just written in AppleScript,it really has a lot more power and ability because it tapsinto the power of the Cocoa class NSStringto do all the heavy lifting.
Now this works by convertingto all the case conversions we need,it works with special characters and it workswith multiple languages.
So this is a very useful one and to demo this I’m going to bringup my friend Chris Page from the AppleScript team, thank you.
[ Applause ]
Now this is a script that Sal showed you earlierand it has a function that changes the case of some textand just a trivial example of using that and I’m going to runthat script and we can see the result down here.
We converted this string to upper case.
So just very quickly I’m goingto give you the quick run through, this is what happenswhen you already know how to do this.
I’m going to take this function, I’m going to copy itinto a new script and I’m going to save itin the script libraries folder and the slowest partof this is giving it a name,I’m going to call it AppleScript change case and we’re done,we’ve now created a library.
Now I’m going to run through that a little bit slowerfor people who are trying to follow along,I’m going to copy this text, create a new script,paste it in and save it.
Now this is…if this is the first time doing thisand you don’t have the script libraries folder here’s a littletrick you can press Command Shift G to bring up this sheetthat lets you navigate to the libraries folderin your home directory because the library folder is normallyhidden from users but as a developer you need to know thisto be able to get to that folder.
You can also get there in finder.
Then you would use the New Folder button to createthat folder and then you could save the scriptin there and that’s it.
I’m going to close this and now I’m goingto take the client code that uses this library I’m goingto copy it out of there and create a new script,I’m going to close this one so it’s out of our wayand paste the code in.
Now I’m going to show you how to use the libraryif you were following along earlier,this might look very familiar,I’m going to say tell script AppleScript, change caseand I’m going to compile it and you can see that it works.
I manage not to misspell the library name.
If I had just to show you, if I put an x into the namewhich means it’s not going to be found,when we try to compile it we get an error that saysthat it couldn’t find that script so you’ll know earlyon during script development whether you’ve referredto a library correctly and when I run it we get the same result,how now brown cow is in upper case.
That is the simplest case of using the script library justto reuse a function you’ve been usingin your scripts up till now.
Now we want to take advantage of AppleScript objective Cto do more than we can do with AppleScript,if I take this stringand introduce some accented characters and run you can seedown here in the resultsthat the accented o’s did not get converted to upper case.
That’s because AppleScript doesn’t have native supportfor doing case changes and it doesn’t…you would haveto write a very extensive script to handle all the charactersin Unicode but the foundation framework that’s partof Cocoa has in a string class that has a rich varietyof text manipulation functions,several interesting ones for changing case.
So we’re going to take advantage of thatin an AppleScript objective C version of the librarythat we already created.
I’m going to create a new script and save itin script libraries folder again, this time I’m goingto name it ASOC change case.
ASOC is a short hand we sometimes usefor AppleScript objective C and as Sal mentioned I’ll needto save it as a script bundlebut that’s a pretty easy thing to do.
I’m going to open the bundle drawerand update…well the simplest thing that I haveto do here is enable AppleScript objective C library,that makes ASOC available when this code is runningand while I’m here I’m going to fill out these other fields.
These fields set values insideof the info P list file inside the bundle, there’s a lotof documentation about how do thatand how the various fields work if you’re unfamiliar with themand I’m going to fill in a couple pieces of information,I’ve got my…the name, ASOC change case and I’m goingto put my bundle identifier in herewhich I mistyped…let me try it again.
Okay the version number default is 1.0, we highly recommendthat you take advantage of version numbersbecause when you start distributing and reusingand updating libraries you can use it to distinguishwhich version you’re talking about.
I’m going to show you later there’s a way for youto have your script check the version to make surethat it’s new enough for your client scriptand I’ll put a copyright notice in here.
But again all we really need to do was click this check box,let me put that script aside for a moment and now I’m goingto put in the AppleScript version of our library code.
This is essentially the same function, we haven’t changedthat we’re just changing the implementation,like Sal mentioned earlier we construct an NSStringfrom our AppleScript string, we call the appropriate methodof NSString to do the capitalization we wantand then we return it and convert it backto an AppleScript string.
All right so that’s it we’ve created an AppleScript objectiveC version of the library and now let’s use it.
I’m going to go back to my client script and I’m just goingto change the name to match our new library and I’m goingto run it and this time you can see in the result panethat all the accented o’s have been converted to upper case.
Furthermore we now have a third optionsbecause the NSString class has a methodfor doing capitalization, it capitalizes every wordin your string, we can put a different value in hereand run it and so now we’ve extended…now we get the wordcapitalized version of the string.
So you can see that using AppleScript objective C we cango beyond what we can do in AppleScriptand we can replace large chunks of AppleScriptthat might have had to do a lot of their own workwith existing framework functions that are availableon the system to every other application.
Now you can use them in librarieswhich means you can use them in your scripts and script applets.
Just briefly I want to mention that when we lookup libraries we search in a number of locations,Sal will mention those later and we start by lookingin the current applications bundle which meansthat you can conveniently distribute an appletthat contains libraries that you want to bundle with itand the user can just make that…put that applicationon the machine and run it without havingto install libraries anywhere before use.
[ Applause ]
Okay thank you.
So you can see that it’s not just magicon a slide you can actually do this stuff live, right,and so we were looking at the example that Chris had thereand if we look at the syntax that was involved here,we have tell the script library identifier to call this method,now the short coming in this whole process or the got chain all this process is that if you have a lot of librariesor you have complex libraries, you have to remember the namesof all these different handlers and you haveto remember what parameters are passed through.
It would be so much nicer if I could just…insteadof saying change case of text like that,if I could just use some words I could rememberlike transform text and then give it the textas a direct parameter to and then use some wordlike upper case instead of a number or lower caseor word case, I really want to be able to say thisbecause I can remember this, remembering the names of 150or 200 different handlers that’s really a pain, I want to be ableto use this kind of terminology.
So this is what we want to do as we examine the next thingof creating libraries with terminology.
Step one is you start with the dictionary.
So here is an example dictionary of a script library,it’s not a really complex one but it’s quite functional,it has one suite in it,it’s called AppleScript text utilities, there’s two commandsin the suite, one is for transforming text, the other isfor replacing strings in text.
You can see the definition for the suite and the definitionfor each one of the particular commands.
This is actually a file;scripting dictionaries are actually filesthat get displayed within the AppleScript editor application.
These scripting dictionary files are called sdefs which standsfor scripting dictionary and they are placed insideof the bundle of your AppleScript libraryso for the first time ever today we’re going to look at howto create an sdef or a scripting definition fileor just call the scripting dictionary.
Now these are XML based documents,they have a name extension of .sdefand they define the various scripting elementsthat are part of your library.
For example a suite or a command that might have parametersand it might also use enumerationsin the command as well.
In addition you can include documentationin your scripting dictionary so that your customeror you can actually see an example scriptthat uses the terminology.
So let’s look at how this is done.
This is a scripting dictionary file, this is an sdef.
It’s an empty sdef but it is a fully formed empty sdefand you can edit this with you know anyof the great applications we have out therethat can edit XML documents.
We’re going to use this when we define our terminologyfor an AppleScript dictionary.
The first line is an XML declaration,it’s a standard declaration used by every XML file.
Next becomes a reference to the DTDor document type declaration.
This is just boiler plate stuff, it points to a folderin your system folder that contains the master sdef filethat tells the system how an sdef file is supposedto be read.
Then finally there’s a pairing of tags for dictionary,it’s an open and close tag for dictionary and we’re goingto use this as our template for creating a dictionary that workswith this command, transform text how now brown cowto upper case.
So I’m just going to leave that on the bottom of the screenas we do this so we can keep in mind how this works.
Are you ready to go…here we go, so the first thing I’m goingto do is insert within the dictionary tags a new setof tags called suite, I’m going to create a script suite.
The opening tag to the script suite has a couple parameters,one is the name, I’m going to call it text utilitiesand a description, I can give a descriptionand then a four character code, Apple reserves the useof four character codes that are all lower case.
So those are used by Apple applications like definderto define their objects, you’re welcome to use any combinationof upper case and lower case letters that you want that workfor you, in this case I have four upper case lettersthat I’m using for the four character code for my suite.
Next I’m going to open up this suite and I’m goingto insert a command, I want to put a command in my script suiteand the opening tagof the command tag pairing has a couple of attributes.
The first is the name, that’s the name of the commandin this case it’s transform text,so I want that to be the name of the command,that’s what I’m going to remember in my brainwhen I write my scripts and it gets and 8 character code,commands are the only thing that gets this 8 character codeand the reason it does isbecause the first four characters are usually the codeof the suite that the command belongs to followedby an additional four characters so that’s what I did here.
Next we’re going to open up the command pairing and we’re goingto insert a direct parameter tag.
Now the direct parameter is the object that we’re addressingin this case the text.
So it has a couple of attributes in this tag,the first is the kind of data, in this case type is textand then the description, the text to transform.
There’s no code necessarybecause this is the direct parameter of my command.
Once I’ve done that now I can continue defining my…the scopeof my command by addressing the second parameter here,so I insert a parameter tag in my command pairingand this one gets the name of two,it gets a four character codeand under type I have case conversion.
Now case conversion is not a data type or anythingin particular it happens to be the name of the setof enumerations that I’m going to be using,I’m going to create another, another pairing later in my sdefthat will hold the enumerations like upper case, lower case,word case so for right now I’m just going to typein case conversion and then that’s it.
That’s my basic command tag right there.
After that I’ll insert a pairing for enumeration and this iswhere I will list the various enumerators like upper case,word case, it gets a couple of attributes,the most important being its name has to match the typethat you just called in the previous command so the typefor the parameter in the command has to match the name of the setof enumerations that we’re going to useand it also gets its own four character code.
Once I’ve created the enumeration set I can insert myfirst enumerator, it gets a couple of parameters,it’s name is going to be upper case,it’s gets a four character codeand then we can insert the other two enumerators, lower case,word case, each with its own four character code.
That’s the sdef that will define the use of this command,that’s a fully formedand correct scripting definition file for having a single suitewith a single command that is transform text direct parameterto enumerator upper case, lower case, word case.
This is what it looks like,if you saved this you could just stop therebut you can do a little bit of extra and I suggestthat you make that effort.
We’re going to insertin the command pairing a documentation tag, an openand closing documentation tag and this allows youto put information in there about the commandso you can help somebody out who’s reading your dictionary,you can tell them hey this command is used usuallyfor this, this is how you say it and you’ll noticethat I’ve included a pair of 8 opening and closing HTML tagsin there and that’s because the dictionary viewerin the AppleScript editor used HTML to display the formattingand the color of the information in its window.
So that means that I can then take some HTML and put itinto my documentation.
What I like to do myself is always have an example scriptof how to call the command.
That’s what I did here, I have an examplethat says transform text how now brown cow to upper case.
So no matter what when the user looks at the dictionaryfor my library they can see oh this is how you use it,okay I got it.
Now when this is all displayedwithin the AppleScript editor application it will looksomething like this, you’ll have your suite definition here thenyou’ll have the definition of your command and how it’s calledand then my documentation so this is all documentation,I apply text transformation, for example change the caseof targeted text to upper case and then I have a sample scriptthat the user can see and I show them what the result typicallywould be.
So it’s worth it to add that little bit of extrain your scripting definition file when you create it.
Now that we’ve done that we save that file, we give it the namethat we had for it, AppleScript text utilitiesand we just put it aside.
Next step is we create a script bundle, very easy,you go to AppleScript editor, you open up a new file,you go save and in the drop down sheetunder file format you choose script bundle as the formatthat you want to use and then up at the top you give it a name,I’m going to call this AppleScript text utilitiesand you save it.
Once it’s been saved we get the access to the bundled drawer,we open that up and then again here is the panelwith all the various options for us.
The script name, the bundle identifier,the short version number, the bundle version number,the copyright string, the name of the sdef file,whether we’re using AppleScript objective C, the contentsof the resources folder and the little action menuthat you can do things in the resources folder.
So in this case we’re going to move the windowto the side a little bit and we’re going to actually takethat file and drag it into the resources folder area hereand let it go.
This will copy that sdef file into the script bundle for usand you can see now it has become partof a list that’s available.
Then I can fill out the information up thereby checking the check box that my using AppleScript objective Cand most importantly put the nameof the scripting definition file, the title of it,in that field, you can leaved off the .sdef then you simplysave your script bundle, close the drawer and then save it.
Next we add the code to the script bundle,so we have our AppleScript Library bundleand we’ve been using this one handler over and over againin Chris’ examples and in what I’ve been showing you.
But when you have a terminology your handler hasto match the terminologyso you here we have an AppleScript handler,you can tell because it begins with on and ends with in rightbut the handler syntax has to match the terminologythat I defined in my dictionary so I beginwith my command, transform text.
Next I need something to represent my direct parameterso I have a variable here called source text,it’s going to represent whatever text is getting passedto my handler, then my secondary parameter twoand then another variable called case indicator that’s goingto represent one of the enumerations that I created,upper case, lower case word case.
Let’s take a look at how this works again it takes the sourcestring, converts it to a Cocoa string using NSString’s stringmethod, string with string and then based upon a comparisonbetween what you pass into that handler for an enumeratorversus one of the other options it appliesand instance method wither upper case string, lower case stringand then finally we convert everything backto just plain AppleScript text by sayingas text and we’re done.
So this is the handler that we’re going to use,we take our bundle we paste it into it, we save it,next we install it in case you didn’t save itinto the AppleScript Libraries folder,script libraries folder just take the file,make a script libraries folder in your home library directoryand drag that script file in there and that’s it,it is now installed and now we can call itso next step using the library.
So again we can just target the librarywith an AppleScript Library identifier scriptand then the name of the library and we can encasethat within a tell statement but this time insteadof calling the nameof the handler I can just use my terminology, transform text,here it is to upper case and when this runs it runs fine justlike the other handlers worked, it works perfectly.
So here’s an example of using that library in a scriptfor the finder, so this particular script will changethe case of selected folders in the finderso if I have some folders selected and I want their namesto be upper case this is the script that will do that.
You can see that it runs and the case changes.
Now I’ll walk you through it real quick,you get the selected items that are selected and you iteratethrough each one then we call our script library, right hereand then we apply the results of the transformation to the nameand then end our repeat.
So here’s our tell block, now there’s a problemwith this tell block,if I’m only using it once that’s not bad but if I have a scriptthat has to use this all the time it’s a painand in addition we have this thing going on with variablewhere I have to create a variable to get the resultsof the transformation that I can then use later on in the scriptbecause it’s outside of the tell block.
I want all of that to go away and we fix that issuewith a new construct that we’ve created called the use clause,so at the top of my script I enter the word useand then my script library identifier and whatthat does is dynamically load my libraryinto memory making it globally available throughout thisscript, so I no longer need this tell block addressing my libraryand as a matter of fact once I got rid of that I can get ridof the nonsense with the variables back and forthand put everything on one line so that the finder will talkto this command transparentlyand it all becomes one single statement.
So we go from this to that with the use clause, which is a wayto load and import terminologyinto your script, this is what we like.
Here’s what it looks like in an actual script window,now with the use clause and then taking out the backand forth with the variable.
That’s exactly what we like, it’s easy to remember,it’s powerful to use, it’s clean and it’s a smooth syntax.
Now when you deploy libraries, once you’ve created somethinglike this you can put it in multiple locations,the one that we’ve been talkingabout is your home library folderand in addition you can go to the top level on your computerand you can place it in a script libraries folderwithin that library folder.
If you have a script bundle or a script application youwithin its resources folder you can create a script librariesfolder and place it in there.
So you can pass…give someone a AppleScript dropletor AppleScript applet that contains script librariesthat it can draw from,in addition any application that’s using calling AppleScriptyou can create in its resources folder a script library folder.
So AppleScript Libraries are a new plug-in architectureenabling quick development and accessto your favorite sets of handlers.
It’s different from scripting additionsbecause you can write them yourself in AppleScriptand they’re controlled by the script that loads them.
They can use AppleScript objective Cto access all the wonderment of Cocoaand they can also publish their own scripting terminology.
In support of them we created two new constructs the scriptlibrary identifier, that finds the library for you and loadsand we also created a new use clause that allows youto load the library for global access.
So for more information you can contact these poor guysor watch the video from the earlier sessionon automation over view.
One more thing, this is a very important day for usbecause 20 years ago AppleScript was given to the world.
[ Applause ]
In 1993 AppleScript releasedto the…I mean Apple released AppleScript to the publicand since then it’s become a phenomenal language that peopleand companies rely upon to automate the processesthat they do, they build their businesses on it,they build their careers on it and we wantto take this opportunity on the 20th anniversaryto say thank you to all of the developersthat made their apps scriptable, we want to say thank youto all the scriptors who wrote and write scripts everydayand share them with others.
We want to thank our customers for using AppleScriptand in addition we want to thank all of the engineers that workedon scriptable applications and workedat Apple in creating this.
We really appreciate it, thank you so much.
[ Applause ]
[ Silence ]