hard for a computer. So, AppleScript relies on clues to help it understand the meaning of each
element of a statement of a script. For that reason, we have to put a string between double
quotes. Otherwise, AppleScript could mistake a string for a variable name. Check out the
following script :
set stringToBeDisplayed to "Hi there!"
display dialog "stringToBeDisplayed"
display dialog stringToBeDisplayed
Run the script yourself to see what it does. Statement [6.2] displays the
‘stringToBeDisplayed’, whereas statement [6.3] displays ‘Hi there!’ Because the Script Editor
shows compiled scripts in color, it is easy to see that while in statement [6.3]
‘stringToBeDisplayed’ is a variable name, for which reason it is shown in green, in statement
[6.2] ‘stringToBeDisplayed’ is shown in black, indicating that this word is data (a string). At
times, the colorful formatting will help you to track down bugs quicker.
As stated before, AppleScript needs clues to decipher the English-like script into something a
Mac can understand. Here is another example of why these clues are important: If we write
‘thirty’ as a number between double quotes, i.e. as “30”, it is not a number anymore but a
string. Recognizing the data type is very important, because some operations can only be
performed on a specific data type. For example, while you can divide two numbers, you can’t
divide a string by another string. Let’s look at a couple of operations that can be performed on
Like numbers (and voters), strings can be manipulated. You can glue strings together, which
operation is called concatenation, using an ampersand .
set nameOfActress to "Julia"
set actressRating to "very pretty"
set resultingString to nameOfActress & " is" & actressRating
display dialog resultingString
In the third statement [7.3], we concatenate three strings, two of which are referred to by
Please note that the number of spaces between a string and an ampersand is of no
consequence for the resulting string contained by the variable ‘resultingString’. After compiling,
the Script Editor reduces that number of spaces to 1 if you had added more than one. If you
need one or more spaces to separate the words of the sentence to be displayed,
have to provide them
between the double quotes of a string. In statement [7.3], apart from
the space left of the word ‘is’ , there should have been another space next to the ‘s’ of the
word ‘is’ .
There are more commands acting on strings available. Some of them require stuff we will
cover in later chapters, so we will leave that for the time being. But we can give you one other
example of a command relating to strings. You can ask for the length of a string .