Lesson 2 - displaying dialogs - Applescript
In the last lesson, you learned how to comment and name variables. In this lesson, you will learn to display a dialog, how to display a dialog with arguments, and how to display dialogs with variables in them.
Enter the following code into AppleScript Editor and run it:
Enter the following code into AppleScript Editor and run it:
display dialog "Hello, world!"
Congratulations! You just created your first dialog in AppleScript! Now let's build on it.
Enter the following code into AppleScript Editor and run it:
Enter the following code into AppleScript Editor and run it:
#My third AppleScript app
display dialog "Hello, world!"
Notice how the program doesn't change? This is because the comment is ignored.
Now we will display a variable with our dialog.
Enter the following code into AppleScript and run it:
Now we will display a variable with our dialog.
Enter the following code into AppleScript and run it:
#My fourth AppleScript app
set theVariable to "Hello, world!" #Define theVariable
display dialog "" & theVariable #Display nothing and theVariable
Now we have displayed our variable in a dialog. Since AppleScript requires quotations after a "display dialog" command, we have to put "" before our variable.
Next we will discuss the phrases that can modify the "display dialog" command. The first one we will discuss is "buttons".
Enter the following code into AppleScript and run it:
Next we will discuss the phrases that can modify the "display dialog" command. The first one we will discuss is "buttons".
Enter the following code into AppleScript and run it:
display dialog "Hello, world!" buttons {"1", "2"}
You will see that the two buttons that we are used to seeing - "Continue" and "Cancel" - are gone and have been replace with "1" and "2". It is important to understand that the brackets NEED to go around the button names if there are more than one button.
Now let's discuss "default button".
Enter the following code into AppleScript and run it:
Now let's discuss "default button".
Enter the following code into AppleScript and run it:
display dialog "Hello, world!" buttons {"1", "2"} default button 1
This argument tells AppleScript to set the first button in the list to be the default button.
We will now learn about the "cancel button" argument. This is VERY much like the "default button" argument.
Enter the following code into AppleScript and run it:
We will now learn about the "cancel button" argument. This is VERY much like the "default button" argument.
Enter the following code into AppleScript and run it:
display dialog "Hello, world!" buttons {"1", "2"} default button 1 cancel button 2
This code now sets the cancel button to the second button listed. Press "2" while running the app and you will notice that the result is "error 'User canceled.' number -128". This means the application quit because of a user signal. This is an easy way to have an "Exit" or "Quit" button in your app without worrying about exiting loops. Loops will be discussed in another lesson.
Now it is time to learn about "default answer".
Enter the following code into AppleScript and run it:
Now it is time to learn about "default answer".
Enter the following code into AppleScript and run it:
display dialog "Hello, world!" buttons {"1", "2"} default button 1 cancel button 2 default answer ""
This gives the user a place to put input other that buttons. If you keep "default answer" blank by using "", then there will be a blank input box. If this were changed to "My first answer", then "My first answer" would be displayed in the input box.
Now we will go over the "with hidden answer" option.
Enter the following code into AppleScript and run it:
Now we will go over the "with hidden answer" option.
Enter the following code into AppleScript and run it:
display dialog "Hello, world!" buttons {"1", "2"} default button 1 cancel button 2 default answer "" with hidden answer
All this does is hide the input of the text box.
Now we will go over aesthetic changes to the dialog window. We will now see an example of a "with title" argument.
Enter the following code into AppleScript and run it:
Now we will go over aesthetic changes to the dialog window. We will now see an example of a "with title" argument.
Enter the following code into AppleScript and run it:
display dialog "Hello, world!" buttons {"1", "2"} default button 1 cancel button 2 default answer "" with title "My AppleScript Program" with hidden answer
Simply enough, this adds a title to the top of the dialog window.
Next is "with icon". "With icon" has 4 options: "stop", "caution", "note", and a path to a custom ".icns" file.
Enter the following code into AppleScript and run it:
Next is "with icon". "With icon" has 4 options: "stop", "caution", "note", and a path to a custom ".icns" file.
Enter the following code into AppleScript and run it:
display dialog "Hello, world!" buttons {"1", "2"} default button 1 cancel button 2 default answer "" with title "My AppleScript Program" with icon note with hidden answer
You will notice that this causes the "note" icon to display on the left of the dialog. Play with this as you will but this will not be discussed more in this tutorial.
Let's put this all together!
Enter the following code into AppleScript and run it:
Let's put this all together!
Enter the following code into AppleScript and run it:
set typeOfDay to "good" #set variable
display dialog "Hello, world! Today is a " & typeOfDay & " day!" buttons {"1", "2"} default button 1 cancel button 2 default answer "" with title "My AppleScript Program" with icon note with hidden answer #display variable and dialog with options
Congrats! You just created a program that uses everything in the past two lesson.
In the next lesson, you will learn to use operators in AppleScript.
In the next lesson, you will learn to use operators in AppleScript.