Cooking with Data Types - Part 1

I want to get back to the original purpose for this column: teaching beginners how to write AppleScripts. With that in mind, let’s get started.

This month, we’re going to explore data types. Data types are simply the form in which information is returned from AppleScript commands. They are important because they are the key ingredients in any AppleScript recipe. As an AppleScript chef, of sorts, you’ll need to know what sort of ingredients are called for in any given dish. If you use the wrong ingredient, your souffle falls or your cake might taste downright bad. Passing improper data types is another key mistake most beginners make which creates great frustration in an otherwise talented chef.

For example, when using AppleScript to place an image into a picture box in InDesign, you’ll need to know what sort of data you need to pass to InDesign within the command so it can properly execute the command. This data must come in a particular form, or data type. If the data passed to InDesign in this command isn’t in the proper form, your script will throw an error stating “… unexpected type”. If a command requires a file reference, it wouldn’t do any good to pass a boolean value of true or false.

A general list of possible data types includes:

  • integer
  • real
  • string
  • list
  • record
  • boolean
  • reference
  • date

Most all of the information you’ll work with in your AppleScript recipes will come in one of these forms or another. Let’s go down the list and I’ll give you some examples.

First in our list is integer. Easily enough, this data type is simply a standard number like 3, 76, 983. The same goes for a negative number like -3, -98, etc.

Our second data type is real. As in a ‘real number’. If you remember your grade school math classes, a real number is just another name for any number expressed in decimal form. As in 3.4, 8.0, 62.03 and so on.

Third on the list is a string. A string can also be referred to as ‘text’ and they are used and recognized as exactly the same thing. When I first started teaching myself AppleScript, the word ‘string’ was somewhat new to me (not having learned a programming language before). So I usually used ‘text’ as my name for this data type as it made a bit more sense to me. But just remember they’re interchangeable. A string is text and text is a string.

A string is simply a series of characters. As in “my dog”, “sometext” or even “98jtiho+_?”. The key here is, however, that a string is specifically expressed between quotes. These quotes indicate that said series of characters is just a string.

Next we have our list data type. This label is aptly chosen and simply implies that anything in list form is a series of multiple values. There are a couple of key traits that every list will have: curly braces and commas separating the items in that list. For example:

Two interesting traits of a list are that it containsany of the other data types and it can do so with any mix of types. For example, it can be a list of integers, a list of strings, even a list of lists or a list of strings and reals. The delicious possibilities are endless. See the example below for a mixed type list.

A record is a variation on the list data type. Think of records as lists with labels. The information they contain is just like a database record where each entry has a label accompanied by a value. In any given AppleScript meal, you might partake of records in one of two forms. You can make up your own records to handle information like the example below.

Alternatively, you will more often taste records in the form of a given object’s properties. See the next illustration for an example of what you might call a ‘naturally occurring record’.

As you can see in the example above, the properties of my arbitrarily chosen file within the Finder is returned in my Script Editor’s results window in the form of a record.

A very important and powerful lesson in regards to this entree we call a record is that we can retrieve information from a record in an amazingly simple way. In the next two illustrations, I’ve shown how we can get just what we want out of a large record and do it with ease. The first screenshot shows the same sort of simple script as our first record example.

In step two, we’ve added ‘get creator type of theProps’. All this means is that we can retrieve information from anywhere in the record simply by asking for its label. In this case, the creator type entry within this record. See the screenshot below. (Remember, what you see in the result pane is what was returned from the last command of the script.)

You will likely have encountered our next data type, boolean, on more than one occasion. This is perhaps the simplest of all the data types. A boolean is just a value of true or false. See below for an example using boolean values.

Our data type list also includes reference. This term is very broad and means something unique to each application you might command along your AppleScript dining experiences. If you’re scripting Quark XPress, a reference might mean ‘picture box 1 of document 2’. The illustration below demonstrates what a reference means to the Finder.

Last on our list is the date data type. A date is expressed with the word ‘date’ in front of a string in a very particular form, as shown in the result pane of the example below.

The date string is, of course, very useful for determining time. The example below shows how you can use it to figure an arbitrary date in the future and act upon it within your script.

This concludes your crash-course cooking class on data types. Next time, we’ll expand on the data types concept and venture into coercing where I’ll show you how to magically change one form of data into another with the power of the word ‘as’.

Thanks for reading!

I know this an old post, but was very helpful! Thanks

How do you determine the data type of an existing variable if you do not already know what type is?

I think a verb in your question is missing so it’s hard to understand what you mean