Skip to main content
Inspiring
December 10, 2024
Answered

Select all open documents instead of current

  • December 10, 2024
  • 1 reply
  • 867 views

Hi All,

 

I have a lovely script created by an ex. colleague and some lovely chaps here on this forum (see below) which applies specific cell styles to tables, depending on the cells' position in the table. What I would like to do is edit the script to target all open documents instead of the active document. I am guessing I need "app.documents" instead of "app.activeDocument" at the top, but I am worried this won't work lower down in the script, where "app.documents[0]" appears, as I think this means the first of all open documents? If anyone could help with tweaking the code, it would save me hours running the script on each document individually.

Here is the code below...

//get all the 'stories' in the document
var documentStories = app.activeDocument.stories.everyItem();

// Remove overrides from all cells
try {
documentStories.tables.everyItem().cells.everyItem().clearCellStyleOverrides(true);
}
// If no tables exist, display alert box "No table exist!"
catch (err) {
alert ("No tables exist!");
}

//get all of the tables from within the stories
var allTables = documentStories.tables.everyItem().getElements();

//loop through all the tables and run the tableTwentyTwentyTwo function on each individual table.
for(var i = 0; i < allTables.length; i++){
tableTwentyTwentyTwo(allTables[i]);
};

//apply override styles
function tableTwentyTwentyTwo(table){
//get all of the rows in the table
var rows = table.rows;
//get all of the columns in the table
var columns = table.columns;
//loop through the table columns
for(var i = 0; i< columns.length; i++){
//get the current column
var col = columns[i];
//if we are on the first column
if(i == 0){
//apply style to first column
col.cells.everyItem().appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Contents Left Cell 2022");
}
//if we are on the last column
else if(i == (columns.length - 1)){
//apply style.
col.cells.everyItem().appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Contents Right Cell 2022");
}
else{
//apply this to all other columns
col.cells.everyItem().appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Contents Cell 2022");
}
}
//after looping through the columns, set the header style to override (row index 0)
rows[0].cells.everyItem().appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Head Cell 2022");
//after looping through the columns, set the footer style to override (row index -1)
rows[-1].cells.everyItem().appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Contents Last Row Cell 2022");

// Dave H's 'Mickey Mouse' attempt at applying style to first cell of first row
rows[0].cells[0].appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Head Left Cell 2022");
// Dave H's 'Mickey Mouse' attempt at applying style to last cell of first row
rows[0].cells[-1].appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Head Right Cell 2022");
// Dave H's 'Mickey Mouse' attempt at applying style to first cell of last row
rows[-1].cells[0].appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Contents Last Row Left Cell 2022");
// Dave H's 'Mickey Mouse' attempt at applying style to last cell of last row
rows[-1].cells[-1].appliedCellStyle = app.documents[0].cellStyles.itemByName("Table Contents Last Row Right Cell 2022");
}

Any help would be hugely appreciated,

Thanks,
Dave.

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

The easiest way would be to convert your current code into a function and call it in a loop - with document as a parameter. 

 

var allDocs = app.documents.everyItem().getElements();
for(a=0;a<allDocs.length;a++) 
{
processDocument(allDocs[a]);
};

function processDocument(myDoc)
{
// your current code goes here
};

 

Then, in your code, you need to change:

var documentStories = app.activeDocument.stories.everyItem();

to: 

var documentStories = myDoc.stories.everyItem();

 

And it should work - I'm on my phone so can't check. 

 

1 reply

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
December 10, 2024

The easiest way would be to convert your current code into a function and call it in a loop - with document as a parameter. 

 

var allDocs = app.documents.everyItem().getElements();
for(a=0;a<allDocs.length;a++) 
{
processDocument(allDocs[a]);
};

function processDocument(myDoc)
{
// your current code goes here
};

 

Then, in your code, you need to change:

var documentStories = app.activeDocument.stories.everyItem();

to: 

var documentStories = myDoc.stories.everyItem();

 

And it should work - I'm on my phone so can't check. 

 

Inspiring
December 10, 2024

Thanks so much Robert - I'll edit that and see what happens 🙂

Robert at ID-Tasker
Legend
December 10, 2024

Check my updated reply.