In the last two blogs we have started a script to automate the creation of a new document. When complete the script is designed to allow the user to choose a number of options from a custom dialog. The values returned will be used to automatically create a new document. Among the values to be chosen from the user will be the following:

  • document preset from a filtered list.
  • number of pages for the document
  • style sheet to import styles from
  • style categories to import from style sheet

Toward this end the following subroutines (handlers for AppleScript and functions for ExtendScript) were created:

  • getPresets – lists names of document presets returned from filtered list
  • getStyleFileList – lists names of style files found in specified style folder
  • importStyles – imports styles indicated from style file reference

While at it, a good idea might be to expand the style import options to include color swatches.

USE A GLOBAL FOR IMPORT OPTIONS

With the addition of the color import option, we now have four boolean values that need to be passed to the handler. Not only that, but the user dialog will need to get the values for eachof these from the user and pass them back to the calling statement. Rather than passing these four individual values around, we can set up a global variable to hold a list (array in ExtendScript). The items in this list can be initialized to false, then updated when the user submits the custom dialog.

Let’s see how this would work with a simplified custom dialog:

AppleScript

--global list of options: text, object, table, and color initialized to false
property styleOptions : {false, false, false, false}
--list of styleFile names from which user will choose 
property stylefileList : {}
set styleFolderStr to "Presets:Styles"
try
	--get list of style file names 
	set appFolderPath to getAppPath()
	getStyleFileList(appFolderPath, styleFolderStr)
	userDialog("Project Settings", true, "Dialog Loabel")
on error errStr
	activate
	display alert errStr
end try
userDialog
--=====SUBROUTINES
on userDialog(dlgName, cancelIt, dlgLabel)
   tell application "Adobe InDesign CS6"
      activate
      --make sure script preferences allow a dialog 
      set origLevel to user interaction level of script preferences
      set user interaction level of script preferences to interact with all
      --initialize wasCancelled variable used to flag if dialog is cancelled
      set wasCancelled to false
      --create the dialog
      set dlgRef to make dialog with properties {name:dlgName, can cancel:cancelIt, label:dlgLabel}
      tell dlgRef
         set dialogColumn to make dialog column
         tell dialogColumn
            --create enabling group only if there are stylesheets from which to choose
            if length of stylefileList > 0 then
               set enable1 to make enabling group with properties {static label:"Import Styles", checked state:true}
               tell enable1
                  tell (make dialog column)
                     tell (make dialog row)
                        make static text with properties {static label:"Choose stylesheet:"}
                        set styleDrop to make dropdown with properties {min width:180, string list:stylefileList, selected index:0}
                     end tell --dialog row
                     set textField to make checkbox control with properties {static label:"Text Styles", checked state:true}
                     set objectField to make checkbox control with properties {static label:"Object Styles", checked state:true}
                     set tableField to make checkbox control with properties {static label:"Table and Cell Styles", checked state:false}
                     set colorField to make checkbox control with properties {static label:"Color Swatches", checked state:false}
                  end tell --dialog column
             end tell --enable1
           end if
        end tell --dialog column
      end tell --dialog reference
     --show the dialog and get results if user does not cancel
     set userResponse to show dlgRef
     if userResponse = true then
        if length of stylefileList > 0 then
           if checked state of enable1 = true then
              set item 1 of styleOptions to checked state of textField
              set item 2 of styleOptions to checked state of objectField
              set item 3 of styleOptions to checked state of tableField
              set item 4 of styleOptions to checked state of colorField
           else --user cancelled
              set wasCancelled to true
           end if	
        end if
      end if
      destroy dlgRef
      --if user cancelled, throw error; otherwise return values
      if wasCancelled = true then error "User cancelled"
      --all values to be returned at this point are global
      return {}
   end tell
end userDialog
--Returns path to application folder as a string value
on getAppPath()
	tell application "Adobe InDesign CS6"
		set appPath to file path as string
	end tell
	return appPath
end getAppPath
--Returns list of file names at given folder path
on getStyleFileList(appFolderPath, styleFolderStr)
	set stylePath to appFolderPath & styleFolderStr
	tell application "Finder"
		set itExists to exists folder (stylePath)
	end tell
	if itExists = true then
		set stylefileList to list folder stylePath
	end if
end getStyleFileList

ExtendScript

//global array of inprt style options: text, object, table, and color; initialized to false
var styleOptions = [false, false, false, false];
//arra of style file names from which user will choose
var stylefileList = [];
//call to main function
var styleFolderStr = "/Presets/Styles/"
main();
styleOptions;
function main(){
    try {
    //get list of stylefile names
    var appFolderPath = getAppPath();
    stylefileList = getStyleFileList (appFolderPath, styleFolderStr, ".indd");
    //get user's response from custom dialog
    userDialog("Project Settings", true, "Dialog Label");
    } catch (e) {
    app.activate;
    alert (e);
    }
}
//HELPER FUNCTIONS
function userDialog(dlgName, cancelIt, dlgLabel){
    //make sure script prefeences allow a dialog
    var origLevel = app.scriptPreferences.userInteractionLevel;
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
    //initialize flag to indicate if user cancelled
    var wasCancelled = false;
    //create the dialog
    var dlgRef = app.dialogs.add({name:dlgName, canCancel:cancelIt, label:dlgLabel});
    //add a column
    var dialogColumn = dlgRef.dialogColumns.add();
    //add enabling group //create enabling group only if there are stylesheets from which to choose
    if (stylefileList.length > 0) {
        var enable1 = dialogColumn.enablingGroups.add({staticLabel:"Import Styles", checkedState:true});
        var enableColumn = enable1.dialogColumns.add();
        var enableRow = enableColumn.dialogRows.add();
        enableRow.staticTexts.add({staticLabel:"Choose stylesheet:"});
        var styleDrop = enableRow.dropdowns.add ({minWidth:180, stringList:stylefileList, selectedIndex:0});
        var textField = enableColumn.checkboxControls.add({staticLabel:"Text Styles", checkedState:true});
        var objectField = enableColumn.checkboxControls.add({staticLabel:"Object Styles", checkedState:true});
        var tableField = enableColumn.checkboxControls.add({staticLabel:"Table and Cell Styles", checkedState:false});
        var colorField = enableColumn.checkboxControls.add({staticLabel:"Color Swatches", checkedState:false});
    }
    if (dlgRef.show()== true) {
        if (stylefileList.length > 0) {
          if (enable1.checkedState == true) {
             styleOptions[0] = textField.checkedState;
             styleOptions[1] = objectField.checkedState;
             styleOptions[2] = tableField.checkedState;
             styleOptions[3] = colorField.checkedState;
           }
        }
    } else {
        wasCancelled = true;
    }
   //destroy the dialog
   dlgRef.destroy()
   if (wasCancelled == true) {
       throw ("User cancelled");
   }
   //all values to be returned at this point are global
   return[];
}
//returns path to application as decoded string value
function getAppPath() {
    var appPath = app.filePath;
    var appFolder = Folder.decode(appPath);
    return appFolder
}
//returns larray of files found in style path
function getStyleFileList (appFolderPath, styleFolderStr, fileExt) {
    var stylePath = appFolderPath + styleFolderStr;
    var folderRef = Folder(stylePath);
    //determines system script is running on
    if (File.fs == "Windows") {
        var fileList = folderRef.getFiles("*"+fileExt);
    } else {
        checkExt = fileExt.toUpperCase();
        var fileList = folderRef.getFiles (listByExt);
    }
    if (fileList.length == 0) {
        throw ("No files found");
    } else {
        var nameList = new Array;
        for (var i = 0; i <filelist.length; objref.name.touppercase().indexof(checkext)!="-1;">

As you can see, even a simplified display dialog can end up being not so simple. But once you get this much working, it will be a simple

</filelist.length;>