Creating a More Complex Script


You now know how to use Script Editor to create a simple AppleScript script. This type of script, however, has limited value. A simple script that doesn’t take advantage of the full AppleScript language is not very intelligent.

More frequently, you’ll use AppleScript to create more complex scripts. This section explains how to create a full-blown script quickly and use the resulting custom utility to augment an application’s capabilities.

Making a Finder utility

Your Mac OS X disk is full of special folders, but when someone sends you a file, it’s up to you to figure out where the file belongs. For example, you have to sort TIFF and JPEG files into your Pictures folder, QuickTime files into your Movies folder, and MP3 files into your Music folder. You also have to classify and put away fonts, sounds, and so on. The Finder doesn’t help you sort out any of this.

You can, however, write a simple script that recognizes certain types of files and uses the Finder to move files to the folders where you want the files to go. The destination folders can be any folders that you have permission to change. These include all the folders in your home folder. If you log in as a user with administrator privileges, the destination folders can also include folders in the main Library folder.

Beginning the script

To begin writing a new script in Script Editor, choose File New Script. Then type the following statement in the script editing area of the new script window:

choose file

This command gives the script user a way to specify the file to be moved. The choose file command displays a dialog for choosing a file. This command is part of a scripting addition that is preinstalled in Mac OS X. The name of this scripting addition is Standard Additions.

start sidebar
Changing AppleScript Formatting

You can change the way Script Editor formats text by choosing Script Editor Preferences... and clicking on the Formatting icon in the toolbar. A dialog appears listing various components of AppleScript commands and the text format of each component. You can change the format of any component by selecting it in the dialog and then choosing different formatting from the Font and Style menus.

click to expand

end sidebar

Seeing the script’s results

The script isn’t finished, but you can run it now to see the results of the one statement you have entered thus far. Click Run to run the script in its current condition. When AppleScript performs the choose file statement, it displays a dialog for choosing a file. Go ahead and select any file and click Choose. Because there aren’t any more script statements, AppleScript stops the script.

AppleScript shows you the result of the last script action in the result window. If this window isn’t open, choose Show Result from the Script Editor Controls menu. The result window contains the word alias and the path through your folders to the file you selected. This wording does not mean that the file is an alias file. In the context of a script, alias means the same thing as file path. Figure 23-5 is an example of the result window.

click to expand
Figure 23-5: Script Editor’s result pane shows the result of running a script.

Using variables

The result of the choose file statement is called a file specification, or file spec. A file spec tells Mac OS X exactly where to find a file or folder. You need the file spec later in the script, so you must put it in a variable, which is a container for information. You can place data in a variable and then retrieve it whenever you want to before the script finishes running. The data in a variable is called the variable’s value. You can change a variable’s value by placing new data in it during the course of the script.

On the next line of the script, type the following statement:

copy the result to thisFile

This statement places the result of the choose file statement in a variable named thisFile. You can include the thisFile variable in any subsequent script statements that need to know the file spec of the chosen file. When AppleScript sees a variable name, it uses the current value of the variable. In this case, the value of variable thisFile is the file spec you got from the first statement.

When you run the script, you see that the copy command doesn’t change the result of the script (as displayed in the result window). Because the result is just being copied to a variable, the result doesn’t change.

Capitalizing script statements

You may notice the capital F in the thisFile variable and wonder whether capitalization is important when entering AppleScript statements. In general, you can capitalize any way that makes statements easier to read. Many AppleScript authors adopt the convention of capitalizing each word in a variable name except the first word, hence thisFile. This practice helps you distinguish variables from other terms in statements, which are generally all lowercase.

Getting file information

Ultimately, the script you are creating decides where to move a selected file based on the type of file it is. In Mac OS X, a file’s type may be indicated by an extension (suffix) at the end of the file’s name or by a hidden four-letter code known as the file type. Therefore, the script needs to determine the name and the file type of the selected file. You can use another command from the Standard Additions scripting addition to get this information. (Standard Additions is preinstalled in Mac OS X.) Enter the following statements beginning on the third line of the script:

copy the info for thisFile to fileInfo copy the name extension of the fileInfo to nameExtension copy the file type of the fileInfo to fileType

The first of these statements uses the info for command to get an information record about the selected file that is now identified by the variable thisFile. The first statement also copies the entire information record into a variable named fileInfo.

A record in AppleScript is a structured collection of data. Each data item in a record has a name and a value. AppleScript statements can refer to a particular item of a record by name, using a phrase similar to item of record. This is the phrasing used in the second two statements above.

Each of the second two statements gets an item of a record and copies it into a variable. The item names in these statements, name extension and file type, are taken from the AppleScript dictionary definition of the record. In this script, the record was obtained by the info for command in a previous statement. Figure 23-6 shows the AppleScript dictionary entries for the info for command and the record it provides.

click to expand
Figure 23-6: An AppleScript dictionary defines the structure of a record provided by an AppleScript command.

To test the script so far, run it, choose a file, and look at the result. The result window contains the four-letter file type of the file you chose, displayed as a piece of text. Figure 23-7 shows the whole example script so far.

click to expand
Figure 23-7: This script uses the info for command to get file name and file type information.

Using parentheses

You may notice that when AppleScript compiles the example script, which happens when you run the script or check its syntax, AppleScript adds parentheses around info for thisFile but does not add parentheses in other statements. AppleScript adds parentheses around a command that returns a value, which any info for command does. However, AppleScript does not add parentheses around a command at the end of a statement, such as the choose file command in the first statement of the example script. Nor does AppleScript add parentheses around elements that refer to a property, such as the name extension of the fileInfo in the example script.

Parentheses group elements of a command together. You can type your own parentheses around elements that you want to group in a statement. Parentheses make a complex AppleScript statement easier to read. Parentheses may also affect the result of a statement, because AppleScript evaluates elements within parentheses before evaluating other elements of a statement.

Working with an application

For the next part of the script, you need to add statements that move the chosen file to the folder where it belongs. The script can use an application — the Finder — to move the file. Add the following statement to have AppleScript start using the Finder:

tell application "Finder"

After AppleScript encounters this statement, it knows all the commands and objects from the Finder’s AppleScript dictionary. This means that subsequent statements use commands and objects that the Finder understands. AppleScript sends these commands and objects to the Finder in Apple event messages. The script doesn’t yet include any statements for the Finder, but we add some next. Later we add an end tell statement to have AppleScript stop using the Finder.

Performing script statements conditionally

When creating complex scripts you may want to give your script the ability to decide what to do based on the factors you specify. AppleScript, like all programming languages, lets you include a series of conditional statements, or conditionals for short. Each conditional begins with an if statement, which defines the condition to be evaluated. The if statement is followed by one or more other statements to be performed only if the condition is true. The conditional ends with an end if statement. In AppleScript, a simple conditional looks like this:

if the fileType is "MooV" or the nameExtension is "mov" then move thisFile to folder "Movies" of home end if

In this example, the if statement contains a two-part condition. The first part of the condition determines whether the current value of the fileType variable is MooV, which is the four-letter file type of QuickTime movie files. The second part of the condition determines whether the file name ends with mov, which is the file name extension of a QuickTime movie file. If either part of the condition is true, AppleScript performs the included move statement. If both parts are false, AppleScript skips the included move statement and goes on to the statement that follows the end if statement. Remember that AppleScript sends the move command to the Finder because of the tell application statement earlier in the script.

Note

AppleScript considers the dot, or period, between the file name and the extension to be a separator. The dot is not part of the extension or the file name as far as AppleScript is concerned.

Include as many conditionals in the example script as you want. In each conditional, use the four-character file type and corresponding file name extension for a different type of file, and specify the path of the folder to which you want AppleScript to move files of that type. A quick way to enter several conditionals is to select one conditional (from the if statement through the end if statement), copy it, paste it at the end of the script, and change the relevant pieces of information. You can repeat this for each conditional you want to include.

start sidebar
Finding a Folder Path

If you don’t know the full path of a folder, you can use a script to get this information. Open a new window in Script Editor and type the following script in the script editing area:

choose folder

Run the script and select a folder. The result is a file spec for the folder you selected. You can copy the text from the result window and paste it in any script.

You may notice that the file spec has a colon after each folder name. AppleScript uses colons in file specs to maintain compatibility with Mac OS 9 and earlier. Outside of AppleScript, Mac OS X generally follows the UNIX and Internet convention of putting a slash after each folder name.

end sidebar

You can make the above script a bit more interesting, by using the choose folder in place of a hard coded folder path:

move thisFile to choose folder 

This will ask the user where to place the file after it is moved.

Breaking long statements

When you type a long statement, Script Editor never breaks it automatically (as a word processor does). You can break a long statement manually by pressing Option-Return. (Do not break a statement in the middle of a quoted text string, however.) AppleScript displays a special symbol ( ) to indicate a manual break. Here’s an example:

if the fileType is "JPEG" or  the nameExtension is "jpg" or  the nameExtension is "jpeg" then move thisFile to folder "Pictures" of home end if

In this example, the first statement, which goes from if through then, takes three lines because it has two manual line breaks.

Ending the use of an application

After the last statement that is directed at the Finder, the script needs a statement that makes AppleScript stop using the application. Type the following statement at the end of the script:

end tell

This statement doesn’t include the name of the application to stop using because AppleScript automatically pairs an end tell statement with the most recent tell statement. Subsequent statements in the script can’t use the commands and objects of that application.

start sidebar
Finding a File’s Type

You may not know the file type of the files that you want to move. For example, you may know that you want to put font files in your Fonts folder, but you may not know that the four-letter file type of a font file is FFIL. To make a script that reports the file type, copy the following three-line script to a new Script Editor window:

choose file copy the result to thisFile copy the file type of the info for thisFile to fileType

Run this three-line script and select a file whose four-character file type you need to learn. If the result window is not visible, choose Show Result from the Controls menu. The result of the script is the file type of the selected file. You can copy and paste the result from the result window into a conditional statement in any script window.

If you choose a file that has no file type, one of two things happens. The result window displays an empty value (indicated by quotation marks with nothing between them) or AppleScript reports an error, saying that it can’t get the file type.

end sidebar

Now is a good time to recheck the script’s syntax. If you tried to compile recently, you got an error message about a missing end tell statement. Click Compile now, and after AppleScript compiles the script you see Script Editor neatly indent statements to make the structure of the script more apparent. If AppleScript encounters any errors while compiling your script, Script Editor advises you of them one by one.

Trying out your script

After creating a new script, you must run it and test it thoroughly. To test the script that moves files according to their type, follow these steps:

  1. Run your script.

  2. When the dialog appears, select a file that is of a type your script should recognize but that is not in the destination folder; then click Choose.

  3. Switch to the Finder, and make sure that the file you selected actually moved from the source folder to the destination folder.

  4. Repeat the test, selecting a different file type that your script should recognize.

Figure 23-8 shows an example of a script with four conditional statements that move a selected file depending on its file type or file name extension.

click to expand
Figure 23-8: This script uses conditional statements to determine where to put a file; it also incorporates user choice for PDF files.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net