AppleScript Programming/Advanced Code List/Display Dialog

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

With command "display dialog" you can make the script invoke a sign, that can be used in so many purposes that it would be senseless to list them all. But for least few example, it can be used to note the user, ask for option (yes/no), prompt for user input, and so on.

In its simplest form, it is like this:

display dialog "Hello!"

Which will give result like this:

When considering from the view of complexity, it is very opposite of this code...

set my_pass to text returned of (display dialog "Enter password for " & ¬
	quoted form of my_name ¬
	with title "LiveJournal Post Event" ¬
	with icon stop ¬
	default answer "" ¬
	buttons {"Continue…"} ¬
	default button 1 ¬
	with hidden answer ¬
        giving up after 5)

... and this window:

NOTE: For the script above to work, you need to set variable "my_name".

  • I will introduce every piece of the code shown in the second example, so that you will have more insight about what you can do.
set myVariable to text returned of (

When you start display dialog-command with this, you will invoke dialog-window with input prompt. Just remember to add the ")" at the end, which I left out from above example, so that you won't be trying to make it like ... return of () display dialog ....

quoted form of myVariable

With this part of command you can put "single-dot"-quote around the variable, instead of "two-dot"-quote, which is not possible with \', while \" can be used to add quotes into the dialog-text.

with title "Hello punk!"

This will set a title for the dialog-window.

with icon stop

This will simply add the stop-sign into dialog-window.

default answer ""

When you use the input-prompt, this will insert sort of "default" into the input-field. If it's "" as like in the example, there will be nothing in the input-field. But if it's "1", then there will be number 1 "pre-written" (and selected) in the input-field.

NOTE: Adding this to display dialog will also create input-prompt.

buttons {"Uh, no.","Continue…"}

With this you can determine the amount of buttons and what they have as text. Without this, there will be default buttons "Cancel" and "OK".

NOTE: Without determining the button names, you cannot do "if button returned of result is "OK"".

default button 1

With this you can determine which button is "selected" in the dialog-window. If this code is not included while you set your own buttons (with buttons {}), no button will be "selected", and pressing enter won't close the dialog - forcing user to click one of the buttons. Button order is determined from "left to right", so button number 1 is first button from left.

with hidden answer

This will make the text written into the input-field appear as balls, resembling "••••". There is no need for any sort of "with visible answer"-code.

giving up after 5

With this you can limit the time the dialog-window is displayed. If this code is not included, the window can only be dismissed with the buttons (or the "return" key for the default button). When included, a separate "gave up:true" or "gave up:false" is returned as a sub-field of the result.

You can break out the separate return sub-field values using something like this:

   set theReturnedItems to (display dialog "How are you?" default answer "Example" buttons {"Quit", "OK"} default button 2 giving up after 4)
   set theAnswer to the text returned of theReturnedItems
   set theButtonName to the button returned of theReturnedItems
   set theGaveUpState to gave up of theReturnedItems as string

Since the "result" can change at any time, it is important to preserve its parts in a local variable right away. You can not use the "set my_pass to text returned of" before the (display dialog ...) because that alters the result. You must preserve the entire result because you may need to extract separate sub-fields.

  • That's it, you know as much about the "display dialog" as the original writer at the moment he wrote this.

END NOTE: All text that I have written (with exception of code) are mine, spontaneously formed and written. Equalities to any copy-written source is pure coincidence.