2.4. Basic Data Type Conversions

AppleScript allows you to freely store different types of data in your variables. As a result, it is good programming practice to always know what type of value you have stored in a variable at any point in time. Does the variable contain an integer, a real, a string, or perhaps a date? The answer to this question determines how you can use the variable in expressions and pass its value as an argument to a command.

Some AppleScript commands automatically convert values for you. For example, the display dialog command displays the string specified after the command in a dialog:

display dialog "Programming in AppleScript is fun."

What you may not know is that the following works as well:

display dialog 100

and produces the output shown in Figure 2-9.

Figure 2.9. Figure 2-9

Here, the display dialog command sees an integer as its argument and, realizing that, converts it to a string to be displayed. The display dialog command does the conversion for you. Although that may work in this case, in similar cases with other commands, it may not. It's usually better to know what types of values your commands expect and not to rely on the command to do the conversions for you. If you make sure that you provide the correct types, you can avoid problems that might occur when a command cannot or does not perform the correct conversions for you.

2.4.1. Coercing ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.