Adobe InDesign CS5 Scripting Techniques - Working With File And Folder Objects

Published: 05th April 2011
Views: N/A

>Adobe InDesign CS5 , after conquering QuarkXPress, is now widely regarded as the industry-standard page layout, favoured by designers and prepress professionals worldwide. The ability to control Adobe InDesign CS5 with JavaScripts makes the program even more powerful, enabling developers to automate all sorts of procedures and workflows which, when performed manually, may be prone to error and sometimes very tedious. In this article, we will examine some of the commands available for dealing with files and folders.

The first thing we need to do is to create a folder object. One of the simplest ways of doing this is to use the "create" method and to place the new folder object into a variable.

var fldSource = new Folder("c/indesign/projects/barkertruman");

This same syntax is used to create a folder objects that points to an existing folder or to one that has not yet been created. If the folder does not yet exist, we can create it using the create method.

var blnSuccess = fldSource.create();

If the attempt to create the new folder succeeds, our Boolean variable blnSuccess will contain true. If the attempt to create the folder fails, it will contain false.

If the folder object references a folder that exists, we might want to examine the contents of the folder. Let's say, for example, we want to check for the existence of an InDesign file called "log.txt". The getFiles method creates an array containing the names of all the files in the folder and, its optional argument (mask) allows you to specify the file extension of the files you want to return.

var arrFiles = fldSource.getFiles("*.indd");

We can then check, within a for loop whether the name of any of the files is "log.txt".

if(arrSource[i].name == "march.txt")

blnExists = true;

Here, we are setting the value of a Boolean variable to true if the file is found.

Working with the file object is not much different to working with the folder object. Firstly, you need to create the object and assign a file to the object by specifying its path.

var myFile = new file("c/indesign/projects/barkertruman/log.txt");

The file object is used mainly used for the creation and manipulation of text files during such operations as keeping a log of activities and reading settings. The methods available with the file object include open, read, write and seek. The open method opens the file for read/write operations. The read method reads the contents of the file starting at the current position. The optional argument specifies the number of characters to read. Similarly, the write method can be used to write to the file starting at the current position. The seek method is used to reset the position within the file.

InDesign Automation Using XML and JavaScript by Grant Gamble - Available from Amazon.

Report this article Ask About This Article

More to Explore