• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Select all open documents instead of current

Explorer ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

147

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 10, 2024 Dec 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'

...

Votes

Translate

Translate
Community Expert ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

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. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

Check my updated reply. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

Thanks again Robert. I can't check this until my Admin lets me add the code, but I'll let you know. Thanks again :).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

quote

Thanks again Robert. I can't check this until my Admin lets me add the code, but I'll let you know. Thanks again :).


By @davidh27380473

 

You are welcome. 

 

But you shouldn't be blocked from accessing User Scripts folder? 

 

Are you on a PC or a Mac?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

It is company permission and not actual access.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

quote

It is company permission and not actual access.


By @John D Herzog

 

I know, but it rather requires extra effort from the IT guys? 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

I think their freedom with their computers and what they can add and subtract is more strict. They could put it in, but want to get permission so as to not get introuble for adding a script to a company machine. I mean they showed us the code so they can obviously get to it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

Hi Robert. Your code worked like a dream - I can now target all open documents. Thank you for your help 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

@davidh27380473

 

You are welcome. 

 

If you work on a PC and have this kind of processing to do more often - and not only on tables - I would suggest checking my ID-Tasker tool. Isn't free but you don't have to be a coder to perform way more complicated operations. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

LATEST

I am on Mac, but it's something I'd definitely consider if it was available on Mac. Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines