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

Updating Dave Saunders find style/change case script

Community Beginner ,
Sep 06, 2011 Sep 06, 2011

Copy link to clipboard

Copied

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.
}

TOPICS
Scripting

Views

8.0K

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 Beginner , Sep 24, 2011 Sep 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 =

...

Votes

Translate

Translate
Community Expert ,
Sep 06, 2011 Sep 06, 2011

Copy link to clipboard

Copied

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.

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 ,
Sep 06, 2011 Sep 06, 2011

Copy link to clipboard

Copied

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

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
Guest
Sep 19, 2011 Sep 19, 2011

Copy link to clipboard

Copied

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

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 Beginner ,
Sep 24, 2011 Sep 24, 2011

Copy link to clipboard

Copied

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

}

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 Beginner ,
Oct 30, 2011 Oct 30, 2011

Copy link to clipboard

Copied

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

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
New Here ,
Feb 18, 2013 Feb 18, 2013

Copy link to clipboard

Copied

Hello ^^

This new version was great to convert existing text in InDesign.

I wonder if it's possible to use it directly in a style, so that in the future, anything I type in that particular style always appears in lowercase?

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
Contributor ,
Feb 19, 2014 Feb 19, 2014

Copy link to clipboard

Copied

meluseene,

I think the answer is "kind of"

This will change a style so that everything you type in the future will appear in uppercase:

app.activeDocument.paragraphStyles.item("StylenameHere").capitalization = Capitalization.ALL_CAPS;

(Of course, you can always edit the style to the same thing).

But the opposite of ALL_CAPS is NORMAL, not lowercase. NORMAL means that newly typed text will appear as it is typed. If the shift key is up, it will be lowercase.

Once you have type in a text frame, you can do something like this:

someTextObject.changecase(ChangecaseMode.LOWERCASE);

which lowercases the underlying characters. If the style is enforcing ALL_CAPS, you won't see any difference unless you also change the styling:

someTextObject.capitalization=Capitalization.NORMAL;

or maybe

someTextObject.appliedParagraphStyle.capitalization=Capitalization.NORMAL;

if you want to change the underlying style for all instances.

But I don't think there is a way to always get lowercase in the future...

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 Beginner ,
Jun 04, 2015 Jun 04, 2015

Copy link to clipboard

Copied

Hi All, thanks for updating this script.

I updated the script to include the option of BOTH character and paragraph styles to choose from, this way you only need one script.

I did not fully test it, just tested the paragraph option (which i needed). If you find any issues, post back here.

//DESCRIPTION: Update of Dave Saunder's ChangeCaseOfSelectedStyle script to replace either Paragraph and Character Styles

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

myDoc = app.activeDocument;

myCStyles = myDoc.characterStyles;

myCStringList = myCStyles.everyItem().name;

myPStyles = myDoc.paragraphStyles;

myPStringList = myPStyles.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()) {

     myCStyle = dropdowns.add({stringList:myCStringList,selectedIndex:0,minWidth:133});

    }

   }

   with (dialogRows.add()) {

    with (dialogColumns.add()) {

     staticTexts.add({staticLabel:"Paragraph Style:"});

    }

    with (dialogColumns.add()) {

     myPStyle = dropdowns.add({stringList:myPStringList,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();

}

  theCStyle = myCStyle.selectedIndex;

  thePStyle = myPStyle.selectedIndex;

  theCase = myCase.selectedIndex;

  myDialog.destroy();

  app.findTextPreferences = NothingEnum.NOTHING;

  app.changeTextPreferences = NothingEnum.NOTHING;

  if (theCStyle > 0) {

  app.findTextPreferences.appliedCharacterStyle = myCStyles [theCStyle];

  }

  if (thePStyle > 0) {

   app.findTextPreferences.appliedParagraphStyle = myPStyles [thePStyle];

  }

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

}

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
Enthusiast ,
Dec 21, 2020 Dec 21, 2020

Copy link to clipboard

Copied

This thread is 5+ years old, but I'll give it a shot...

Any chance there's an update on this script that works with the latest version of ID (at least I'm guessing the newer version is what's causing the problem)? Here's the error message I'm getting:

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
Enthusiast ,
Dec 22, 2020 Dec 22, 2020

Copy link to clipboard

Copied

Hmmm...my screenshot didn't show up. Let's try again.

 

Screenshot 2020-12-21 21.00.53.png

 

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
New Here ,
May 25, 2021 May 25, 2021

Copy link to clipboard

Copied

LATEST

I think this now works:

 

//DESCRIPTION: Update of Dave Saunder's ChangeCaseOfSelectedStyle script to replace either Paragraph and Character Styles
if ((app.documents.length != 0) && (app.selection.length != 0)) {
myDoc = app.activeDocument;
myCStyles = myDoc.characterStyles;
myCStringList = myCStyles.everyItem().name;
myPStyles = myDoc.paragraphStyles;
myPStringList = myPStyles.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()) {
     myCStyle = dropdowns.add({stringList:myCStringList,selectedIndex:0,minWidth:133});
    }
   }
   with (dialogRows.add()) {
    with (dialogColumns.add()) {
     staticTexts.add({staticLabel:"Paragraph Style:"});
    }
    with (dialogColumns.add()) {
     myPStyle = dropdowns.add({stringList:myPStringList,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();
}
  theCStyle = myCStyle.selectedIndex;
  thePStyle = myPStyle.selectedIndex;
  theCase = myCase.selectedIndex;
  myDialog.destroy();
  app.findTextPreferences = NothingEnum.NOTHING;
  app.changeTextPreferences = NothingEnum.NOTHING;
  if (theCStyle > 0) {
  app.findTextPreferences.appliedCharacterStyle = myCStyles [theCStyle];
  }
  if (thePStyle > 0) {
   app.findTextPreferences.appliedParagraphStyle = myPStyles [thePStyle];
  }
  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();
}

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
New Here ,
Feb 19, 2014 Feb 19, 2014

Copy link to clipboard

Copied

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

}

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 23, 2020 Dec 23, 2020

Copy link to clipboard

Copied

Hi kyriosity,

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

 

Regards,
Uwe Laubender

( ACP )

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