2009-07-16

Display Dialog with Dropdown Menu of Array Elements

Another way to display an array
(How's that for alliteration?)
A recent post dealt with displaying a dialog box with a checkbox for every array elements. That works if you want to select more than a single entry in the array. But many times you only need the option to choose a single array element. In that case the GUI should include a dropdown menu or a set of radio buttons. This generic ExtendScript function takes an array of elements and a prompt string and constructs and displays a dialog box allowing the user to select an element of the array.
The function returns not the array element but the index in the array of that element.
Note, if you want the items sorted then use the .sort() method on the array prior to passing it to the function.
If the user doesn't select an array element or clicks cancel the function returns a -1 value. Note, in JavaScript an array can't be referenced with a negative index. But in ExtendScript, if you have a collection, instead of a true array, then -1 is the last item in the ExtendScript collection.
This function was written for a script that allows the user to select text in Adobe InDesign or Adobe InCopy and toggle the Bold or Italic font styles with a keyboard shortcut. The script is quite nice because nothing is hard coded. Instead a site installs the pieces, assigns the keyboard shortcuts, and then uses it. The first time they use it, it displays a dialog asking what Character Style to apply to the selected text. Any future time the same type of text is selected, the script will know what Character Style to apply. It works great. That script requires Character Styles when a normal Bold or Italic font style won't suffice such as when you not only apply a bold, but you actually switch fonts. This script is for sale to any site that needs it, just send a e-mail to eps@electronicpublishingsupport.com
A separate link to toggleStyle() will be setup eventually.

//
function selectArrayElementViaDropdown(lst,prmt) {
//-----------------------------------------------------------
//-- S E L E C T A R R A Y E L E M E N T F R O M D R O P D O W N
//-----------------------------------------------------------
//-- Generic: Yes for current versions of Adobe Products
//-- that support custom dialog boxes
//-----------------------------------------------------------
//-- Purpose: Displays a dialog a single dropdown menu of
//-- choices passed into 'lst'.
//-----------------------------------------------------------
//-- Parameters: 2
//-- lst: An Array of items to display to the user
//-- prmt: A string prompt to ask the users what to do.
//-----------------------------------------------------------
//-- Returns: index of chosen item or -1 if they clicked cancel
//-----------------------------------------------------------
//-- Calls: Nothing.
//-----------------------------------------------------------
//-- Sample Use:
//~ var pStyleNames = ['Body bj', 'Body Ragged brr', 'Body Wire bw']
//~ var selectedStyle = selectArrayElementViaDropdown ( pStyleNames , 'Choose the Paragraph Style to apply to the body of this story' ) ;
//~ if ( selectedStyle >= 0 ) {
//~ var theStyleNameToUse = pStyleNames[selectedStyle]
//~ // Do something with the style name here
//~
//~
//~ }// End of the if block
//-----------------------------------------------------------
//-- Written sometime in 2009 by Jon S. Winters
//-- eps@electronicpublishingsupport.com
//-----------------------------------------------------------
//
var listDialog = app.dialogs.add({canCancel:true});
with (listDialog) {
with (dialogColumns.add()) {
with (dialogRows.add() ) {
// show prompt
staticTexts.add({staticLabel:prmt});
}
with (dialogRows.add() ) {
//show the list
var userChoice = dropdowns.add({stringList:lst})
}
}
}
//-- Show the dialog
var listResult = listDialog.show() ;
//
if (listResult) {
return userChoice.selectedIndex ;
}
else {
return -1 ;
}
}
//

No comments:

Post a Comment