Basics of AppleScript

Button Pressed

Script [7.1.1]:

set myName to "Nayan Seth"
display dialog myName buttons {"Cancel", "Ok"} default button 2

Explanation: This will display a dialog with contents of myName and 2 buttons Cancel and Ok.

Figure 7.1.1

Figure 7.1.1 Dialog Output

However how do I find out about the button which was pressed. Let’s modify the Script

Script [7.1.2]:

set myName to "Nayan Seth"
set dialogName to display dialog myName buttons {"Cancel", "Ok"} default button 2
set buttonName to button returned of dialogName
display dialog "You pressed " & buttonName

Explanation: Here we have the same variable myName but instead of displaying dialog directly, I assign the dialog to a variable named dialogName.

I create another variable buttonName. This will return the value of button which was pressed by dialogName. Then I display a dialog which prints the value of button pressed by the user.

Figure 7.1.2

Figure 7.1.2-2

Figure 7.1.2 Button Pressed

Dialogs can display numbers and short strings. They cannot display lists. However they can be used to take user input.