Skip to main content
Inspiring
October 12, 2025
Answered

Basic programming script question

  • October 12, 2025
  • 4 replies
  • 800 views

Hi,

I'm not a programmer and I have a basic question about two options, which is the best option?:

- Doing a task in every page

for (loop pages) {
 doTask ();
}

function doTask () {
 //do something
}

- Or, in every page doing the task

doTask();

function doTask () {
 for (loop pages) {
  //do something
 }
}

 

there is no difference, it depends the task...

 

Thanks

 

P.D.:

It's a hugh ammount of pages, the task... add two text frames

Correct answer nicosh

Thank you @m1b  and @Manan Joshi for your responses.

If I understand, the best way to reuse the function is:

var pages = [## //initial,
             ### //final];

doTask(pages);

function doTask(pages_param){
 for (loop inital to final) {
  //do something
 }
}

 

 

@Vamitul - ks also thank you for your solution but beyond the purpose of my script because I have to filter the pages by applied master spread. I'll keep in mind to next time.

4 replies

nicoshAuthorCorrect answer
Inspiring
October 13, 2025

Thank you @m1b  and @Manan Joshi for your responses.

If I understand, the best way to reuse the function is:

var pages = [## //initial,
             ### //final];

doTask(pages);

function doTask(pages_param){
 for (loop inital to final) {
  //do something
 }
}

 

 

@Vamitul - ks also thank you for your solution but beyond the purpose of my script because I have to filter the pages by applied master spread. I'll keep in mind to next time.

Community Expert
October 13, 2025

Yeah this should be perfect. Then you can call it like

doTask(pages)

doTask(app.documents[0].pages)

-Manan

-Manan
Participating Frequently
October 13, 2025

Strictly from a programming perspective, there is absolutely no difference. The code gets transformed to the intermediary bytecode format that will be two nested loops (-- actually, that's a big simplification, but enough for the purpose).

In ExtendScript context, however, depending on the actual needs, you have the possibility of a huge optimisation:
```
Page.prototype.doTask=function() {
//do stuff here, the page is under 'this'
}
doc.pages.everyItem().doTask();
```

This would call the `doTask` for all the pages without looping (basically in parallel). 

—————————————————————————————Krommatine Systems — Makers of InDesign plugins, tools, and automations.
Community Expert
October 13, 2025

I think there is a caveat I had read somewhere about adding properties to InDesign objects in JSX. They might fail in some cases. I am not totally recalling that part. @Vamitul - ks do you happen to know about this? If so please do mention some details on that part as well.

-Manan

-Manan
Participating Frequently
October 13, 2025

Ahh, yes, a blast from the past. This was discussed and tested about 15 years ago, or more, but a lot of the information has been lost when the forums have been reorganized. 

I'll try to summarize and explain:

First thing to understand is that "native InDesign objects" in ExtendScript are not JavaScript objects, but wrappers for commands (or "object specifiers") to access the internal document DataBase. So, a bit of code like:

`doc.pages.item(5)` is nothing more than a pointer to a POSSIBLE location in the DB. If the document has only 3 pages, the call itself is ok, but the "location" is empty, so trying to resolve it will cause an error ("Object is invalid"). 

When it comes to extending the prototypes, this means that trying to add a PROPERTY to the native objects is doomed to fail badly because the objects only exist when resolved. METHODS, on the other hand, work just fine because they are instructions on how to manipulate a specific type of object, with the fantastic advantage that those instructions can be applied in a single step, using the collection access (`everyItem()`). 

—————————————————————————————Krommatine Systems — Makers of InDesign plugins, tools, and automations.
Community Expert
October 13, 2025

It all depends upon how you envision the function to be used now and in future. Now if this function might be at some point be used with an arbitary collection of pages sourced via any means then the first option is fine. In that option you are not hardcoding the collection to iterate. If you always know that you need to iterate only a certain collection you can hardcode iterating that as in the second option. Or better you could send in the collection to work with as an argument so that you have best of both options.

-Manan

-Manan
m1b
Community Expert
Community Expert
October 12, 2025

hi @nicosh, ,The first way is more flexible, meaning you could repurpose the function to process an arbitrary collection of pages. Performance wise, it may depend on the specifics of your task but probably both would be fine.

- Mark