Automating InDesign CS6


Scripting New Documents_importStyles

Monday, April 15, 2013

When you create a new document there are five major steps you most likely will take:

  • Create new document, preferably using a document preset
  • Select style sheet from which to import styles
  • Import text styles from the style sheet
  • Import object styles from the style sheet
  • Import table styles from the style sheet

In the previous blog, we started a script to automate the process of creating a new document. It gives the user a list of document presets from which to choose. The custom dialog also provides an input field for entering the number of pages. From there, the script can create the document.

New Document

AppleScript

--Values returned from the dialog
set presetName to "Letter_Single"
set numberPages to 2
--create document using values
tell application "Adobe InDesign CS6"
	set docRef to make document with properties {document preset:presetName}
	tell docRef
		set pages per document of document preferences to numberPages
	end tell
end tell

ExendScript

//Values returned from the dialog
var presetName = "Letter_Single";
var numberPages = 2;
//create document using values
var docRef = app.documents.add(true, presetName);
docRef.documentPreferences.pagesPerDocument = numberPages;

Choose Style Sheet

To import styles from style sheets requires the user select from a list of InDesign documents saved in a designated file folder. A common location for this folder is often in InDesign's Presets folder. For the following discussion we will assume that this will be the location where style documents are saved. To get the path to this folder the following can be used:

AppleScript

tell application "Adobe InDesign CS6"
     set appPath to file path as string
     set stylePath to appPath & "Presets:Styles:"
end tell
stylePath

ExtendScript

//These values are returned from the custom dialog
var appPath =  app.filePath;
var folderStr = Folder.decode(appPath);
var stylePath = folderStr + "/Presets/Styles/";
stylePath;

With the path to the folder established, it becomes a simple matter to get a list of the files in the folder using AppleScript.

AppleScript

The following subroutines return a list of files whose file type matches one of the values in the typeList ("IDd8" is InDesign CS6).

--set fileTypeList to file types for current versions
set fileTypeList to {"IDd6", "IDd7", "IDd2", "IDd8"}
--call subroutine to get path to style file folder
set stylePath to getStylePath()
--call subroutine to get list of files found in the folder
set styleList to getStyleFileList(stylePath, fileTypeList)

(*Returns list of style files in Presets:Styles folder for application*)
on getStyleFileList(stylePath, fileTypeList)
     tell application "Finder"
          set styleList to name of files of folder stylePath where file type is in fileTypeList
     end tell
     return styleList
end getStyleFileList

ExtendScript

Because ExtendScript needs to support both Windows and Macintosh, getting the list of file names is not quite as easy.

try {
     var fileList = getStyleFileList (".indd");
} catch (e) {
    alert (e);
}
fileList;

function getStyleFileList (fileExt) {
    var appPath = app.filePath;
    var folderStr = Folder.decode(appPath);
    var stylePath = folderStr + "/Presets/Styles";
    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; i++) {
            nameList.push(Folder.decode(fileList[i].name));
        }
    }
    return nameList;
    //filter function for getFiles on Macintosh checks for files by extension
    function listByExt (objRef) {
        if (objRef instanceof File) {
            //returns true if value of variable checkExt is part of file name
            return objRef.name.toUpperCase().indexOf(checkExt)!= -1;
        }
    }
}

Once the script has a list of files, this list can be presented to the user in a custom dialog drop down. Once the user determines the style file to use and the styles to import, the script can use the following statements for importing the styles:

AppleScript

--variable styleChoice is reference to file name chosen by user
set stylesheetRef to stylePath & ":" & styleChoice
set importObjectStyles to true
set importTextStyles to true
set importTableStyles to false
importStyles (docRef, stylesheetRef, importObjectStyles, importTextStyles, importTableStyles)

(*Imports styles from stylesheet referenced given boolean values for object, text, and/or table styles*)
on importStyles(docRef, stylesheetRef, doObject, doText, doTable)
     tell application "Adobe InDesign CS6"
          tell docRef
               if doObject is true then
                    import styles format object styles format from stylesheetRef
               end if
               if doText is true then
                    import styles format text styles format from stylesheetRef
               end if
               if doTable is true then
                    import styles format table styles format from stylesheetRef
               end if
          end tell
     end tell
end importStyles

ExtendScript

//variable fileIndex is reference to index of file name chosen by user
var fileName = styleList[fileIndex];
var stylesheetRef = File(stylePath + fileName;
var importObjectStyles = true;
var importTextStyles = true;
var importTableStyles = false;
//strategy to use in case of stylename clash
var strategyOpt = GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE;
//call importStyles function 
importStyles (docRef, stylesheetRef, doText, doObj, doTable, strategyOpt)

function importStyles (docRef, fileRef, textStyles, objStyles, tableStyles, strategyOpt) {
if (textStyles == true) {
docRef.importStyles (ImportFormat.TEXT_STYLES_FORMAT, fileRef, strategyOpt);
}
if (objStyles == true) {
docRef.importStyles(ImportFormat.OBJECT_STYLES_FORMAT, fileRef, strategyOpt);
}
if (tableStyles == true) {
docRef.importStyles(ImportFormat.TABLE_STYLES_FORMAT, fileRef, strategyOpt);
}
}

Now all you need is to put the script together with a custom dialog box to get the user input. Look for more in next week's blog.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=15239&PostID=962551&A=Trackback
Trackbacks
Post has no trackbacks.