CHOOSE FROM LIST

With AppleScript we have the ability to create a user dialog with a single list to choose from. The command choose from list is part of StandardAdditions. Given a list of string values, the user may choose one or more items from the list depending on the value for multiple selections allowed. If the user Cancels out of the dialog, the result is false. Otherwise the item(s) chosen are returned in a list.

set theItems to {"No border", "Rounded Corner", "Standard Corner"}
set thePrompt to "Choose border style from list"
set theChoice to choose from list theItems with title "Border Style" with prompt ¬
thePrompt without multiple selections allowed
if theChoice is false then
   activate
   display alert "User Cancelled"
else
   set itemSelected to item 1 of theChoice
end if

…Choose from List

DIALOG WITH DROPDOWN

A similar dialog can be created using Adobe InDesign’s custom dialog with a dropdown. Here the prompt, the dialog title, and ability to cancel are passed to a handler that creates the dialog.

set theItems to {"No border", "Rounded Corner", "Standard Corner"}
set thePrompt to "Choose border style from list"
set canCancel to true
set theChoice to userDialog("Border Style", canCancel, thePrompt, theItems)
if theChoice is {} then
   activate
   display alert "User Cancelled"
else
   set itemSelected to item 1 of theChoice
end if
on userDialog(dlgName, canCancel, thePrompt, theItems)
   tell application "Adobe InDesign CC 2019"
	activate
	set dlgRef to make dialog with properties {name:dlgName, can cancel:canCancel}
	--establish script preferences
	set origLevel to user interaction level of script preferences
	set user interaction level of script preferences to interact with all
	tell dlgRef
	   --enter widget information here
	   tell (make dialog column) --column
		tell (make dialog row)
		   make static text with properties {static label:thePrompt, static alignment:left align}
		   set choiceDropdown to make dropdown with properties {min width:144, string list:theItems, selected index:0}
		end tell --row
	   end tell --column
	end tell --dialog
	--open the dialog 
	set userResponse to show dlgRef
	if userResponse = true then
	   --record user information here 
	   set borderChoice to item ((selected index of choiceDropdown) + 1) of theItems
	   set theInfo to {borderChoice}
	else
	   set theInfo to {}
	end if
	--close the dialog
	destroy dlgRef
	--reset script preferences
	set user interaction level of script preferences to origLevel
	--if user cancelled theInfo will be an empty list
	return theInfo
    end tell
end userDialog

…Dialog with Dropdown

RADIO BUTTON CONTROL

InDesign’s dialog has a wealth of widgets that can be used for giving the user multiple choices. To provide a single choice within a group of items, the radiobutton control may be the item of choice.

In place of the code in the above that follows –enter widget information here, substitute the following:

--enter widget information here
tell (make dialog column) --column
   make static text with properties {static label:thePrompt, static alignment:left align}
   set myButtonGroup to make radiobutton group
   tell myButtonGroup
	   make radiobutton control with properties {static label:item 1 of theItems}
	   make radiobutton control with properties {static label:item 2 of theItems}
	   make radiobutton control with properties {static label:item 3 of theItems}
   end tell
end tell --column

Then to record the user’s choice, substitute:

--record user information here 
set borderChoice to selected button of myButtonGroup
set theInfo to {item (borderChoice + 1) of theItems}

Notice that in both of the examples above, the actual item chosen from the list is being returned. Optionally, you might want to simply return the index of the item returned since the list is defined at the top of the script. Often, in working with the result of a multiple choice, it is easier to work with the index (integer value) rather than working with the string. Just make sure that you add 1 to the index as AppleScript starts list indexes at 1 instead of zero.

MULTIPLE CHOICES

To allow the user one or more choices, the checkbox control can be used. Adding to the code above we now have a dialog with both radio button and checkbox controls. Notice, in this example that the script returns the index of the radio button chosen.

set checkItems to {"Center Horizontal", "Center Vertical"}
set checkPrompt to "Center Alignment:"
set theItems to {"No border", "Rounded Corner", "Standard Corner"}
set thePrompt to "Choose border style from list"
set canCancel to true
set theChoice to userDialog("Border Style", canCancel, thePrompt, theItems, checkPrompt, checkItems)
if theChoice is {} then
   activate
   display alert "User Cancelled"
else
   copy theChoice to {borderIndex, doH, doV}
end if
on userDialog(dlgName, canCancel, thePrompt, theItems, checkPrompt, checkItems)
   tell application "Adobe InDesign CC 2019"
      activate
      set dlgRef to make dialog with properties {name:dlgName, can cancel:canCancel}
      --establish script preferences
      set origLevel to user interaction level of script preferences
      set user interaction level of script preferences to interact with all
      tell dlgRef
	--enter widget information here
	tell (make dialog column)
	    tell (make border panel)
		make static text with properties {static label:thePrompt, static alignment:left align}
		set myButtonGroup to make radiobutton group
		tell myButtonGroup
		     make radiobutton control with properties {static label:item 1 of theItems}
	             make radiobutton control with properties {static label:item 2 of theItems}
	             make radiobutton control with properties {static label:item 3 of theItems}
		end tell
	    end tell --border panel
	    tell (make border panel)
		tell (make dialog column)
		    make static text with properties {static label:checkPrompt, static alignment:left align}
		end tell
		tell (make dialog column)
		    set doHoriz to make checkbox control with properties {static label:item 1 of checkItems}
			set doVert to make checkbox control with properties {static label:item 2 of checkItems}
		end tell --dialog column			
	    end tell --border panel
	end tell --column
    end tell --dialog
    --open the dialog 
        set userResponse to show dlgRef
	if userResponse = true then
	    --record user information here 
	    set borderIndex to (selected button of myButtonGroup) + 1
	    set doH to checked state of doHoriz
	    set doV to checked state of doVert
	    set theInfo to {borderIndex, doH, doV}
	 else
	    set theInfo to {}
	 end if
	 --close the dialog
	 destroy dlgRef
	 --reset script preferences
	 set user interaction level of script preferences to origLevel
	 --if user cancelled theInfo will be an empty list
	 return theInfo
    end tell
end userDialog

..Radio and Checkbox controls

An advantage of using radiocontrol buttons and checkboxes, is that a value is returned for each widget whether the user clicks on the widget or not. In the above, if the user does not choose a radio button, the value returned will be zero (0); if no checkboxes are checked, their respective values will be false.

UPWARD AND ONWARD

The code required for creating a user dialog can become quite involved, but as you can see from above, it can be relatively simple.

Disclaimer:
Scripts provided are for demonstration and educational purposes. No representation is made as to their accuracy or completeness. Readers are advised to use the code at their own risk.