Skip to Content
Scripting InDesign CS3/4 with JavaScript
book

Scripting InDesign CS3/4 with JavaScript

by Peter Kahrel
April 2009
Beginner to intermediate
80 pages
2h 12m
English
O'Reilly Media, Inc.
Content preview from Scripting InDesign CS3/4 with JavaScript

Catching Errors

We’ve mentioned a few times by now that you should always test whether it’s safe to let a script do something. Two of the examples we’ve seen are the following:

if (app.selection[0].constructor.name != "InsertionPoint")
if (myString == "nonsense")

These two checks are specific: the first one tests whether a selection is an insertion point; in the second one we make sure that we do something only if the variable myString has a particular value. We use specific tests like these when we are well aware of what could go wrong.

However, we often don’t know what could go wrong, or we might run into problems because of several reasons. Trying to list the possible error conditions could be tedious, and you never know if you’ve got them all. To deal with these situations, you can use JavaScript’s general error catcher. Let’s force an error. Place the cursor in some text in an InDesign document, then run this script:

try
    {
    app.selection[0].contents = 3
    }
catch (myError)
    {
    alert (myError);
    exit ()
    }

What we want to do goes in the block under try. Any error that occurs during the execution of the clause(s) in the try block are caught by the catch statement and the script passes control to the script code in the catch block.

Here, we’ve chosen to display the error and to halt the script. You could also decide that at some stage in the script something could go wrong, but you’re not interested to know the problem: all you care about is that the script gets on with it. In that case you ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GREP in InDesign

GREP in InDesign

Peter Kahrel
XML Hacks

XML Hacks

Michael Fitzgerald

Publisher Resources

ISBN: 9780596803599