2009-07-17

Display Dialog with Radio Buttons of Array Elements

The Final Installment in Selecting an Array Element

Radio Buttons are the third user interface option when it comes to selecting items from a list. Like a Drop Down Menu, a user is only permitted to select a single choice. When the number of items to select from is limited, it is easiest to select from a set of radio buttons.

All three versions of Array Element selection presented have some unique feature. The checkbox version had a method of controlling how many items would appear in a column. That could be incorporated here. This function has the ability to display an optional script version and discusses that in the scripts comments. This version, as do all the others, could actually accept an Adobe InDesign or Adobe InCopy collection. A collection is a superset of an Array object and collections have properties other than .length. For example if you wanted to get the name of all the Master Pages in a document...
var mps = app.documents[0].masterSpreads.everyItem().name
You could also just refer to the name of one item in the collection using the code within the function below. Just read the embedded documentation.


//
function chooseFromList( lst , prmt , dflt ) {
//-------------------------------------------------------------------------
//-- C H O O S E F R O M L I S T
//-------------------------------------------------------------------------
//-- Generic: Yes for any version of Adobe InDesign or Adobe InCopy
//-- that can display a custom dialog.
//-------------------------------------------------------------------------
//-- Purpose: Displays a dialog with a series of radio buttons with
//-- choices passed into 'lst'.
//-------------------------------------------------------------------------
//-- Returns: index of chosen item or -1 if they clicked cancel
//-------------------------------------------------------------------------
//-- Parameters: 3
//-- lst: An array of things that can be coorersed into strings
//-- prmt: a string that will be used to instruct the user what they
//-- should select. Something like ; 'Select a Paragraph Style:'
//-- dflt: a
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var a = [2, 4, 6, 8, 'who', 'do', 'we', 'appriciate']
//~ var daChoice = chooseFromList ( a , 'Select Something:' )
//~ if ( daChoice != -1 ) alert ( 'You picked: ' + a[daChoice] )
//-------------------------------------------------------------------------
//-- Written by Jon S. Winters on 2008.12.24
//-- Edited: 2009.07.17 to provide better comments.
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- Version 1.1: Force Adobe InDesign to display dialogs. Without the
//-- line below the dialog may not appear on some systems.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL ;
//-- Assign a default default choice if one isn't passed;
dflt = dflt || 0 ;
//-- If the default value is less than 0, reset to zero.
//-- This was initially done because this function was created for
//-- a script where the user would get the dialog multiple times
//-- throughout the course of the day and the persistent
//-- target engine would pass as default the last choice
//-- but if a user cancels, then the last choice will be -1.
dflt = (dflt <>
//-- Standard way to make a new dialog with a prompt and a cancel button.
var listDialog = app.dialogs.add({canCancel:true, name:prmt}) ;
//-- Setup the buttons array which will be filled as the dialog is
//-- constructed.
var buttons = new Array () ;
//-- Go through the processes of setting up the areas of the dialog
//-- consult the Adobe Documentaion if you really want to know
//-- what is going on here. Otherwise, just use it.
with (listDialog) {
with (dialogColumns.add()) {
var userChoice = radiobuttonGroups.add()
with ( userChoice ) {
//-- Loop through every item of the list. Note, if the list
//-- is really a collection of Adobe InDesign or Adobe
//-- InCopy objects, they will have .name properties.
//-- Thus, you can add .name in the staticLabel. See
//-- the commented out version.
for ( var loopIndex = 0 ; loopIndex <>
//-- Below for standard Array of Elements that can be
//-- displayed as a string. Note the conversion to
//-- the String object. This will be necessary for
//-- list elements that don't have strings.
buttons[loopIndex] = radiobuttonControls.add( {staticLabel:String(lst[loopIndex]) } );
//-- Below version will allow you to see the name of a item
//-- in a collection.
//~ buttons[loopIndex] = radiobuttonControls.add( {staticLabel:lst[loopIndex].name } );
} //-- end of for loop
//-- Note, the next thing is interesting. We are currently in
//-- a with (radionbuttonGroup) statement. That has a
//-- selectedButton property. And now that all the
//-- buttons have been added, the selected buttton
//-- can be set to the default value.
selectedButton = dflt ;
} ///-- end of with radiobuttonGroups
//
//-- This next thing is also interesting. ExtendScript, like
//-- ECMAScript and Javascript, has a global object. That
//-- object is referenced as
//-- this
//-- some of my scripts set a global 'scriptVersion'
//-- if the scriptVersion exists, then the dialog will get
//-- the text of that scriptVersion
if (this.scriptVersion) {
with (dialogRows.add() ) {
with (dialogRows.add() ) {
staticTexts.add({staticLabel:String(this.scriptVersion)}) ;
}
}
}
//
}
}
//-- Show the dialog, and wait for them to click OK or Cancel.
var listResult = listDialog.show() ;

//-- return the selected list index unless they clicked Cancel.
return ( listResult ? userChoice.selectedButton : -1 ) ;
}
//

No comments:

Post a Comment