Skip to main content
December 10, 2013
Question

Clear setting javascript

  • December 10, 2013
  • 2 replies
  • 836 views

I use the adobe findchangebylist to modified, I want to do a clear setting script, including break link to style and character, turn off hyphenation, ligatures, set mojikumi to nothing .....

but when i run the script and choose selection (Selection mean: Select text only, text and table, select table only or table cells) that occur error, by the way, click current document is ok, can anyone to help me to fix it, my script is shown as below:

main();
function main(){
var myObject;
//var myCheckSelection = false;
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if(app.documents.length > 0){
  if(app.selection.length > 0){
   switch(app.selection[0].constructor.name){
    case "InsertionPoint":
    case "Character":
    case "Word":
    case "TextStyleRange":
    case "Line":
    case "Paragraph":
    case "TextColumn":
    case "TextFrame":
    case "Text":
    case "Cell":
    case "Column":
    case "Row":
    case "Table":
     myDisplayDialog();

     break;
    default:
     //Something was selected, but it wasn't a text object, so search the document.
     //myFindChangeByList(app.documents.item(0));
                    //alert("Nothing Selected, Please Select Text Frame or Text.");
   }
  }

  else{
   //Nothing was selected, so simply search the document.
            //myFindChangeByList(app.documents.item(0));
            alert("Nothing Selected, Please Select Text Frame or Text.");
  }
}
else{
  alert("No documents are open. Please open a document and try again.");
}
}
function myDisplayDialog(){
var myObject;
var myDialog = app.dialogs.add({name:"Joan New Clear Setting_v1.0"});
with(myDialog.dialogColumns.add()){
   with(dialogRows.add()){
   with(dialogColumns.add()){
staticTexts.add({staticLabel:"Search Range:"});
     var mySearchButtons = radiobuttonGroups.add();
     with(mySearchButtons){
     radiobuttonControls.add({staticLabel:"Current Document", checkedState:true});
                   if(app.selection[0].contents != ""){
    radiobuttonControls.add({staticLabel:"Selection", checkedState:true});
                }
     }
    }
   with(dialogColumns.add()){
    with(borderPanels.add()){
     staticTexts.add({staticLabel:"Clear Setting:"});
     var myDateButtons = radiobuttonGroups.add();
     with(myDateButtons){
     radiobuttonControls.add({staticLabel:"English", checkedState:true});
     radiobuttonControls.add({staticLabel:"Chinese"});
      }
    }
   }
  }
 
}

var myResult = myDialog.show();
if(myResult == true){
  switch(mySearchButtons.selectedButton){
   case 0:
                 BreakLinktoStyleDocument();
                 TurnOffHyphenationDocument();
                 myObject = app.documents.item(0);
                 myCheckSelection = false;
    break;
              case 1:
                 BreakLinktoStyleSelection();
                 TurnOffHyphenationSelection();
                 myObject = app.selection[0];
                 myCheckSelection = true;

    break;
  }
  switch(myDateButtons.selectedButton){
  case 0:
  myFindChangeByList1(myObject, myCheckSelection);
                 
  break;
  case 1:
  myFindChangeByList2(myObject, myCheckSelection);
  break;
   }
  myDialog.destroy();
  //myFindChangeByList1(myObject);
}
else{
  myDialog.destroy();
}
}
//-------------------------------------------------------------------- -----------------------------
function TurnOffHyphenationDocument() {
app.activeDocument.stories.everyItem().texts.everyItem().hyphenation=f alse;
myTables = app.activeDocument.stories.everyItem().tables.everyItem();
myTables.cells.everyItem().paragraphs.everyItem().hyphenation=false;
}

function TurnOffHyphenationSelection() {
app.selection[0].texts[0].hyphenation=false;
app.selection[0].cells.everyItem().texts[0].hyphenation=false;
}

//-------------------------------------------------------------------- ---------------------------------------------------------------------- ------------------------------------------------------
function BreakLinktoStyleDocument() {
app.activeDocument.stories.everyItem().texts.everyItem().applyParagrap hStyle(app.activeDocument.paragraphStyles.item(0), false);
app.activeDocument.stories.everyItem().texts.everyItem().applyCharacte rStyle(app.activeDocument.characterStyles.item(0), false);
myTables = app.activeDocument.stories.everyItem().tables.everyItem();
myTables.cells.everyItem().paragraphs.everyItem().applyParagraphStyle( app.activeDocument.paragraphStyles.item(0), false);
myTables.cells.everyItem().paragraphs.everyItem().applyCharacterStyle( app.activeDocument.characterStyles.item(0), false);
}

function BreakLinktoStyleSelection() {
app.selection[0].texts.everyItem().applyParagraphStyle(app.activeDocum ent.paragraphStyles.item(0), false);
app.selection[0].texts.everyItem().applyCharacterStyle(app.activeDocum ent.characterStyles.item(0), false);
app.selection[0].cells.everyItem().texts[0].applyParagraphStyle(app.ac tiveDocument.paragraphStyles.item(0), false);
app.selection[0].cells.everyItem().texts[0].applyCharacterStyle(app.ac tiveDocument.characterStyles.item(0), false);
}

//-------------------------------------------------------------------- ------------------------------
function myFindChangeByList1(myObject, myCheckSelection){
var myScriptFileName, myFindChangeFile, myFindChangeFileName, myScriptFile, myResult;
var myFindChangeArray, myFindPreferences, myChangePreferences, myFindLimit;
var myStartCharacter, myEndCharacter;
var myFindChangeFile = myFindFile("/ScriptSupport/te.txt")
if(myFindChangeFile != null){
        // Because the selection will change as we add/remove characters,
        // we'll need to reset the selection after each pass if we are
        // checking the selection. We'll get the index of the first character
        // in the selection (relative to the start of its parent story) and
        // the index of the last character in the selection (relative to the
        // *end* of the story, and we'll use them later in the script to
        // keep the ends of the selection in place.
  if(myCheckSelection == true){
   var myStart = myObject.characters.item(0).index;
   var myEnd = myObject.characters.item(-1).index;
   var myStory = myObject.parentStory;
   var myStoryEnd = myStory.characters.item(-1).index;
   myEnd = (myStoryEnd - myEnd)+1;
  }
  myFindChangeFile = File(myFindChangeFile);
  var myResult = myFindChangeFile.open("r", undefined, undefined);
  if(myResult == true){
   //Loop through the find/change operations.
   do{
    myLine = myFindChangeFile.readln();
    //Ignore comment lines and blank lines.
    if((myLine.substring(0,4)!="text")||(myLine.substring(0,4)!="grep")|| (myLine.substring(0,5)!="glyph")){
     myFindChangeArray = myLine.split("\t");
     //The first field in the line is the findType string.
     myFindType = myFindChangeArray[0];
     //The second field in the line is the FindPreferences string.
     myFindPreferences = myFindChangeArray[1];
     //The second field in the line is the ChangePreferences string.
     myChangePreferences = myFindChangeArray[2];
     //The fourth field is the range--used only by text find/change.
     myFindChangeOptions = myFindChangeArray[3];
     switch(myFindType){
      case "text":
       myFindText(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);
       break;
      case "grep":
       myFindGrep(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);
       break;
      case "glyph":
       myFindGlyph(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);
       break;
     }
     if(myCheckSelection == true){
      myStartCharacter = myStory.characters.item(myStart);
      myEndCharacter = myStory.characters.item(-myEnd);
      myObject = myStory.texts.itemByRange(myStartCharacter, myEndCharacter);
      app.select (myObject);
     }
    }
   } while(myFindChangeFile.eof == false);
   myFindChangeFile.close();
  }
}
alert("Done");
}
function myFindText(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){
//Reset the find/change preferences before each search.
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences = NothingEnum.nothing;
app.findChangeTextOptions = NothingEnum.nothing;
var myString = "app.findTextPreferences.properties = "+ myFindPreferences + ";";
myString += "app.changeTextPreferences.properties = " + myChangePreferences + ";";
myString += "app.findChangeTextOptions.properties = " + myFindChangeOptions + ";";
app.doScript(myString, ScriptLanguage.javascript);
  myFoundItems = myObject.changeText();
//Reset the find/change preferences after each search.
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences = NothingEnum.nothing;
app.findChangeTextOptions = NothingEnum.nothing;
}
function myFindGrep(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){
//Reset the find/change grep preferences before each search.
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findChangeGrepOptions = NothingEnum.nothing;
var myString = "app.findGrepPreferences.properties = "+ myFindPreferences + ";";
myString += "app.changeGrepPreferences.properties = " + myChangePreferences + ";";
myString += "app.findChangeGrepOptions.properties = " + myFindChangeOptions + ";";
app.doScript(myString, ScriptLanguage.javascript);
var myFoundItems = myObject.changeGrep();
//Reset the find/change grep preferences after each search.
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findChangeGrepOptions = NothingEnum.nothing;
}
function myFindGlyph(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){
//Reset the find/change glyph preferences before each search.
app.changeGlyphPreferences = NothingEnum.nothing;
app.findGlyphPreferences = NothingEnum.nothing;
app.findChangeGlyphOptions = NothingEnum.nothing;
var myString = "app.findGlyphPreferences.properties = "+ myFindPreferences + ";";
myString += "app.changeGlyphPreferences.properties = " + myChangePreferences + ";";
myString += "app.findChangeGlyphOptions.properties = " + myFindChangeOptions + ";";
app.doScript(myString, ScriptLanguage.javascript);
var myFoundItems = myObject.changeGlyph();
//Reset the find/change glyph preferences after each search.
app.changeGlyphPreferences = NothingEnum.nothing;
app.findGlyphPreferences = NothingEnum.nothing;
app.findChangeGlyphOptions = NothingEnum.nothing;
}
function myFindFile(myFilePath){
var myScriptFile = myGetScriptPath();
var myScriptFile = File(myScriptFile);
var myScriptFolder = myScriptFile.path;
myFilePath = myScriptFolder + myFilePath;
if(File(myFilePath).exists == false){
  //Display a dialog.
  myFilePath = File.openDialog("Choose the file containing your find/change list");
}
return myFilePath;
}
function myGetScriptPath(){
try{
  myFile = app.activeScript;
}
catch(myError){
  myFile = myError.fileName;
}
return myFile;
}

and here is my text file, te.txt

//001

grep {findWhat:"."} {appliedLanguage: app.languagesWithVendors.item("English: USA"), kerningMethod: "無"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, includeLockedStoriesForFind:true, widthSensitive:true}

//002

grep {findWhat:"."} {kinsokuSet: "繁體中文避頭尾(s)"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, includeLockedStoriesForFind:true, widthSensitive:true}

//003

grep {findWhat:"."} {mojikumi: "nothing"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, includeLockedStoriesForFind:true, widthSensitive:true}

//004

grep {findWhat:"."} {ligatures:false} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, includeLockedStoriesForFind:true, widthSensitive:true}

//005

grep {findWhat:"."} {gridAlignment: 1852796517} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, includeLockedStoriesForFind:true, widthSensitive:true}

//006

grep {findWhat:"."} {leadingModel: 1248619858} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, includeLockedStoriesForFind:true, widthSensitive:true}

//007

grep {findWhat:"."} {characterAlignment: 1247896172} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, includeLockedStoriesForFind:true, widthSensitive:true}

This topic has been closed for replies.

2 replies

Jump_Over
Legend
December 10, 2013

Hi,

There is at least one error source here:

app.selection[0].cells....

In a few places your code is trying to process with object "Cell" but selection can be a "pure" text (not a table).

Not easy to fix 250-liner with lack of error description.

Did you try to debug your code using, let say, Extend Script Tool Kit? Or other tool?

Jarek

Peter Spier
Community Expert
Community Expert
December 10, 2013

I've moved this discussion to the scripting forum for you. This sort of question will get much faster help here than in the general discusions.