Skip to main content
Inspiring
October 31, 2019
Answered

Scripting code to select all active documents

  • October 31, 2019
  • 1 reply
  • 895 views

So Im regularly using a script to change case in a paragraph style for work. 

 

the script Im currently using has this code to apply changes to the currently active document: 

 

app.activeDocument;

 

But what I want to do is to change all open documents all at once, like the "all documents" in find/change does. 


Does anyone know the code for this? 

 

If it helps, here is the entire script. 

 

//DESCRIPTION: Update of Dave Saunder's ChangeCaseOfSelectedStyle script for CS5 and later

if ((app.documents.length != 0) && (app.selection.length != 0)) {
myDoc = app.activeDocument;
myStyles = myDoc.paragraphStyles;
myStringList = myStyles.everyItem().name;
myCaseList = ["Uppercase","Lowercase", "Title case", "Sentence case"];
myCases = [ChangecaseMode.uppercase, ChangecaseMode.lowercase, ChangecaseMode.titlecase, ChangecaseMode.sentencecase];

var myDialog = app.dialogs.add({name:"Case Changer"})
with(myDialog){
with(dialogColumns.add()){
with (dialogRows.add()) {
with (dialogColumns.add()) {
staticTexts.add({staticLabel:"Paragraph Style:"});
}
with (dialogColumns.add()) {
myStyle = dropdowns.add({stringList:myStringList,selectedIndex:0,minWidth:133});
}
}
with (dialogRows.add()) {
with (dialogColumns.add()) {
staticTexts.add({staticLabel:"Change Case to:"});
}
with (dialogColumns.add()) {
myCase = dropdowns.add({stringList:myCaseList,selectedIndex:0,minWidth:133});
}
}
}
}
var myResult = myDialog.show();
if (myResult != true){
// user clicked Cancel
myDialog.destroy();
errorExit();
}
theStyle = myStyle.selectedIndex;
theCase = myCase.selectedIndex;
myDialog.destroy();


app.findTextPreferences = NothingEnum.NOTHING;
app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.appliedParagraphStyle = myStyles [theStyle]; var myFinds = myDoc.findText();
myLim = myFinds.length;
for (var j=0; myLim > j; j++) {
myFinds[j].texts[0].changecase(myCases[theCase]);
}


} else {
errorExit();
}


// +++++++ Functions Start Here +++++++++++++++++++++++


function errorExit(message) {
if (arguments.length > 0) {
if (app.version != 3) { beep() }
alert(message);
}
exit();
}

This topic has been closed for replies.
Correct answer Jongware

There is nothing special about "activeDocument", it's just one of the "app.documents" collection. So you can loop over *all* (open) documents using this simple construction:

 

 

 

for (var d=0; d<app.documents.length; d++)
{
    myDoc = app.documents[d];
    .. rest of Dave's script .. (amended for CS5 by ??)

 

 

 

That ought to work -- but the funny thing is, you don't need to. Just as in the interface itself, with JavaScript you can search *all documents*. So instead of adding a loop, change

 

 

var myFinds = myDoc.findText();

 

to

var myFinds = app.findText();

-- at a glance I see only one occurrence of "myDoc" but take care, there may be more.

1 reply

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
October 31, 2019

There is nothing special about "activeDocument", it's just one of the "app.documents" collection. So you can loop over *all* (open) documents using this simple construction:

 

 

 

for (var d=0; d<app.documents.length; d++)
{
    myDoc = app.documents[d];
    .. rest of Dave's script .. (amended for CS5 by ??)

 

 

 

That ought to work -- but the funny thing is, you don't need to. Just as in the interface itself, with JavaScript you can search *all documents*. So instead of adding a loop, change

 

 

var myFinds = myDoc.findText();

 

to

var myFinds = app.findText();

-- at a glance I see only one occurrence of "myDoc" but take care, there may be more.

Inspiring
October 31, 2019

This is brilliant! Thanks a lot!