Section 14.3. Preventing Errors


14.3. Preventing Errors

There's no sure-fire way to keep your scripts from misbehaving, but there are a few problems that happen more often than others. By keeping an eye out for these pitfalls, you can greatly reduce the chance of your script crashing, hanging, or otherwise misbehaving:

  • Stop infinite loops. Page Sidebar 8.1 has a tempting proposition: letting a repeat statement run forever. Unfortunately, if you do that, you won't be able to quit your script without clicking the Stop button in Script Editoran unduly harsh way to halt your code.

    The fix? Insert an exit repeat command somewhere in your repeat statement. That way, there'll always be something to trigger your loop to stop.

  • Make sure an item exists before you try to use it. Using the exists command, you can verify that an item does, well, exist. Whenever you're scripting tentative itemssay, a specific Finder window that might not be around tomorrow, or a particular TextEdit documentit's always a good idea to put your commands inside an if/exists statement, so your script doesn't end up sending commands to nonexistent items.

  • Don't convert between incompatible data types. The as command lets you turn one kind of information into anothersay, a number into a string. This is a powerful tool for type-specific commands (like choose from list, which won't accept anything but a list), but be warned: some kinds of data don't respond well to being coerced. Table 14-2 shows the results of the most common coercions.

Table 14-2. What happens when you coerce different data types

Coerce

To string

To number

To list

To record

To alias

From string

No change.

Works fine if you're coercing a quoted number (like "2.5") into an AppleScript number. On the other hand, your script will crash if you try to turn any other string (like "Welp") into a number.

Encases the string in curly braces, creating a single-item list. For example, "Goat hut" would become {"Goat hut"}.

Error

Works fine if you're coercing a string in colon-separated path form (like "Macintosh HD:Applications:") into an alias. However, if you try to convert a nonpath string (like "airplane") into an alias, you'll be left with an error.

From number

Puts the number in quotation marks. 4 would become "4", for example.

No change.

Encases the number in curly brackets, creating a single-item list. For example, 7.2 would become {7.2}.

Error

Error

From list

Concatenates each item from the list, inserting AppleScript's text item delimiters in between. For instance, if the text item delimiters were set to a hyphen, and you converted {"pizza", 4, "me"} into a string, you'd end up with "pizza-4-me".

Works correctly if the list is only one item long, and that item is a number (for example, {5} would become 5). On the other hand, if you try to convert a multi-item list into a number, you'll get an error.

No change.

Error

Works correctly if the list is a single item long, and that item is a colon-separated path (see above). However, if your list is more than one item longor the items aren't file pathsyou'll get an error.

From record

Error

Error

Returns a list of just the values from the record (Section 10.2). For instance, {name:"José", age:33} would become simply {"José", 33}.

No change.

Error

From alias

Returns a string of the alias, in colon-separated form. For example, an alias to your System folder would become the string "Macintosh HD:System:".

Error

Encases the alias in curly braces, much like for strings (see above).

Error

No change.




AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

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