Skip to main content
Participating Frequently
September 6, 2011
Answered

Updating Dave Saunders find style/change case script

  • September 6, 2011
  • 6 replies
  • 8708 views

How can I update a Dave Saunders script from CS1 and CS2 to CS5?

he wrote a brilliant script which searches for a paragraph style and changes the case to upper or lower.

However, it was for CS1 and CS2.   I can 't get it to run on CS5.

it trips up on

  app.findPreferences = null;
object does not support 'findPreferences'

here is the script.  thanks for your help!

//DESCRIPTION: Converts text in designated parastyle to designated case

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.findPreferences = null;
  app.changePreferences = null;
  myFinds = myDoc.search('',false,false,undefined,{appliedParagraphStyle:myStyles[theStyle]});
  myLim = myFinds.length;
  for (var j=0; myLim > j; j++) {
   myFinds.texts[0].changecase(myCases[theCase]);
  }

} else {
errorExit();
}

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

function errorExit(message) {
if (arguments.length > 0) {
  if (app.version != 3) { beep() } // CS2 includes beep() function.
  alert(message);
}
exit(); // CS exits with a beep; CS2 exits silently.
}

This topic has been closed for replies.
Correct answer seddybell

I have been wanting this script to work for ages as well, but I just started studying javascript in InDesign several weeks ago reading "Scripting InDesign CS3/4 with Javascript" by Peter Kahrel. Even though it says CS3/4, it does go into CS5. Not sure if Peter updated it recently or not.

Anyway, thanks to that and Haakenlid and Manan's help, I was able to update the script and get it to work successfully. I had gotten as far as changing "app.findPreferences = null;" to "app.findTextPreferencess = NothingEnum.NOTHING;", but I was stuck on line 43 (myFinds = myDoc.search...). I had to change one thing in Haakenlid's code: "appliedParagraphStyle" should be "appliedCharacterStyle". Without further ado, here is the fixed 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.characterStyles;

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:"Character 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.appliedCharacterStyle = myStyles [theStyle]; var myFinds = myDoc.findText();

  myLim = myFinds.length;

  for (var j=0; myLim > j; j++) {

   myFinds.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();

}

6 replies

Community Expert
December 23, 2020

Hi kyriosity,

this code was damaged last year when the thread was transferred from the old to the new forum.

 

Regards,
Uwe Laubender

( ACP )

Participant
February 19, 2014

I've adapted seddybell's new script so that it works with paragraph styles instead of character styles. Hope that's useful to someone.

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

//jblondie: Fork of seddybell's update so that you can select by Paragraph Style instead of Character Style.

if ((app.documents.length != 0) && (app.selection.length != 0)) {

myDoc = app.activeDocument;

myStyles = myDoc.paragraphStyles; //jblondie: replaced character with paragraph

myStringList = myStyles.everyItem().name;

myCaseList = ["UPPERCASE","lowercase", "Title Case", "Sentence case"]; //jblondie: Changed myCaseList so it's consistent with InDesign's built in Change Case tool

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:"}); //jblondie: replaced Character with Paragraph

    }

    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(); //jblondie: replaced Character with Paragraph

  myLim = myFinds.length;

  for (var j=0; myLim > j; j++) {

   myFinds.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();

}

seddybellCorrect answer
Participant
September 24, 2011

I have been wanting this script to work for ages as well, but I just started studying javascript in InDesign several weeks ago reading "Scripting InDesign CS3/4 with Javascript" by Peter Kahrel. Even though it says CS3/4, it does go into CS5. Not sure if Peter updated it recently or not.

Anyway, thanks to that and Haakenlid and Manan's help, I was able to update the script and get it to work successfully. I had gotten as far as changing "app.findPreferences = null;" to "app.findTextPreferencess = NothingEnum.NOTHING;", but I was stuck on line 43 (myFinds = myDoc.search...). I had to change one thing in Haakenlid's code: "appliedParagraphStyle" should be "appliedCharacterStyle". Without further ado, here is the fixed 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.characterStyles;

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:"Character 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.appliedCharacterStyle = myStyles [theStyle]; var myFinds = myDoc.findText();

  myLim = myFinds.length;

  for (var j=0; myLim > j; j++) {

   myFinds.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();

}

Participating Frequently
October 30, 2011

thanks folks. seddybell's fix did the trick. much appreciated!

September 19, 2011

Change this:

app.findPreferences = null;

app.changePreferences = null;

myFinds = myDoc.search('',false,false,undefined,{appliedParagraphStyle:myStyles [theStyle]});

To this:

app.findTextPreferences = NothingEnum.NOTHING;

app.changeTextPreferences = NothingEnum.NOTHING;

app.findTextPreferences.appliedParagraphStyle = myStyles [theStyle];

var myFinds = myDoc.findText();

Community Expert
September 6, 2011

The "findPreferences" method does not exist on the app object in CS5

Instead of this you  can use "app.changeTextPreferences" for specifying the search parameter that will be used for searching and replacing in the "doc.changeText()" method. If you only want to search and not replace you can use "app.findTextPreferences" and "doc.findText()".

Since you are searching for the paragraph style you need to use the text preferences for searching and replacing.

The basic steps you could use for this operation would be

1. Clear the find/change text preferences.

2.  Set the find options

3.  Perform the search-and-replace operation

Manan Joshi

www.metadesignsolutions.com

-Manan
Jongware
Community Expert
Community Expert
September 6, 2011

Did you try this?

http://indesignsecrets.com/using-old-scripts-in-cs3.php

-- it mentions "CS3" in the title but the same trick also works for newer versions.