Your prescription for increased productivity and profitability
InDesign does a remarkable job for exporting documents to HTML, but there is one place where it leaves some work to the designer. That is with graphics that have been created inside of InDesign or with graphics copy/pasted from another resource. These graphics don’t make it into the collection of files placed in the image folder created as part of the export.
This is one reason you may want to read through this blog post to see how you can create a script that will look through your document and export these graphics for you.
Define the purpose of the script. Our demonstration script will allow graphics in the active document to be exported as jpeg and or png files to folders specified for each file type.
In writing a script, consider the steps that your process will need to complete. List the steps starting with the core behavior and work outward to include the supporting steps. For our example script, the steps may be listed as follows:
Start By Defining Values needed in variables hardcoded at the top of the script. These can be set initially with default values. Here we are defining the path to the folders for our graphics.
--Define path to folders set folderPath to path to desktop from user domain as string set jpgPath to folderPath & "Images_JPG" set pngPath to folderPath & "Images_PNG"
Define file types for export. This will be a list of file types.
set exportType to {"jpg", "png"}
Get list of items in document for export. A graphic created in the document should be created as a group as is a graphic copied and pasted from another source. Here we will look for groups inside an InDesign document which we assume is open.
tell application "Adobe InDesign CC 2017" tell document 1 set itemList to every group & every group of every page item end tell end tell itemList
Test It
Copy the code above and paste it inside a new file created in the AppleScript editor. Compile and run. Note that placing a variable at the end of the script causes the editor to place its value in the Result window. When you test the script at this point you should have a list of one or more items.
Assuming that we have folders at the expected location (on the desktop), we can now test to make sure the items in the list will export to the folders. We will be naming the files based on the class and id of the item exported. The statements for this will all be inside a repeat loop.
Change the tell document 1 statement block, to read as follows
tell document 1 set itemList to every group & every group of every page item repeat with i from 1 to length of itemList set itemClass to class of item i of itemList set itemId to id of item i of itemList if "jpg" is in exportType then export item i of itemList to (jpgPath & ":" & itemClass & "_" & itemId & ".jpg") format JPG end if if "png" is in exportType then export item i of itemList to (pngPath & ":" & itemClass & "_" & itemId & ".png") format JPG end if end repeat end tell
If all goes well, you should have a file for each of your groups in the folders for both the jpeg and png files.
Let’s say that while you are at it, you may want to export all graphics to both the jpeg and png formats. We can get a list of all graphics by adding the following to our script. This will go below the statement that sets the variable for itemList.
set graphicList to all graphics
The repeat loop for exporting the graphics will be the same as that for the group with the exception it will be using the graphicList. Rather than rewriting the repeat loop, we will put the code in a handler. We will namel the handler exportItems.
First create a wrapper (function) for the procedure at the bottom of the script:
on exportItems (itemList, exportType, jpgPath, pngPath) --InDesign tell block for exporting items in list end exportItems
Now cut the repeat loop from the top part of the script and paste it inside the handler wrapper. Surround the code withi a tell statement block addressed to the application. The handler should now read as follows:
on exportItems (itemList, exportType, jpgPath, pngPath) tell application "Adobe InDesign CC 2017" repeat with i from 1 to length of groupList set itemClass to class of item i of groupList set itemId to id of item i of groupList if "jpg" is in exportType then export item i of groupList to (jpgPath & ":" & itemClass & "_" & itemId & ".jpg") format JPG end if if "png" is in exportType then export item i of groupList to (pngPath & ":" & itemClass & "_" & itemId & ".png") format JPG end if end repeat end tell end exportItems
It we test the script at this point, nothing will get exported to our folders but we can test to make sure we have a list of graphics. Place the graphicList variable after the last end tell statement at the top of the script. Test the script. You should have a list of graphics in the editor’s Result window (that is, if there are graphics in the active document).
Now add a call to the handler passing the itemList and then the graphicList. Change the code inside the tell statement to the document to read as follows:
set itemList to every group & every group of every page item my exportItems (itemList, exportType, jpgPath, pngPath) set graphicList to all graphics my exportItems (graphicList, exportType, jpgPath, pngPath)
We will now add a try/on error/end try statement at the top of the script to trap any errors that may occur. Before the tell statement to document 1 add a try statement so this section now reads as follows
try tell document 1 set itemList to every group & every group of every page item my exportItems (itemList, exportType, jpgPath, pngPath) set graphicList to all graphics my exportItems (graphicList, exportType, jpgPath, pngPath) end tell on error errStr activate display alert "Error: " & errStr end try end tell --application
To give users options as to whether they want to export groups and or graphics and specify the export formats to use, the following custom dialog can be used. We will create this first as a separate script then add it to the main portion of the script once tested. (See previous blog post for details on creating a user dialog.)
userDialog script
set dialogName to "Export Preferences" tell application "Adobe InDesign CC 2017" set origLevel to user interaction level of script preferences set user interaction level of script preferences to interact with all set dlgRef to make dialog with properties ¬ {name:dialogName, can cancel:true, label:dialogName} tell dlgRef --add fields and widgets here tell (make dialog column) set eGroup1 to make enabling group with properties ¬ {static label:"Export Groups", checked state:true} set eGroup2 to make enabling group with properties ¬ {static label:"Export images", checked state:true} tell eGroup1 set groupJpg to make checkbox control with properties ¬ {static label:"Jpg export format", checked state:true} set groupPng to make checkbox control with properties ¬ {static label:"Png export format", checked state:false} end tell tell eGroup2 set graphicJpg to make checkbox control with properties ¬ {static label:"Jpg export format", checked state:true} set graphicPng to make checkbox control with properties ¬ {static label:"Png export format", checked state:false} end tell end tell --column end tell set userResponse to show dlgRef if userResponse = true then set checkedList to {missing value, missing value, missing value, missing value} --capture results from widgets if (checked state of eGroup1 is true) then set item 1 of checkedList to checked state of groupJpg set item 2 of checkedList to checked state of groupPng end if if (checked state of eGroup2 is true) then set item 3 of checkedList to checked state of graphicJpg set item 4 of checkedList to checked state of graphicPng end if end if destroy dlgRef set user interaction level of script preferences to origLevel --return captured results or throw error based on value of userResponse if (userResponse is false) then error ("User Cancelled") end if end tell --return statement here checkedList
If you test the userDialog script, the dialog should look like the following screen capture
…Dialog created with userDialog script.
Once tested, place the code for the dialog inside of wrappers for a handler in the main script We will call the handler UserDialog:
on userDialog () --place code here end userDialog
It is important that you return the result of the dialog back to the main part of the script. Where the comment says “return statement here”, enter return checkedList
Now all you need to do is call the dialog and use the results for the dialog in the script. Sounds easy, but there is a little work involved. The problem is that there are a number of ways the top of the script can now be constructed. You will need to test the values of the checkedList returned from the dialog and pass these values to the exportItems handler as needed. The top of the script could now be written as follows:
--Define path to folders set folderPath to path to desktop from user domain as string set jpgPath to folderPath & "Images_JPG" set pngPath to folderPath & "Images_PNG" --Define file types for export set exportType to {missing value, missing value} tell application "Adobe InDesign CC 2017" try tell document 1 set userResult to my userDialog () if item 1 of userResult is true then set item 1 of exportType to "jpg" if item 2 of userResult is true then set item 2 of exportType to "png" if exportType is not {missing value, missing value} then set itemList to every group & every group of every page item my exportItems(itemList, exportType, jpgPath, pngPath) end if set exportType to {missing value, missing value} if item 3 of userResult is true then set item 1 of exportType to "jpg" if item 4 of userResult is true then set item 2 of exportType to "png" if exportType is not {missing value, missing value} then set graphicList to all graphics my exportItems(graphicList, exportType, jpgPath, pngPath) end if end tell on error errStr activate display alert "Error: " & errStr end try end tell
Test it
With the dialog configured as in the screen capture below, you should have files for all of your groups saved as both jpg and png files. Notice that the Export Groups enabling group is checked but not the Export Images group. Also both the Jpg export format and Png export format are both checked for Export Groups.
...user input for dialog to export groups to both formats
Notice how the exportType list is used to determine the export types to be used. If both values in the list are missing value (the initial value), no items will be exported.
There is one big hole left in this script. The script assumes that the default settings for export preferences are set as needed. If either items 1 and/or 3 of the list returned from the dialog are true, your script will want to set JPEG export preferences. The same for PNG export preferences if items 2 and/or 4 of the list are true. If the settings would always be the same, you could use something like the following:
if item 1 of userResult is true or item 3 of userResult is true then set properties of JPEG export preferences to {jpeg color space:RGB, JPEG Quality:high, ¬ JPEG Rendering style:baseline encoding, use document bleeds:true} end if if item 2 of userResult is true or item 4 of userResult is true then set properties of PNG export preferences to {PNG color space: RGB, PNG quality: medium, ¬ anti alias: true, export resolution: 72, exporting spread: false, transparent background: true, use document bleeds: true} end if
You will find a listing for these export preferences in the Preferences Suite for InDesign’s AppleScript Dictionary.
Note: Export preferences are reserved for the application (not document), so the statements will need to be inside the tell statement to the application (not the document). You will then need to move the call to userDialog before the export preferences statements.
tell application "Adobe InDesign CC 2017" set userResult to my userDialog() --statements to set export preferences here tell document 1 ...
To really be bullet proof, your script will need to make sure the folders as named above are created. We will look at this in the next blog post.
Disclaimer:
Scripts are provided to help users create their own real world scripts. No representation is made as to the completeness or reliabiity of the scripts. Use at your own risk.