display dialog with multiple text boxes?

Is it possible to create “display dialog”, that asks multiple questions in one dialog? I need to input only text to text-fields.

Thanks

cirno,

How about this.

display dialog "Please answer these questions. Separate your answers with a comma please." & return & "How old are you?" & return & "How much do you weigh?" default answer ""
set theAnsr to text returned of the result
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set ansr1 to first text item of theAnsr
set ansr2 to second text item of theAnsr
display dialog ansr1
display dialog ansr2
set AppleScript's text item delimiters to ASTID

I don’t know how many items you need input for, but this should be a start. The last two display dialogs are for checking the results.

PreTech

Since this type of question seems to appear every now and then, I’ve dropped a suggestion in the Code Exchange section under the heading:
A single dialog to handle multiple entries.

I hope it’s the kind of thing you’re looking for… :slight_smile:

This code is no longer available. If you still have, could you post it again.

Thanks,

Jason

A single dialog to handle multiple entries

A question that seems to crop up from time to time goes something like this: “Can I create a dialog to take more than one entry in the same panel?” (In fact, one or two examples appeared here quite recently.)

Typical advice might range from building a custom dialog in AppleScript Studio/Xcode to using a third-party scripting addition (OSAX) - such as 24U Appearance. All worthy suggestions since, depending on exactly what’s required, most of the proposed solutions offer a variety of benefits. But they could also involve an additional learning curve, cost or complication that the enquirer may prefer to avoid - especially if the immediate aim is not overly ambitious.

Often, the purpose is merely to produce a dialog that takes a list as input. Ideally, this would allow the user to enter the list in a single panel, review it in context, edit items as required - and then, finally, confirm the result. Such a feature might be useful as a front end for entering data into some kind of database, or simply as a means of jotting down a general-purpose list. What a pity it can’t be achieved with vanilla AppleScript…

Or can it?

The script below shows a way to extend a regular dialog so that it accepts multiple line entries. It then goes on to output entries - formatted either as text or as a list. Of course, the trick here is not really in the script - but in Standard Additions, which accepts the insertion of return characters in a dialog’s default answer. However, the script demonstrates how the input and output could be formatted - and the multi_dialog handler might be handy for creating a series of multiple-entry dialogs more easily.

The following notes may help:
Input error checking
Input error checking, which would normally depend on individual circumstances anyway, is minimal. (This is left as an exercise for the reader.)

Input formatting
Single or multiple prompts can be entered in a prompt list to appear in the dialog either as standard text, separate questions/items - or a combination of both.

The number of entry rows displayed in a dialog can be set either implicitly (according to the number of prompts listed), or explicitly (by an integer inserted at the beginning of the prompt list).

If a string or a single-item list (with no integer to define the number of entry rows) is passed to the multi_dialog handler, the resulting dialog will behave pretty much like a standard dialog.

Output formatting
The resulting lists can be configured so that they are of either fixed or variable length:

Fixed-length lists
A fixed-length list is useful for situations where output is intended to populate a defined target range of cells or fields - so the length of the resulting list will remain unaltered, regardless of user input. If the entries in the list fall short of the expected number, the list will be padded with an appropriate number of trailing, empty strings. If the expected number is exceeded, the list will be truncated accordingly.

Variable-length lists
The variable-length option could be handy for producing lists of indeterminate length (such as shopping or to do items). In this case, the length of the output is dictated by the user. Variable-length lists are defined by inserting a zero integer at the beginning of the prompt list.


User input
Use a separate line for each answer/item. To add a new line in a dialog, simply press the return key. To confirm a set of answers/items, either press the enter key - or click OK.
The script, which includes a few syntax examples, also demonstrates how the size of a dialog's entry panel is defined by the prompt list:
Open this Scriplet in your Editor:
on multi_dialog for q given listing:l
   tell q as list
       set {a, b, c} to {{}, beginning, count}
       set v to b's class is integer
       if v then set {c, v, q} to {b, b is 0, rest}
   end tell
   if v then set c to 30
   repeat c times
       set a's end to ""
   end repeat
   set {d, text item delimiters} to {text item delimiters, return}
   set {q, a, text item delimiters} to {q as string, a as string, d}
   set r to (display dialog q default answer a)'s text returned
   if (count r) is not 0 then
       if not v then tell r & a
           if l then return paragraphs 1 thru c
           return text 1 thru paragraph c
       end tell
       tell r to repeat with i from (count paragraphs) to 1 by -1
           if (count paragraph i) is not 0 then
               if l then return paragraphs 1 thru i
               return text 1 thru paragraph i
           end if
       end repeat
   end if
   if l then return {}
   ""
end multi_dialog

--------------------------
-- demonstration --
--------------------------

property l : {"1] Name: {items: 2, length: fixed, output: list}", ¬
   "2] Address: {items: 3, length: fixed, output: list}", ¬
   "3] Reasons: {items: 6, length: fixed, output: list}", ¬
   "4] ToDo List: {items: ?, length: variable, output: text}", ¬
   "5] Standard: {items: 1, length: fixed, output: text}"}

property d : l's item 1

tell (choose from list l default items d with prompt "Choose a multi-dialog demonstration:")
   if it is false then error number -128
   set d to it
   set i to item 1's item 1 as integer
end tell

-----------------------------
-- syntax examples --
-----------------------------

if i is 1 then
   multi_dialog for {"¢ What is your first name?", "¢ And your last name?"} with listing
else if i is 2 then
   multi_dialog for {3, "Please enter your:", "", "1: house number & street name", "2: city/area", "3: zip code"} with listing
else if i is 3 then
   multi_dialog for {6, "List up to six reasons to be cheerful:"} with listing
else if i is 4 then
   multi_dialog for {0, "To Do list for " & (current date)'s weekday & ":"} without listing
else
   multi_dialog for "Did you *really* just want a standard dialog?" without listing
end if

Last edited by kai (2005-11-26 11:30:12)
kai

Re: A single dialog to handle multiple entries
Thanks for that, kai. A very useful weapon for the armoury!

The information about ‘display dialog’ and returns is very interesting. In Tiger, at least, if a default answer is specified for ‘display dialog’, and that default contains a return, then the return key on the keyboard will enter returns into the text field. Otherwise, it does its normal thing of dismissing the dialog via the default button, if there is one. I’ve had a few instances of the return key failing to dismiss dialogs on my Tiger machine recently, which I’ve been assuming to be due a Tiger instability. Naturally, I haven’t been able to reproduce the behaviour today, but if it happens again, I’ll see if it’s anything to do with this.

In Jaguar, it’s not possible for the user to enter returns into the text field. The return key always tries to dismiss the dialog with the default button.

Last edited by kai (Yesterday 04:30:12)

#3 2005-11-27 11:48:02
kai

Re: A single dialog to handle multiple entries
Nigel Garvey wrote:
In Tiger, at least, if a default answer is specified for ‘display dialog’, and that default contains a return, then the return key on the keyboard will enter returns into the text field.
Right - the above script relies on that very feature.

Otherwise, it does its normal thing of dismissing the dialog via the default button, if there is one.
Yeah. By contrast, regardless of what the default answer contains, the enter key should dismiss any dialog (again, assuming there’s a default button).

I’ve had a few instances of the return key failing to dismiss dialogs on my Tiger machine recently, which I’ve been assuming to be due a Tiger instability.
So far, I’ve noticed this only when the default answer contains a return character - but I’ll also watch out for any exceptions.

In Jaguar, it’s not possible for the user to enter returns into the text field. The return key always tries to dismiss the dialog with the default button.
I think this feature was added in Panther. (If I’m mistaken, perhaps an obliging Panther user can correct me…)

Last edited by kai (Yesterday 04:30:12)
Don’t you ever sleep?
What - and miss all this fun???
kai

I saved this to my computer so there may be some extraneous info from the web page in here, but this is the info from that link. This is actually several posts by kai and Nigel Garvey

PreTech

:slight_smile: PreTech thank you so much for this contribution.

I must be missing something really basic but I am struggling to get to the output of the dialogue…
How do I get what the user input into separate variables ?

I think this is done already by the script but how do I access those variables ?

thank you for your help,

Daniele M.

Hi Daniele. Welcome to MacScripter.

I presume you’re referring to post #5 above, in which PreTech reposted an entire thread which had gone missing from Code Exhange sometime during 2006. The script and preamble are actually by the late Kai Edwards.

In AppleScript, if you have a list (or a handler which returns one), and you know how many items the list contains, you can set a list of an equal number of variables to it:

set myList to {"cat", "dog", "aardvark"}
set {a, b, c} to myList

return b --> "dog"

I see Kai’s handler uses the same technique internally. :slight_smile: The script returns lists of different lengths — or even just single strings — depending on how the input’s specified. For instance, the “syntax examples” at the end of the script could be:

if i is 1 then
	-- Input is a list containing just two prompts, so a two-text list is returned.
	set {firstName, lastName} to multi_dialog for {"• What is your first name?", "• And your last name?"} with listing
else if i is 2 then
	-- Input is a list beginning with the integer 3, so a three-text list is returned.
	set {streetAddress, city, zip} to multi_dialog for {3, "Please enter your:", "", "1: house number & street name", "2: city/area", "3: zip code"} with listing
else if i is 3 then
	-- Input is a list beginning with the integer 6, so a six-text list is returned.
	set {reason1, reason2, reason3, reason4, reason5, reason6} to multi_dialog for {6, "List up to six reasons to be cheerful:"} with listing
else if i is 4 then
	-- Input is a list beginning with the integer 0, so just text is returned, not a list.
	set todoList to multi_dialog for {0, "To Do list for " & (current date)'s weekday & ":"} without listing
else
	-- Input is just a prompt, so just text is returned, not a list.
	set theReply to multi_dialog for "Did you *really* just want a standard dialog?" without listing
end if

By the way, although the thread apparently disappeared from Code Exchange in 2006, kai’s original post reappeared as an unScripted article in May 2007 and started a new thread there.