1 Reply Latest reply: Mar 5, 2008 5:47 PM by red_menace
BenChase Level 1 Level 1 (10 points)
What I am trying to do is make a dialog box with like 6 different fields for input and then take the entered text and write it to a tab delineated .txt document.

I am getting hung up on creating a dialog box with multiple inputs.

Has anyone done something like this before?
  • red_menace Level 6 Level 6 (14,760 points)
    Vanilla AppleScript doesn't have a multiple entry dialog, but you can fake one. The trick is that if the default answer for a dialog contains return characters, then using a return in the edit box doesn't trigger the OK button. The following handler takes a list of item prompts and creates a dialog with the appropriate number of lines in the edit box. You can input the individual items, separated by carriage returns - the handler will return a list of the items. There are a few ways to use the handler, so I have included a couple of examples:

    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 335px;
    color: #000000;
    background-color: #FFDDFF;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    on run -- examples
    set {FIrstName, LastName} to (MultiDialog for {"• First Name", "• Last Name"} with Title and Prompt)
    display dialog FIrstName & space & LastName

    set test to (MultiDialog for {"item 1", "item 2", "item 3", "item 4", "item 5", "item 6"} given Title:"test", Prompt:"enter 6 items:")
    choose from list test with empty selection allowed
    end run

    on MultiDialog for SomeItems given Title:TheTitle, Prompt:ThePrompt
    displays a dialog for multiple item entry (note that a return is used between each input item)
      for each of the input items, a line of text is placed in the dialog and a line of input is set for the input
        the number of items returned are padded or truncated to match the number of input items
    parameters - SomeItems [list]: a list of the input items
    TheTitle [boolean/text]: use a default or the dialog title
    ThePrompt [boolean/text]: use a default or the prompt text
    returns [list]: a list of the input items, or {""} if error
    set {TheItems, TheInput, TheCount} to {{}, {}, (count SomeItems)}
    if TheCount is less than 1 then return {""} -- error

    if ThePrompt is in {true, false} then -- "with" or "without" prompt
    if ThePrompt then
    set ThePrompt to "Input the following items:" & return & return -- default
    set ThePrompt to ""
    end if
    else -- "given" prompt
    set ThePrompt to ThePrompt & return & return
    end if

    if TheTitle is in {true, false} then if TheTitle then
    set TheTitle to "Multiple Input Dialog" -- default
    set TheTitle to ""
    end if

    repeat TheCount times -- set the number of lines in the input
    set the end of TheInput to ""
    end repeat
    set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
    set {SomeItems, TheInput} to {SomeItems as text, TheInput as text}
    set AppleScript's text item delimiters to TempTID

    set TheInput to paragraphs of text returned of (display dialog ThePrompt & SomeItems with title TheTitle default answer TheInput)

    repeat with AnItem from 1 to TheCount -- pad/truncate entered items
    set the end of TheItems to (item AnItem of TheInput)
    on error
    set the end of TheItems to ""
    end try
    end repeat
    return TheItems
    end MultiDialog