[ Pobierz całość w formacie PDF ]
.Just add them to the bracketed list in the first line, always in quotes and followed by acomma.Scripting AdditionsMuch of AppleScript's power comes in the form of add-on files called scripting additions.You canthink of them as plug-ins, each of which adds a particular new feature to AppleScript's repertoire.InMac OS 9, they sit in your System Folder Scripting Additions folder.In Mac OS X, you may find scripting additions in any of three different places.The standard additionsare in the System Library Scripting Additions folder of the Mac's hard drive.As with fonts,sounds, or other settings, you can also install scripting additions that only you can access by makinga Scripting Additions folder in your Home Library folder.Similarly, if you're an administrator, youcan make a Scripting Additions folder in the main Library folder (in the hard drive window);everyone with an account will be able to use the scripting additions you file there.Like a scriptable application, each of these scripting additions has its own dictionary its ownspecialized AppleScript commands that you can use in your scripts.You view these new commandsjust as you would when studying the vocabulary of a program: by opening Script Editor, choosingFile Open Dictionary, and then navigating to and opening the scripting addition you want.7.5.4 Advanced AppleScriptNo single chapter in fact, no entire book can make you a master AppleScript programmer.Gaining that kind ofskill requires weeks of experimentation and study, during which you'll gain a lot of appreciation for what full-timesoftware programmers endure every day.AppleScript, despite its friendly appearance and abundance of normalEnglish words, uses many of the same structures and conventions as more advanced programming languages.By far the best way to learn AppleScript is to study existing scripts (like those in the Library Scripts folder)and to take the free online training courses listed at the end of this chapter.And there are thousands ofexamples available all over the Web.Trying to figure out these scripts running them after making small changeshere and there, and emailing the authors when you get stuck is one of the best ways to understand AppleScript.Figure 7-8, for example, is a script called Add to File Names.scpt.It's one of the professional sample scripts thatcome with Mac OS X.To open it, navigate to your Library Scripts Finder Scripts folder and double-clickAdd to File Names.scpt.A quick glance can tell you a lot about the tricks of professional scripters:Figure 7-8.The Add to File Names script is a classic AppleScript example.It contains variables,subroutines, nested "if" statements, and many other common elements of profes-sionally writtenscripts.(After typing a description into the box at the top of the window, you can click the flippytriangle to the left of the Description box to hide the entire thing, thus maximizing your script-writingspace.)Description.Careful script writers document their work.They add lots of notes and explanatorycomments for the benefit of whoever might want to study or amend the script later (which is oftenthemselves).Adding a description of the script's function in the Description box (at the top of the ScriptEditor window) is a good first step.(Whoever wrote this particular description didn't know the difference between effect and affect, but it'sthe thought that counts.)Variables.A variable, in programming terms, is a placeholder for some information that may changefrom time to time.The President of the United States is a variable: there always is one, but his namechanges every few years.As stand-ins for more complicated ideas, variables help simplify the script, clarify its purpose, and savetyping.In AppleScript, as in other programming or scripting languages, you can define your ownvariables.In the one line of the Add to File Names script, for example, you can see the command set this_item toitem i of the item_list.That common command tells AppleScript that you're defining your own variablecalled this_item, which will henceforth mean "a reference to the ith item in the list of items calleditem_list." In this case, "i" is a counter that refers to the item's position in the list.NOTEFor some reason, AppleScripters tend to use variable names where the first letter islowercase, but subsequent words appear with no spaces and capital letters,somethingLikeThis, or with underlines, like_this.Scripters typically use i, j, and k ascounters, for example, to hold the number of times a loop has executed.Subroutines.About a third of the way into the script shown in Figure 7-8, one line says, myset_item_name(this_item, the new_item_name).That step tells the script not to proceed through thecommands in sequence, but instead to jump to a subroutine a group of commands that's beenseparated from the first batch.Subroutines allow groups of commands to be more easily reused indifferent parts of the script.In this case, the subroutine begins four lines later ('my set_item_name(this_item, new_item_name)').Nested "if" statements
[ Pobierz całość w formacie PDF ]