Data Types

Like most scripting tools, AppleScript is a “loosely typed” programming language. This means that for the most part you do not have to specify exactly how the computer will store some data when you set a variable to a value. AppleScript takes care (or tries to) of the details for you. So when you use the code fragment: [set num to 75], AppleScript knows that num is an integer or number. If you use:

set numstr to "I'm a string"

numstr is automatically stored as a string. This feature does not forbid you from specifying the data type of a variable, which is a good idea in many situations and creates more readable code. If you want to explicitly set a variable to an AppleScript data type, use the as keyword, as in get current date as string. If you want to ensure that a number will be stored as a real data type, use code such as:

set num to 75.0

This code sets the variable num to a real data type, which is a very large number that can include a decimal point, similar to a double type in Java. A program can now increment or increase the variable num to a much higher number than it could if it were left as an integer, which has a range of -536,870,911 to 536,870,911, inclusive. What if you wanted to have a variable keep track over time of the number of people on Earth who are connected to the Web? This number would eventually exceed one billion, so you would want to use a variable of a real data type.

However, explicitly setting data types in AppleScript is also a potentially ...

Get AppleScript in a Nutshell 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.