AppleScript Programming/Numbers and strings

From Wikibooks, open books for an open world
Jump to: navigation, search

Variables, Numbers, and Strings: In the last section, the "set" command was used to set a variable, in this case "theVariable" to the string "Support Wikibooks!". In this chapter, you will learn about variables, numbers, and strings.

Variables[edit]

Variables, if you are unfamiliar to programming or algebra, are words that represent some function. For example, in Algebra, the variable x in the equation x-5=1 must equal 6. Variables in AppleScript can be most single letters or any word that is not a command. However, since many normal words are commands in AppleScript, the easiest way to pick a sure-fire variable is to use CamelCase, or to have capitalization insideTheSentence likeThis. Variables are always set using the set command or copy command.

Numbers[edit]

Now let's use the three commands we know to write a simple number-based program. Try putting this into your Script Editor:

set firstVariable to 5
set secondVariable to 3
display dialog firstVariable - secondVariable

The result is a message box displaying the result (2).

Strings[edit]

Numbers are very useful for mathematical computations by nature. However, what if you want a script to tell you text? This is where strings come in. A string is a type of data stored by a variable that contains a simple line or lines of text.

By putting this in your Script Editor, you can say, you're working with String Variables:

set firstString to "Hello, "
set secondString to "User!"
display dialog firstString & secondString

The result is a message box displaying the result (Hello, User!).

Now put:

set firstString to "Hello, "
set secondString to "User!"
display dialog firstString & secondString default answer "3"

Now the user can define the variable.


Next: Lists and records