Author Topic: Lesson #2.2 : Commands  (Read 3078 times)

0 Members and 1 Guest are viewing this topic.

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
Lesson #2.2 : Commands
« on: May 09, 2007, 02:59:33 PM »

2.2 Commands

AppleScript is said to be a “natural English” programming language. This means that, unlike many other languages, one can easily infer what a line of code will do with little or no knowledge of AppleScript itself. AppleScript code reads like sentences in a book.

In the previous lesson: Objects, we learned about the “subjects” of our upcoming sentences. In this lesson: Commands, we will learn about the “verbs”.

The simplest command in AS has to be the beep commands.


The purpose of this command is to have your mac sound an “alert” (using the sounds defined in your Preferences > Sound  > Sound Effect > Choose an alert sound)

In the previous lesson, we also brushed on scope. In the preceding script “beep> is not directed to anything so AppleScript will look internally to try to interpret what it means. In fact, and this is very important to remember, AppleScript has a defined scheme for finding the meaning of commands (or any other terminology for that matter). So when your Script Editor attempts to “compile” your code it will look sequentially in these locations:
  • 1. Applescript: AppleScript comes with a “default” set of commands which call be called upon from anywhere in your script.
  • 2. Scripting Additions: Scripting Additions (or OSAX : Open Scripting Architecture eXtenion) are system extensions that extends the “vocabulary” of AS. For the most part, these are freely available on the Internet and generally address a specific “shortcoming” of the default  AppleScript language. As such they also can be called from anywhere in your code.
    Note: AppleScript comes with a set of OSAXes, I would strongly suggest you only enable the ones you actually use. Furthermore, these, being part of  the “Open” side of programming, developers have but a few guidelines to follow when it comes to creating their own Additions. This means that it is not uncommon to see the same terminology used in multiple OSAXes. Needless to say conflicts may occur but in most cases Applescript will 'use” the first definition it finds.
  • 3. Scriptable Application: As I mentioned before, developers are responsible for the implementation of AppleScript in their application. In order to make use of these commands, these commands must be directed to the application itself.
  • 4. User-defined commands: These are subroutines of handlers that you, as a scripter, can add to your code. (We will see those in an upcoming lesson.)

You will soon realize that the most often used commands are “set” and “tell

The “set” command either assign or modify the value of a property or a variable.

set aVariable to 124
set contents of paragraph 1 to "This text"

The “tell” command on the other hand  direct your code to the specified object:

tell application "TextEdit" to launch

Combining your orders:
You would quickly find it cumbersome to specify your “target” at every line:

  • William get up
  • William take out the trash
  • William do your homework

Applescript provides a way around that with its “tell… end tell” construct. By including all your commands directed to a particular object with a tell [Object] ... end tell:

tell application "Finder"
                    set label index of file (ThisFile as text) to 3
                    make new folder at folder ThisFolder with properties {name:”New Folder”}
               end try
          end tell

You can also “nest” your tell statement inside an other:

tell application "Finder"
     set label index of file (ThisFile as text) to 3
     tell application "TextEdit"
          -- more code can go here
     end tell
end tell


Commands may accept (or require) some parameters. Going back to our “beep” example and looking at its dictionary definition (In your Script Editor, select Open dictionary from the File menu and choose “Standard Additions” from the list. Once opened, just type “beep” in the search field and select it in the search results):

beep v : Beep 1 or more times
     beep [integer] : number of times to beep

We can see that the “beep” commands can accept only one optional (denoted by a set of brackets) parameter of type integer that specify the number of “beep” to sound..

Another type of parameter is the “named parameter”. For example, looking at the “say” command:

say v : Speak the given text
     say string : the text to speak, which can include intonation characters
     [displaying string] : the text to display in the feedback window (if different). Ignored unless Speech Recognition is on.
     [using string] : the voice to speak with.
     [waiting until completion boolean] : wait for speech to complete before returning (default is true). Ignored unless Speech Recognition is on.
     [saving to anything] : the alias, file reference or path string of an AIFF file (existing or not) to contain the sound output.

we can see that it needs at least one parameter of type string (the text to say using text to speech. It also allow you to specify other parameter as long as you specify its label as well. The bonus of labeled (or named) parameter is that you only need to specify the parameters that you need. Furthermore, since they are labeled you can put them in the order you see fit:

say "Hi there neighbor!" using "Fred"

There is one special command in AppleScript in a sense that it is itself optional. That is the “get” command. For this reason you will not see it often but it is nice to know that it is there nonetheless.

get 4 * 5
--> 20

4 * 5
--> 20

Getting results out of commands.

The AppleScript language is special in the sense that almost every command produce a result of some sort (of course, as luck would have it, both “say” and “beep” do not follow this rule). The “cryptic” name AppleScript as chosen for the entity is “result

get 4 * 5
say (the result) using "Kathy"

I told you that AppleScript was a “natural English” language, notice the addition of the word “the”  that make's the code easier to read.

This concludes this lesson on “Commands”, come back shortly for a brand new one on “Variables”.

« Last Edit: June 11, 2007, 09:12:08 AM by Publi-Script »
Michel Lemieux
Forum Administrator
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 441
  • Karma: 6
  • Gender: Male
Re: Lesson #2.2 : Commands
« Reply #1 on: May 10, 2007, 03:54:28 AM »

Wooh! I can't believe it! The Road Runner himself!!!  ;D
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz • 10 Go 1066 MHz DDR3 • Mac OS 10.10.5 • QuarkXPress + 2017 • Adobe CS5+6 =