Skip to main content
Participating Frequently
May 16, 2023
Answered

The script craches when call can't find any instace

  • May 16, 2023
  • 1 reply
  • 411 views

Hi all,
Working on a script that looks for anchored groups containing a rectangle name "image"

var t = myDoc.stories.everyItem().groups.everyItem().rectangles.item("image").getElements();

It works great until I test it with a document containing no group or no group with an rectange "image".
In those cases the script crashes on that command.

Same with this other line where I call for a textframe name "title" in a selected group
app.select(y.textFrames.item("title").getElements(0));
 
There must be a way to prevent the script from crashing in these cases. Pls help,
Jan
This topic has been closed for replies.
Correct answer TᴀW

Does the script crash, or does it throw an error?

If the latter, try enclosing the statement in try ... catch.

Otherwise, despite the efficiency, you will need to avoid using everyItem() and simply loop through each item, checking whether it has a rectangle and image.

1 reply

TᴀW
TᴀWCorrect answer
Legend
May 16, 2023

Does the script crash, or does it throw an error?

If the latter, try enclosing the statement in try ... catch.

Otherwise, despite the efficiency, you will need to avoid using everyItem() and simply loop through each item, checking whether it has a rectangle and image.

Visit www.id-extras.com for powerful InDesign scripts that save hours of work — automation, batch tools, and workflow boosters for serious designers.
Participating Frequently
May 16, 2023

Thank's TAW,
This made the trick:

try{
var t = myDoc.stories.everyItem().groups.everyItem().rectangles.item("image").getElements();
} catch (_){
}
If I leave the block after the catch the script continues. And I see what you mean by looping throug each item.

This was a great help. Now I can move on.
Community Expert
May 17, 2023

Whenever you use a chained statement like everyItem().xxx... you need to be mindfull that the statement would execute only if every object in that chain is valid else it will error out. So if you want to check for error conditions either go the long route of traversing each hierarchy or enclose the statement in the try catch block.

-Manan

-Manan