Monday, July 30, 2018

Script of the Day Changing Case Again

Script of the Day Changing Case Again


A member of the BlueWorld list asked for a script to change the case of text in a particular style to lowercase. Completely forgetting that Shane Stanley had posted an AppleScript to do this a few weeks ago, I embarked on converting one of my scripts that makes use of a simple user dialog to do the job. Like many scripts, the doing of the job is relatively easy, but giving the user a way to say what he wants to do is a little more challenging.

In this case, I decided to let the user pick a paragraph style and choose which of the four possible case changes he wants to apply. Once more, the script is wrapped in the selection handling framework. Then a dialog with two drop-down menus is presented to the user. Finally, the real work is done in the small loop just before the functions.
//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[j].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.
}
Im thinking that a variation of this script that let you choose character styles instead (or even one that let you choose one or the other) might be equally useful. And, I should probably integrate the early Smart Title Case script rather than lumber the user with InDesigns built-in dumb title case.

go to link download