Not Just for Complex Workflows

A popular misconception about scripting is that a script is only needed to automate complex and/or repetitive functionality. While scripts are used for this purpose, there are some manual processes that you may be doing every day that can be aided with a script. Just think for a minute about the tasks you do that kind of get under your skin. After all, if you are the creative type, anything outside of the realm of design may be distasteful. For me, going to the Finder to get a file or a folder of files is one of those tasks I try to avoid. Sure, opening a folder from the Finder is no big thing especially if you have the folder listed in Favorites, but then maybe you don’t.

Think about it, to get files to place:

  • Select Place… from InDesign’s File menu (Command + D)
  • select the folder from your Favorites list (scroll and Click)
  • or browse the computer’s file structure to find the folder
  • select the file or files from the folder [click or Shift plus click]

If you have a location reserved for a current working folder, all but the last step above can be taken care of by a script using one simple keyboard shortcut. This shortcut might be the same as the Place Files shortcut (Command + D), but using Option+Shift instead (Option + Shift + D).

Sample Script

The following builds a simple script to demonstrate. It assumes that the reserved location will be a folder named WorkingFolder in the User’s directory. Inside this folder will be the image folder, appropriately named “images” (notice capitalization). To get a path to this folder the following can be used:

set parentFolderName to "WorkingFolder"
set imageFolderName to "images"
set userFolder to path to home folder from user domain as string
set myWorkingFolder to userFolder & "WorkingFolder"
set imageFolder to myWorkingFolder & ":images"

Next, the script needs to get a list of files in the folder. To make sure that the image folder exists, the statement can be enclosed inside an if statement.

if (exists imageFolder) then
   set fileList to list folder imageFolder without invisibles
end if 

To alert the user to the fact that the folder does not exist, all of this code can be placed inside a handler to be called from inside a try statement block. This makes the code more readable and reusable. Also, to make the code even more modular, the names of the working folder and the image folder can be passed to the handler.

set parentFolderName to "WorkingFolder"
set imageFolderName to "images"
try
	set fileList to getFileList(parentFolderName, imageFolderName)
on error errStr
	activate
	display alert "Error: " & errStr
end try
--============
--Handlers
--============
on getFileList(parentFolder, imageFolder)
	set userFolder to path to home folder from user domain as string
	set myWorkingFolder to userFolder & parentFolder
	set myImageFolder to myWorkingFolder & ":" & imageFolder
	if exists (myImageFolder) then
		set fileList to list folder myImageFolder without invisibles
	else
		error "Folder " & myImageFolder & " not found in path given."
	end if
	return fileList
end getFileList

Next, the script needs to have the user choose the files to place from the list of images. For this, AppleScript provides the choose from list method.

     set userChoice to choose from list fileList with prompt "Prompt here" with multiple selections allowed 

Again, because an error condition is raised if the user dismisses the dialog by pressing the Cancel button, the code is best placed inside a handler.

--returns list of files chosen from fileList; else throws error
on getUserChoice(fileList, thePrompt, allowMultiple)
	set userChoice to choose from list fileList with prompt thePrompt multiple selections allowed allowMultiple 
	if class of userChoice is list then
		return userChoice
	else —if user cancels, the result is false, not a list
		error "User Cancelled"
	end if
end getUserChoice

The handler, called from within the try statement block, passes the list of files, the user prompt, and a true or false (boolean) to indicate if multiple images can be chosen.

     set userChoice to getUserChoice (fileList, "Select image files from list", true)

Should you want to place the images inside an existing InDesign document, the script will need to make sure there is a document open. For this a handler can be used to return a reference to the active document if it exists. This handler also throws an error if a document is not found, or if an InDesign dialog is open.

--returns reference to the first document; otherwise throws errors
on getDocRef()
	(*Returns reference to active document; otherwise generates error.*)
	tell application "Adobe InDesign CC 2014"
		if modal state = true then
		     error "Please close dialogs before running script"
		end if
		if not (exists document 1) then
		     error "Requires active document"
		else
		     return document 1
		end if
	end tell
end getDocRef 

Again, this handler is called from inside the try statement block.

     set docRef to getDocRef ()

Lastly, to place the images in InDesign’s place gun the following handler can be used. It relies on the path to the image folder that was established in the getFileList() handler. For this a global variable can be used.

--at top of script
global myWorkingFolder 
--call handler from inside try statement handler just before on error statement
loadPlaceGun(docRef, userChoice)
--handler for loading reference to files chosen in place gun
on loadPlaceGun (docRef, userChoice)
     set filePathList to {}
     set imageFolder to myWorkingFolder & ":images"
     repeat with i from 1 to length of userChoice
         set end of filePathList to (imageFolder & ":" & item i of userChoice) as alias
     end repeat
     tell application "Adobe InDesign CC 2014"
         tell docRef
               if loaded of place gun 1 is false then
                    tell place gun 1
                         load place gun filePathList without showing options
                    end tell
               end if
          end tell
     end tell
end loadPlaceGun

And there you have it, a script that can be easily modified to return the images chosen by the user from any pre-defined folder location. Place the script in one of InDesign’s dedicated script folders and give it a keyboard shortcut. Next time you start a project, set up your file structure to use the script. See our blog of July 19 for instructions on working with scripts and setting up keyboard shortcuts.