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

Calc total chars paragraph

Engaged ,
Oct 31, 2020 Oct 31, 2020

Copy link to clipboard

Copied

Hi,

I have a script to copy all index entries from document English into Spanish. 

Works in the most of documents, but with this document I get an Error when the English paragraph length are higher than the spanish. For example:

if

English length paragraph 10 

Spanish length paragraph 9

I get an ERROR because Spanish need be equal or higher than English paragraph length.

 

But if:

English length paragraph 10 

Spanish length paragraph 20

Works!

 

Please any help?

 

My Script:

 

 

var myDocEn = app.documents[0];
var myDocSp = app.documents[1];

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "^I";
var found = myDocEn.findText();
app.findTextPreferences = app.changeTextPreferences= NothingEnum.nothing;
var foundLen = found.length;
$.writeln("\t"+"Total Index found: " + foundLen); 


if (foundLen == 0) {
  alert ("Didn't find any index markers.\nPlease check the correct document is active.");
}

for (var i = 0; i < foundLen; i++) {
  var item = found[i];
  var story = item.parentStory;
  var lastSelIP = item.insertionPoints.lastItem();
  var paras = story.texts.itemByRange(story.insertionPoints.item(0),lastSelIP).paragraphs;
  var currPara = story.paragraphs.item(paras.length - 1);
  var chars = currPara.texts.itemByRange (currPara.insertionPoints.item(0), lastSelIP).characters;
  var paraInx = paras.length - 1;
  var enCharInx = chars.length - 1;
  var spParaLen = myDocSp.stories[0].paragraphs[paraInx].length;

  //var spCharInx = (enCharInx <= spParaLen)? enCharInx:spParaLen - 1;
  var spCharInx = ParaLen(enCharInx,spParaLen);

  function ParaLen(enCharInx,spParaLen){
    $.writeln("English Index Column: " + enCharInx + "\n" + "Spanish Paragraph Length: " + spParaLen); 
    var spCharInx =0
    if (enCharInx <= spParaLen){
      spCharInx = enCharInx
      }
    else{
      spCharInx = spParaLen - 1
    }
    $.writeln("---> Placed on Spanish Column: " + spCharInx + "\n");
      return spCharInx
      
  }

  myDocEn.stories[0].paragraphs[paraInx].characters[enCharInx].duplicate (LocationOptions.BEFORE, myDocSp.stories[0].paragraphs[paraInx].characters[spCharInx]);

}

 

 
I attach 2 indesign if you want to test.
I think the error is in the linke 28, when I calculate the English paragraph Length.
 
Thanks!
 
 
 
TOPICS
Scripting

Views

307

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

Advocate , Nov 01, 2020 Nov 01, 2020

I do not understand why you are checking character index with para length in spanish document.

Anyway you can put one validation:

else if (enCharInx > spParaLen){
    return null;
    }

And if return is null then do not duplicate.

var myDocEn = app.documents[0];
var myDocSp = app.documents[1];
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "^I";
var found = myDocEn.findText();
app.findTextPreferences = app.changeTextPreferences= Noth
...

Votes

Translate

Translate
Advocate ,
Nov 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

I do not understand why you are checking character index with para length in spanish document.

Anyway you can put one validation:

else if (enCharInx > spParaLen){
    return null;
    }

And if return is null then do not duplicate.

var myDocEn = app.documents[0];
var myDocSp = app.documents[1];
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "^I";
var found = myDocEn.findText();
app.findTextPreferences = app.changeTextPreferences= NothingEnum.nothing;
var foundLen = found.length;
$.writeln("\t"+"Total Index found: " + foundLen);
if (foundLen == 0) {
    alert ("Didn't find any index markers.\nPlease check the correct document is active.");
    }

for (var i = 0; i < foundLen; i++) {
    var item = found[i];
    var story = item.parentStory;
    var lastSelIP = item.insertionPoints.lastItem();
    var paras = story.texts.itemByRange(story.insertionPoints.item(0),lastSelIP).paragraphs;
    var currPara = story.paragraphs.item(paras.length - 1);
    var chars = currPara.texts.itemByRange (currPara.insertionPoints.item(0), lastSelIP).characters;
    var paraInx = paras.length - 1;
    var enCharInx = chars.length - 1;
    var spParaLen = myDocSp.stories[0].paragraphs[paraInx].length;
    //var spCharInx = (enCharInx <= spParaLen)? enCharInx:spParaLen - 1;
    var spCharInx = ParaLen(enCharInx,spParaLen);
    if(spCharInx != null){
        myDocEn.stories[0].paragraphs[paraInx].characters[enCharInx].duplicate (LocationOptions.BEFORE, myDocSp.stories[0].paragraphs[paraInx].characters[spCharInx]);
        }
    }
//==============
function ParaLen(enCharInx,spParaLen){
    $.writeln("English Index Column: " + enCharInx + "\n" + "Spanish Paragraph Length: " + spParaLen); 
    var spCharInx =0;
    if (enCharInx <= spParaLen){
        spCharInx = enCharInx;
        }
    else if (enCharInx > spParaLen){
        return null;
        }
    else{
        spCharInx = spParaLen - 1;
        }
    $.writeln("---> Placed on Spanish Column: " + spCharInx + "\n");
    return spCharInx;
    }

Best

Sunil 

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
Engaged ,
Nov 01, 2020 Nov 01, 2020

Copy link to clipboard

Copied

Hi Sunil,

thanks so much for check the code.

 

The reason is because in one paragraph the total length can be:

longer than english

lower than english

equal than english

 

If I use your code, you can see in this picture how the spanish text don't get all entries if the english are longer than spanish.

Screen Shot 2020-11-01 at 4.18.55 PM.png

 

 

 

 

 

 

 

 

 

 

 

I modify the script like this:

var myDocEn = app.documents[0];
var myDocSp = app.documents[1];

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "^I";
var found = myDocEn.findText();
app.findTextPreferences = app.changeTextPreferences= NothingEnum.nothing;
var foundLen = found.length;
$.writeln("\t"+"Total Index found: " + foundLen); 


if (foundLen == 0) {
  alert ("Didn't find any index markers.\nPlease check the correct document is active.");
}

for (var i = 0; i < foundLen; i++) {
  var item = found[i];
  var story = item.parentStory;
  var lastSelIP = item.insertionPoints.lastItem();
  var paras = story.texts.itemByRange(story.insertionPoints.item(0),lastSelIP).paragraphs;
  var currPara = story.paragraphs.item(paras.length - 1);
  var chars = currPara.texts.itemByRange (currPara.insertionPoints.item(0), lastSelIP).characters;
  var paraInx = paras.length - 1;
  var enCharInx = chars.length - 1;
  var spParaLen = myDocSp.stories[0].paragraphs[paraInx].length;

  //var spCharInx = (enCharInx <= spParaLen)? enCharInx:spParaLen - 1;
  var spCharInx = ParaLen(enCharInx,spParaLen);

  function ParaLen(enCharInx,spParaLen){
    $.writeln("English Index Column: " + enCharInx + "\n" + "Spanish Paragraph Length: " + spParaLen); 
    var spCharInx = 0
    if (enCharInx < spParaLen){
      spCharInx = enCharInx
      }
    else if (enCharInx = spParaLen){
      spCharInx = enCharInx -1
      }    
    else if (enCharInx > spParaLen){
      spCharInx = spParaLen - 1
      }   
    else{
      spCharInx = spParaLen - 1
    }
    $.writeln("---> Placed on Spanish Column: " + spCharInx + "\n");
      return spCharInx
      
  }

  myDocEn.stories[0].paragraphs[paraInx].characters[enCharInx].duplicate (LocationOptions.BEFORE, myDocSp.stories[0].paragraphs[paraInx].characters[spCharInx]);

}

 

 

Now works:

Screen Shot 2020-11-01 at 4.21.38 PM.png

 

 

Now works in all cases, please let me know if you see some advaice or impruvement.

Screen Shot 2020-11-01 at 4.24.27 PM.png

 

 

Best regards. 

 

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
Advocate ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

LATEST

You need to put your conditions like this:

if (enCharInx < spParaLen){
      spCharInx = enCharInx
      }
    else if (enCharInx == spParaLen){
      spCharInx = enCharInx -1
      }    
    else if (enCharInx > spParaLen){
      spCharInx = spParaLen - 1
      }   
    else{
      spCharInx = spParaLen - 1
    }

Best

Sunil

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