Applescript is a basic coding language that is great for beginners. To start using AppleScript open the “Script Editor” Application on your mac (You cannot use AppleScript on a Windows PC.)
Once you have it open you can make your first program in AppleScript by typing
1 |
beep |
in the code box. Click the button to run the code. You should hear your computer beep. If not, make sure your computer isn’t muted.
Variables
A variable is a piece of information that your program stores, for example, the score in a game.
Types of Variables
In most programming languages, there are 4 types of variables:
String
A string is a piece of text. You must enclose your string in quotes like as shown below:
1 |
set myString to "Hello World" |
Integer
An integer is a whole number. You do not need to put in quotes.
1 |
set myInteger to 21 |
Float
A float is a number that can have decimals. You do not need to put in quotes.
1 |
set myFloat to 3.14 |
Array
An array is a list of variables. It can have strings, integers, and floats, It can have multiple types of variables.
1 |
set myArray to ["Hello","Goodbye", 7.9, 5007] |
Logic
To create logic we use an if statement.
1 2 3 4 5 6 |
set score to 58 if score > 50 say "You scored more than 50 points." else say "You scored less than 50 points." end if |
More Commands
Speak
1 |
say "Put the text you want your computer to say here, Make sure it's in quotes!" |
Popup Box
1 |
display dialog "Put the text you want a popup to say here! Again, Make sure it's in quotes!" |
The text must be in quotes because it is a string. You can also make a variable for the string, as shown below.
1 2 |
set messageText to "Hello World" display dialog messageText |
Remember that the variable name does not need to be in quotes.
Repeat
1 2 3 |
repeat say "I Will Repeat This Sentence Until You Press The Stop Button" end repeat |
Popup Box With Text Box
1 |
display dialog "What is your name?" default answer "" |
Getting The Answer
This will say your name when you click “OK”
1 2 3 |
set x to display dialog "What is your name?" default answer "" set yourName to the text returned of x say yourName |
How it Works
We create a variable called x, but instead of making it a string, float, or array, we make it a popup box.
Then we create a variable called yourName and set it to the text returned of x.
Finally, we use the say command to speak yourName.
Adding Custom Buttons
You can add custom buttons to your dialog, by adding the buttons attribute, with an array of strings (The button names).
1 |
display dialog "Chocolate or Vanilla?" buttons ["Chocolate", "Vanilla"] |
You can also use a variable for the buttons array:
1 2 |
set flavors to ["Chocolate", "Vanilla"] display dialog "Chocolate or Vanilla?" buttons flavors |
Comments
Comments are text in you code that your computer will not read. They are there to help you, and others understand the code.
To create a comment in AppleScript, Just type “–” (without the quotes) before your comment.
1 |
--This is a comment |
Making a Simple Game
This game uses many of the topics above. I added comments to the code to help you understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
--Create the "score" variable, and set it to the integer 0 set score to 0 repeat --Create the "msgBox" variable, and set it to a popup box set msgBox to display dialog "Your Score: " & score buttons ["Give Up", "Add 10 Points"] --Create the "btnClicked" variable, and set it to --the text of the button that was clicked (A String) set btnClicked to button returned of msgBox --If the player clicks "Give Up", congratulate them --and exit the program with the "return" command if btnClicked = "Give Up" then say "Good Job! You Scored " & score & "Points!" return end if --Add 10 to the score set score to score + 10 end repeat |
1 Comment
MAMA'S LITTLE ANGEL KEEPER · May 17, 2018 at 2:30 PM
TYLLLLLLLLLLEEEEEERRRRRRRR . KKKKKKAAAAAAAAAANNNNNNOOOOOOXXXXXXXXX!!!!!!!!!!!! YOU ARE A HOMIE AND I WILL MISS YOU WHEN YOU GO TO HIGH SCHOOL. I HOPE THAT YOU KNOW THAT YOU MADE HTM A BRIGHTER PLACE WHILE YOU WERE HERE. PLEASE CONTINUE TO SHARE YOUR INNER LIGHT WITH EVERYONE!
-NAMASTE