Automating Adobe InDesign 2019 - AppleScript


Managing Handlers and Templates

Tuesday, May 28, 2019

Handlers and Templates

When writing a script it is best to break the process down into a number of discrete steps (modules). This way you can create and test each step separately. Once tested, the modules can be brought into a script as handlers. Moreover, these handlers can be reused over and over to reduce workload, enforce consistency, and possibly prevent errors. As a very simple example, a routine for reading a text file can be written as follows:

set fileToRead to choose file with prompt "Choose file to read"
set fileText to read file using delimiter {"\n"}

Written as a handler, it could read as follows:

Super Simple Handler

try
   set fileToRead to choose file with prompt "Choose file to read"
   set fileText to readFile (fileToRead)
on error errStr
   activate
   display alert "Error: " & errStr
end try

(*Reads text file using return as delimiter*)
on readFile (fileToRead)
   set fileText to read fileToRead using delimiter "\n"
   return fileText
end readFile

As this is code that could probably be used in any number of scripts, you might want to save it as part of a collection of handlers that can be easily accessed.

You could simply save the file in a protected folder. Then, when needed, open the file and copy the contents into your script.

Another option would be to take advantage of AppleScript Editor's Script folder. To see the folders in this location, you can use the following:

set theFolder to path to library folder as string
set libFolder to theFolder & "scripts:Script Editor Scripts"
set libFolders to list folder libFolder without invisibles
libFolder

You can add your own folder to this list to act as a library for your InDesign handlers. You could do this manually or use the following to add a folder named "InDesign Handlers" if one does not already exist. When run, the script will add a subfolder named "InDesign Handlers" and then within it a folder named "Files/Folders."

Create Handler Folder

set folderName to "InDesign Handlers"
set subFolderName to "Files/Folders"
set theFolder to path to library folder as string
set libFolder to theFolder & "Scripts:Script Editor Scripts"
set theFolder to path to library folder as string
set libFolder to theFolder & "scripts:Script Editor Scripts"
set testFolderPath to libFolder & ":" & folderName
set subFolderPath to testFolderPath & ":" & subFolderName
set myStr to ""
tell application "Finder"
   if not (exists folder testFolderPath) then
	make folder at testFolderPath with properties {name:folderName}
	set myStr to myStr & "Folder " & folderName & " created"
   end if
   if not (exists folder subFolderPath) then
	make folder at folder testFolderPath with properties {name:subFolderName}
	set myStr to myStr & "Folder " & subFolderName & " created"
   end if
end tell
myStr

With this you now have a folder into which a handler such as the Super Simple Handler created above could be saved. Better yet, the handler could be written as a text file and added to the folder. The advantage of using a text file is that its content can be copied into a script simply by holding down the Control key and clicking to choose the file. Let's see how this works by converting our Super Simple Handler to a text file.

Scripts as Text Files

To convert a script to a text file, you first need to convert the script to a string. For this, comment out every quotation mark or backslash with a backslash and surround the entire script with quotation marks. Our Super Simple Handler from above would now look like the following:

"try
set fileToRead to choose file with prompt \"Choose file to read\"
set fileText to readFile (fileToRead)
on error errStr
activate
display alert \"Error: \" & errStr
end try

(*Reads text file using return as delimiter*)
on readFile (fileToRead)
set fileText to read fileToRead using delimiter \"\\n\"
return fileText
end readFile"

Test Your String

For testing, Copy the string from above into a new script as the value of a variable (use myString as the variable). Next, add three lines at the bottom of the string to run the script using do script. Your test script should read similar to the following:

A Script Template

set myString to "try
set fileToRead to choose file with prompt \"Choose file to read\"
set fileText to readFile (fileToRead)
on error errStr
activate
display alert \"Error: \" & errStr
end try

(*Reads text file using return as delimiter*)
on readFile (fileToRead)
set fileText to read fileToRead using delimiter \"\\n\"
return fileText
end readFile"
tell application "Adobe InDesign CC 2019"
   do script myString
end tell

Make sure InDesign CC 2019 is running in the background. Compile the script and run. If all goes well, the script will ask you to choose a text file. The text for the file chosen will be displayed in the Result panel.

With this working you are ready to set the script up as a handler that can be accessed using the Control key method mentioned above. For this we will create a script that allows the handler to be used as its content:

Handler File Template

set scriptText to "
--add the handler from above here without its beginning and ending quotes

"
tell current application
   activate
   tell the front document
      set selectedText to contents of selection
      if selectedText is "" then
         set contents of selection to scriptText
      end if
   end tell
end tell

As this can be used as a template to start any number of scripts for preparing files for your handler library you will want to save it. For now, save it to a temporary location. You may want to give it a name that starts with "A" "AScriptTemplate" so it will be at the top of the list when added to Script Editors templates folder. (More below.)

Create a new script by copying and pasting in the code from the template you just saved. Now copy and paste in the text verson of the ReadFile handler without its surrounding quotes as indicated beginning with the word try and ending with end readFile.

Saving the Handler

Save this code with the name ReadFile.scpt. You can drag the handler to the Files/Folders folder created above or use the following script to place the file::

set myDesktopAlias to path to desktop from user domain
set folderName to "InDesign Handlers"
set subFolderName to "Files/Folders"
set theFolder to path to library folder as string
set libFolder to theFolder & "Scripts:Script Editor Scripts"
set handlerFolderPath to libFolder & ":" & folderName & ":" & subFolderName
set fileToMove to choose file with prompt "Select file to move to handler folder" default location myDesktopAlias
if file type of (info for fileToMove) is not "osas" then
   error "Wrong file type chosen"
end if
tell application "Finder"
   move fileToMove to handlerFolderPath
end tell

In running this script the file chosen is moved to the folder "Files/Folders" in your script handlers library. It is now ready to be added to any script using the Control key menu shortcut.

Try it. With a new script open, place the cursor where you want the handler to be placed. Hold down the Control key and choose the handler from a menu similar to the following screen capture. Done. Start adding your most used handlers to your library in a similar manner.

Once you have a collection of most-used handlers you will soon appreciate having taken a little time out to create your library.

Shows handler in Handler Library menu...Handler Library menu

Script Templates

In version OS 10.7 AppleScript Editor added the ability to start a new script using a script template. Script templates are saved in a protected folder where you can have any number of sub-folders to organize your templates. Apple also provides a variety of useful script templates at this location, including scripts for creating file processing droplets, Mail rule actions, iChat message responders, and more.

If you haven't taken advantage of this yet, now is the time. For instance, you may decide that you want to manage all of your favorite handlers from the Control key menu. For this purpose you will be using the template file we create above often to start a new script. Move the template file to Script Editor's Templates folder or use the following script.

Move To Template Folder

Run the script and choose the AHandlerTemplate script you saved above. (You will be asked to enter in the computer manager's password.) Now when you start a new script you should have your script template available. Templates are ideal for saving complex scripts that need only a few values or handlers changed.

New from Template Script Editor menu item...New From Template menu item

Onward and Upward

With templates and a library of handlers that are Control-key available you can have most of the work done for you the next time you need to write a script. Just think of the possibilities!

You can move files to your InDesign Handlers and AppleScript's Templates folder manually. But using a script can be so much easier. Besides, in writing the scripts to move the files, you have gained some experience in working with the Finder.

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.

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

Recent Posts


Tags


Archive