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 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
...
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
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.
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:
Now works in all cases, please let me know if you see some advaice or impruvement.
Best regards.
Copy link to clipboard
Copied
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