STYLE FILE REFERENCE

This blog post assumes that you have been following this series to create text and object styles. This post will create script handlers leading up to the creation of a document that has paragraph and object styles assigned. The assumption is that you have a document with object and text styles–a “style sheet”–saved in a folder named “Styles” in a convenient location on your computer. A typical location could be inside the application folder for InDesign. The path to the application’s folder is returned with the following:

   tell application "Adobe InDesign CC 2019"
	set appPath to file path
   end tell

Notice that the result returned from running this code returns a file reference:

   file "Macintosh HD:Applications:Adobe InDesign CC 2019:"

To add the name of a folder inside this path, the file reference needs to be changed to a string:

   tell application "Adobe InDesign CC 2019"
	set appPath to file path as string
   end tell
   set folderPath to appPath & "Styles"

To get a reference to a file within the “Style” folder, a script could have the user select the file with a choose file statement. Here the path to the Style folder inside the application’s folder is returned from a handler (getPath).

   set folderName to "Styles:"
   set promptStr to "Choose file for importing styles"
   set fileChoice to choose file with prompt promptStr without invisibles default location getPath(folderName)

   on getPath(folderName)
      tell application "Adobe InDesign CC 2019"
        set appPath to file path as string
      end tell
      return (appPath & folderName) as alias
   end getPath 

In the above, the path to the Style folder is returned as an alias reference. The alias reference is required for the default location parameter of the choose command. Including default location prevents the user from having to “folder dive” to find the desired file.

If you do not have access to InDesign’s application folder you may decide to create a “Style” folder in the current user’s library and put your style files there. For this, you would use the following getPath () handler:

   on getPath(folderName)
	set folderPath to (path to library folder from user domain) as string
	return (folderPath & folderName) as alias
   end getPath

USING A DOCUMENT PRESET

To create a document, a document preset may be the preferred route. Later, we will import styles from our style sheet into this document. Of course, this assumes that you have saved document presets as you created manually. To get a list of document presets, a script can use the following:

(*Intent index indicates item from {print intent, web intent, mobile intent}*)
   set intentIndex to 1
    try
	set myList to getPresets(intentIndex)
   on error errStr
	display alert ("Error: " & errStr)
   end try

   (*Returns list of names for document presets having intnet designated by list index*)
   on getPresets(intentIndex)
      tell application "Adobe InDesign CC 2019"
	 set intentList to {print intent, web intent, mobile intent}
	 set theIntent to item intentIndex of intentList
	 set fullList to (name of document presets where intent is theIntent)
      end tell
   end getPresets

If there are no document presets with the indicated intent, trying to get the presets by name will throw an error. This is trapped in the try/on error statement block. Try the code above using the value 2 and 3 for the intentIndex. 

GET PRESET CHOICE

If the script does not error, the next step for a script would be to have the user choose the preset from the list of presets available. For this our sample script will use a handler developed in the previous blog post:

getNameFromList. The only change to this handler is in the description comment.

   (*Dialog for user to select item from list*)
   on getNameFromList(styleList, thePrompt)
	set userChoice to choose from list styleList with prompt thePrompt
	if userChoice is not false then
		return item 1 of userChoice
	else
		error "User cancelled"
	end if
   end getNameFromList

Add the following call to the handler to the top of the script inside the try block:

   set presetPrompt to "Select preset for new document"
   set presetName to getNameFromList (myList, presetPrompt)

CREATE DOCUMENT

Again, if the user does not make a choice and clicks the Cancel button, the try statement block will catch the error. If the user makes a choice, add the following to call a handler that creates the document. Add the following call at the top of the script inside the try block:

  set docRef to createDocument (presetName)

Then add the handler to the bottom portion of the script:

   (*Creates document given name of document preset*)
   on createDocument(presetName)
	tell application "Adobe InDesign CC 2019"
		set docRef to make document with properties {document preset:presetName}
	end tell
	return docRef
   end createDocument

ADD STYLES

For the final functionality, the script will allow the user to choose a style file from which to import styles. Again a handler will be used. The call will be to a handler named addStyles:

With the call to the handler added, the top portion of the script should now read as follows:

   set folderName to "Styles"
   try
	set myList to getPresets(1)
	set presetPrompt to "Select preset for new document"
	set presetName to getNameFromList(myList, presetPrompt)
	set docRef to createDocument(presetName)
        addStyles(docRef, folderName, "Text and Object Styles")
   on error errStr
	display alert ("Error: " & errStr)
   end try

The handler gets the file choice from the user and imports the styles based on the value for the variable importPref which, for our purpose, assumes the user wants to import text and object styles:

(*Imports styles to document using file chosen from folder alias returned from getPath handler*)
    on addStyles(docRef, folderName, importPref)
      set choicePrompt to "Choose file for styles to import"
      set folderRef to getPath(folderName)
      set fileChoice to choose file with prompt promptStr default location getPath(folderName) without invisibles
      tell application "Adobe InDesign CC 2019"
	 tell docRef
	    if importPref is "Text and Object Styles" then
	       import styles format text styles format from sourceFile
	       import styles format object styles format from sourceFile
	    else if importPref is "All Styles" then 				
	       import styles format object styles format from sourceFile
	       import styles format text styles format from sourceFile
	       import styles format table styles format from sourceFile
	    end if
	 end tell
      end tell
   end addStyles

ONWARD AND UPWARD

If you have been following along, your script should now be complete. Try it out. Think about what you could do to make the script more user friendly. One idea might be to combine the two dialogs into one custom dialog. We will go through this process step by step in our next blog post. Stay tuned.

 

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.