Tuesday

ExtendScript Toolkit

Most people don't like using the ExtendScript Toolkit (ESTK) but that is what you'll probably be using when you first start to write your scripts, and it's the best way to step through your program when debugging. In a later blog I can cover using Sublime Text as a text editor as it has some nice features like linting, code completion and the ability to execute Indesign code.

On a pc, you can find the ESTK at Start Menu > All Programs > Adobe ExtendScript Toolkit CS4.
On a mac, just press the Command + Spacebar to bring up Spotlight and type 'estk'.

Below is how I like to set it up, with three panels. The left panel is where you write your code, the middle panel contains the Data Browser panel, this is where you can inspect the values of your variables and the state Indesign is in at a given time while running your program. The right panel is where you can enter commands and receive feedback from your program.

Screenshot of the ExtendScript Toolkit (ESTK)
ExtendScript Toolkit (ESTK)


Monday

Create a new Indesign document using Javascript

First of all, Javascript is case sensitive, so if you're following along and reusing my code, make sure to be consistent with how you use uppercase and lowercase letters, for example, 'Indesign' and 'InDesign' are two different words in code.

So lets make a start and write some code.

Fire up the ExtendScript Toolkit (ESTK), type in #target indesign


image of ESTK with '#target indesign' typed into code window
















and hit F5 (Cmd+R on a Mac) to run it.

Indesign should start up if it isn't already running and give you this prompt, if so, tick the 'Don't show again' box and click Yes.


Prompt asking 'Do you want to launch Adobe Indesign CS4'












Now, go back to the ESTK and on the next line, type
#target indesign
alert("Hello world");

Hit F5 (Cmd+R on a Mac) and you should see


Alert dialog box showing 'Hello world' in Indesign


Congrats, you've just written your first script. But let's not stop there, our goal here is to actually create a new document, so add this line

#target indesign
alert("Hello world");
app.documents.add();

Hit F5 (Cmd+R) and BAM you have your new document. Party Time!

That's it for now, next time I'll show you how to save and name your document.

johnt

Sunday

Save and name your indesign file

Now that you're able to create an Indesign document, lets save and give it a name.

But firstly, seeing the "Hello world" prompt gets boring very quickly, you can either comment it out by adding // to the beginning of the line and it will be ignored, or you can just delete it.

#target indesign
//Now you know how to add comments to your code
//alert("Hello world");
app.documents.add();


Ok, back to saving and naming your file in one go. Just add
#target indesign
//Now you know how to add comments to your code
//alert("Hello world");
app.documents.add();
app.documents[0].save();

Hit F5 (Ctrl+R on a mac) to run it.

Screenshot of the Save dialog box














Notice the [0] attached to app.documents[0].save(); this is important as you may have many documents open so you need to tell Indesign to use document 0, which is how counting in javascript works, [0] equals item1, [1] equals item2, [2] equals item3, and so on.

Next, we'll create a text frame and add content to it.

Saturday

Add Content to a Text Frame

So far our javascript does two simple things, it adds a new Indesign file and it saves the file to disk.
#target indesign
app.documents.add();
app.documents[0].save()

To add content to a text frame, we firstly need to create the text frame. If we were doing it by hand, we would:
  1. use the Type Tool to add a text frame
  2. set the dimensions of the text frame by how big we drag the frame out
  3. type something into the text frame

Add a text frame and set it's size

To add the text frame via scripting, we use the textFrames.add() method, and set its size with geometricBounds: [top, left, bottom, right], where top, left, bottom and right are the dimensions of the text frame.

app.documents[0].pages[0].textFrames.add({geometricBounds:[40,40,180,180]});

Add Content

To make our code easier to read, let's assign a variable name to the text frame we just created. Like this:
var myFrame = app.documents[0].pages[0].textFrames[0];

So to place content into myFrame, use

myFrame.contents = "Hello world";

Here is the complete script


Next up I'll show how to import a Word file.

Friday

Import a Word file

In my previous post I showed you how to add content to a text frame. In this post I'll show you how to import a Word file.
Again, if we were doing this manually, the steps would be:
• Create a new Indesign document
• Select File > Place > navigate to your Word file > select and open
• Either click on an existing text frame or draw a new one to place your text.

To do it via scripting we'll replace this line in our current script:
myFrame.contents = "Hello world";

with:
var wordFile = File.openDialog("Choose a Word file to import", "*.doc");
myFrame.insertionPoints.item(-1).place(wordFile);

You can guess just by reading these lines that a dialog box will open asking you to pick a Word file to import, and then it will place it into the frame you created.

An added bonus is if you have formatted the Word file with paragraph and character styles, then they will be imported as well.