Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [160]

By Root 1580 0
of the same letter are taken to be equivalent. Ignored by default.

diacriticals

If ignored, variants of the same letter with different accent marks (or no accent mark) are taken to be equivalent. Considered by default.

expansion

If ignored, ligatures are taken to be equivalent to their component characters. Considered by default.

hyphens

If ignored, hyphens are taken not to exist. Considered by default.

punctuation

If ignored, word-boundary punctuation and quotation marks and apostrophes are taken not to exist. Considered by default.

white space

If ignored, spaces, tabs, and line-break characters are taken not to exist. Considered by default.

numeric strings

(New in Tiger.) If considered, the string is treated as a version number, consisting of sections delimited by a period; order is determined by successive numeric comparison of these sections.

Here's the syntax for writing a string consideration:

considering | ignoring considerations [but ignoring | considering considerations]

-- code

end considering | ignoring

Each set of considerations is any number of string considerations separated by comma; AppleScript will rewrite the last comma as and. Entire string consideration blocks may also be nested. So, for example:

ignoring hyphens, expansion and punctuation

considering white space but ignoring case and diacriticals

"a-" = "Å!" -- true

end considering

end ignoring

String considerations are Unicode-savvy:

set bigEpsilon to «data utxt0395» as Unicode text

set littleEpsilon to «data utxt03B5» as Unicode text

ignoring case

bigEpsilon = littleEpsilon -- true

end ignoring

Here's an example illustrating the new numeric strings consideration:

"1.10" > "1.9.3" -- false

considering numeric strings

"1.10" > "1.9.3" -- true

end considering

In the comparison inside the considering block, 1 is equal to 1 so we proceed to the next section; 10 is greater than 9 so that's the result of the comparison.

Errors


An error is a message at runtime saying, in effect, that something bad has happened and execution cannot continue. The sender of such a message is said to throw an error. The message percolates up through the chain of handler calls (the call chain ), looking for an error-handling block surrounding the line currently being executed; such a block is said to catch the error. If no such block catches the error, it percolates all the way up to AppleScript, and the script terminates prematurely (possibly with an error dialog ).

This entire mechanism is extremely nice, because it provides a target application, or AppleScript itself, with a way to signal that it's impossible to proceed, interrupting the flow of code while leaving it up to the caller whether and how to recover. Your script can implement no error handling, in which case any runtime error will bring the script to a grinding halt. Or your script can implement error handling in certain areas where it expects an error might occur. It can recover from some errors and re-throw others, allowing them to terminate the script. It can even throw an error deliberately as a way of controlling the flow of code.

An error can be a positive thing, and can be built into the structure of a command's implementation. For example, display dialog throws an error if the user clicks the Cancel button in the dialog. This need not kill your script. It will if you let it, and this can be a good thing (because Cancel often means "stop"). But alternatively, your script can catch the error as a way of learning that the user has cancelled, and can then proceed in some other appropriate manner.

I'll talk first about how to throw an error, then about how to catch one.

Throwing an Error


To throw an error, use the error command. It has five optional parameters:

error [messageString]

[number shortInteger]

[partial result list]

[from anything]

[to class]

Here are the default values of the parameters:

messageString

Nothing

number

-2700

partial result

The empty list

from

The currently executing script or script object


Return Main Page Previous Page Next Page

®Online Book Reader