Skip to main content
briann57175377
Inspiring
February 2, 2018
Answered

how to run the javascript in all active indesign documents?

  • February 2, 2018
  • 3 replies
  • 5843 views

Hello all,

Newie here. I browsed through the different forums and assembled a simple script to set up the column and gutter on an indesign page, It's not this simple, it will be expanded later.

var doc = app.activeDocument;

var page = doc.pages.item(0);

    page.marginPreferences.properties = {

       columnCount: 12,

        columnGutter: 0

        };

it works when run via Indesign script menu on the active document. I would like it so that it runs on all open documents. I googled far and wide, and found some sample, but i could not make it work, either syntax errors, var undefined, etc.

I tried the suggestion from how to apply my script to all opened files?, it didn't work.

It didn't work for me.

while (documents.length > 0) {

var doc = app.activeDocument;

var page = doc.pages.item(0);

    page.marginPreferences.properties = {

       columnCount: 12,

        columnGutter: 0

        }

app.activeDocument.close()

}

I tried copying from another script that Mr. Kuhrel helped me with prior, it still runs but only on the active window.

The variables are not correct, that much i can tell but i don't know how to fix it.

for (var i = app.documents.length-1; i >= 0; i--) {

var doc = app.activeDocument;

var page = doc.pages.item(0);

    page.marginPreferences.properties = {

       columnCount: 3,

        columnGutter: 0

}

Is there a universal wrapper that can be used to //insert codes here — to run on all open documents in indesign?

Regards and have a great weekend.

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Closing doc while script run and keep relation for its execution on current DOM status can lead you into false result.

I suggest to use Peter Kahrel way with one modification:

  1. for (var i = app.documents.length-1; i >= 0; i--) { 
  2. var doc = app.documents
  3. var page = doc.pages.item(0); 
  4.     page.marginPreferences.properties = { 
  5.       columnCount: 3
  6.         columnGutter: 0 
  7. }   
  8. }

Jarek

3 replies

briann57175377
Inspiring
February 14, 2018

Sorry for the later reply, this is exactly what I needed. Thank you both!!

Jump_Over
Jump_OverCorrect answer
Legend
February 3, 2018

Hi,

Closing doc while script run and keep relation for its execution on current DOM status can lead you into false result.

I suggest to use Peter Kahrel way with one modification:

  1. for (var i = app.documents.length-1; i >= 0; i--) { 
  2. var doc = app.documents
  3. var page = doc.pages.item(0); 
  4.     page.marginPreferences.properties = { 
  5.       columnCount: 3
  6.         columnGutter: 0 
  7. }   
  8. }

Jarek

Participant
March 22, 2021

Hello,

 

i have nearly the same issue, i want to save all opened pdf documents.. It is only saving one document to the path, can you help me please? 

 

while (app.documents.length > 0) {

this.saveAs("D:/Users/TestUser/Desktop/" + this.documentFileName);

app.activeDocument.close()

}

 

 

I think i know my fault.. "this" only saves one doc in my case, maybe the solution is an array? I really dont know how to code this, im new in adobe scripting

rob day
Community Expert
Community Expert
March 22, 2021

i want to save all opened pdf documents.

 

You can’t open a PDF into InDesign (you can only Place PDFs), so maybe you are looking for an Acrobat script?

 

For InDesign files you don’t need a file path unless they are new documents, so this would work for existing doc:

 

while (app.documents.length > 0) {
    app.activeDocument.save()
    app.activeDocument.close()
}

 

If you wanted a save as to a specified folder then:

 

var fld = Folder.desktop

while (app.documents.length > 0) {
    app.activeDocument.save(File(fld + "/" +app.activeDocument.name))
    app.activeDocument.close()
}

 

Colin Flashman
Community Expert
Community Expert
February 3, 2018

In your second javascript example, change the top line:

while (documents.length > 0) {

to the following:

while (app.documents.length > 0) {

Otherwise, there are other ways to run a script (or lots of scripts) to many documents. Check out this thread here:

How to take an existing script and apply it to multiple documents

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Participant
March 22, 2021

Hello,

 

i have nearly the same issue, i want to save all opened pdf documents.. It is only saving one document to the path, can you help me please? 

 

while (app.documents.length > 0) {

this.saveAs("D:/Users/TestUser/Desktop/" + this.documentFileName);

app.activeDocument.close()

}