Your prescription for increased productivity and profitability
Running out of time and recovering from a minor auto accident, it was now too late to send valentine cards through the mail. In a pinch, I decided to put together a quick electronic card for family and friends. This could be something that I could attach to a personalized email. What better way could there be than to send something personal?
My decision was to use Adobe’s Publish to Web. No time to loose. Thank goodness I had a couple of scripts in my library of files that would help me get the task done in as short of time as possible.
I had fallen in love with some old-fashioned cards: the kind we as young girls put together with lace and cut-outs before anyone even thought of computers. With a little Photoshop magic I modified some scans I had of cards I had inherited from a favorite aunt and set to work.
Once I had the images I wanted modified in Photoshop, it was time to make the document. A script that did the bulk of the work for me was WebDocFromPreset. This is a fairly complex little script that presents the user with options for selecting a document preset, and for importing styles from stylesheets. Some of the basics for the script will be covered in the following discussion.
Of course one can start a project using InDesign’s New Document panel. But when there are other chores may benefit from automation, using a script to create the document becomes a logical choice. The following creates a document in InDesign using an existing preset. For this the script presents the user with a list of document presets having web intent. Once a choice is made, the script creates the document. Easy.
tell application "Adobe InDesign CC 2018" set presetNames to name of document presets where intent is web intent --have user select the preset from the list try set userPrompt to "Choose document preset from list" activate set userChoice to choose from list presetNames with prompt userPrompt if userChoice is false then error "User cancelled" else set presetChoice to item 1 of userChoice end if on error errStr activate display alert ("Error: " & errStr) end try set presetRef to document preset presetChoice set docRef to make document with properties {document preset: presetRef} end tell
No real automation savings here. But as part of a script that imports styles from style sheets, colors from color palettes, and maybe a lot more, creating the document programmatically becomes a vital piece of the automation.
Next, the script can add the ability to import text and object styles from a selected stylesheet.
No stylesheets? Any time you set up a document for a client, take the few seconds necessary to save the document as both a template and a stylesheet. The advantage of having stylesheets handy is that the text and object styles can be imported into any document of just about any size.
I keep my stylesheets in a folder named Styles inside the Presets folder for InDesign. You may decide to keep your stylesheets elsewhere, maybe in the user’s Library folder. To get a list of the stylesheets from within a Styles folder in InDesign’s Presets folder the following can be used.
tell application "Adobe InDesign CC 2018" set appPath to file path as string set stylePath to appPath & "Presets" & ":" & "Styles" end tell set styleList to list folder stylePath without invisibles styleList
With this, it’s a simple matter to have the user select the stylesheet wanted. Adding this to the above, we have the following:
tell application "Adobe InDesign CC 2018" set appPath to file path as string set stylePath to appPath & "Presets" & ":" & "Styles" end tell set styleList to list folder stylePath without invisibles set userPrompt to "Choose style file from list" activate set userChoice to choose from list styleList with prompt userPrompt if userChoice is false then error "User Cancelled" else set styleFileName to item 1 of userChoice end if set styleFilePath to stylePath & ":" & styleFileName styleFilePath
We can put the two routines from above into a script that completes the document setup by adding text styling (both paragraph and character) and object styling to the document.
--include calls to handlers from within a try/on error block to catch any possible errors. try set docRef to docFromPreset() set stylesheetRef to getStylesheetRef() set stylesToImport to {"text", "object", "table"} importStyles(docRef, stylesheetRef, stylesToImport) on error errStr activate display alert ("Error: " & errStr) end try (*Creates document using preset chosen from list of presets having web intent*) on docFromPreset() tell application "Adobe InDesign CC 2018" set presetNames to name of document presets where intent is web intent --have user select the preset from the list try set userPrompt to "Choose document preset from list" activate set userChoice to choose from list presetNames with prompt userPrompt if userChoice is false then error "User canelled" else set presetChoice to item 1 of userChoice end if on error errStr activate display alert ("Error: " & errStr) end try set presetRef to document preset presetChoice set docRef to make document with properties {document preset:presetRef} end tell return docRef end docFromPreset (*Returns reference to file chosen from files in Presets:Styles folder*) on getStylesheetRef() tell application "Adobe InDesign CC 2018" set appPath to file path as string set stylePath to appPath & "Presets" & ":" & "Styles" end tell set styleList to list folder stylePath without invisibles set userPrompt to "Choose style file from list" activate set userChoice to choose from list styleList with prompt userPrompt if userChoice is false then error "User Cancelled" else set styleFileName to item 1 of userChoice end if set styleFilePath to stylePath & ":" & styleFileName return styleFilePath end getStylesheetRef (*Imports text, object, and/or table styles from stylesheet reference passed. stylesToImport is a list containing "text", "object", and/or "table" to indicate styles to be imported*) on importStyles(docRef, stylesheetRef, stylesToImport) tell application "Adobe InDesign CC 2018" tell docRef if stylesToImport contains "text" then import styles format text styles format from stylesheetRef end if if stylesToImport contains "object" then import styles format object styles format from stylesheetRef end if if stylesToImport contains "table" then import styles format table styles format from stylesheetRef end if end tell end tell end importStyles
Notice that the docFromPreset() and the getStylesheetRef() handlers are the same code as above, just placed inside of handlers.
Although the script above is usable, it definitely can be improved. First off, unless edited, all styles (text, object and table) styles will be imported. To make this a first-rate script, you will want to create a custom dialog from which the user can choose the preset, the stylesheetfile, and check off the kind of styles to be imported. For now, we will leave this up to the reader.
While contemplating the creation of a custom dialog, you might want to think about other predetermined collections or settings you might want to add to the document. Perhaps a palette of color choices, some standing graphics or image files. The sky’s the limit.
Scripts provided are for demonstration and educational purposes. No representation is made as to their completeness. Users are advised to use the code at their own risk.