0

This InDesign Javascript iterates over textStyleRanges and converts text with a few specific appliedFont's and later assigns a new appliedFont:-

var textStyleRanges = [];
for (var j = app.activeDocument.stories.length-1; j >= 0 ; j--)
  for (var k = app.activeDocument.stories.item(j).textStyleRanges.length-1; k >= 0; k--)
    textStyleRanges.push(app.activeDocument.stories.item(j).textStyleRanges.item(k));

for (var i = textStyleRanges.length-1; i >= 0; i--) {
  var myText = textStyleRanges[i];
  var converted = C2Unic(myText.contents, myText.appliedFont.fontFamily);
  if (myText.contents != converted)
    myText.contents = converted;        

  if (myText.appliedFont.fontFamily == 'Chanakya' 
  || myText.appliedFont.fontFamily ==  'DevLys 010' 
  || myText.appliedFont.fontFamily ==  'Walkman-Chanakya-905') {          
    myText.appliedFont = app.fonts.item("Utsaah");
    myText.composer="Adobe World-Ready Paragraph Composer";
  }
}

But there are always some ranges where this doesn't happen. I tried iterating in the forward direction OR in the backward direction OR putting the elements in an array before conversion OR updating the appliedFont in the same iteration OR updating it a different one. Some ranges are still not converted completely.

I am doing this to convert the Devanagari text encoded in glyph based non-Unicode encoding to Unicode. Some of this involves repositioning vowel signs etc and changing the code to work with find/replace mechanism may be possible but is a lot of rework.

What is happening?

enter image description here

See also: http://cssdk.s3-website-us-east-1.amazonaws.com/sdk/1.0/docs/WebHelp/app_notes/indesign_text_frames.htm#Finding_and_changing_text

Sample here: https://www.dropbox.com/sh/7y10i6cyx5m5k3c/AAB74PXtavO5_0dD4_6sNn8ka?dl=0

Himanshu
  • 2,384
  • 2
  • 24
  • 42
  • As you may change text contents, you may recompose the story thus loosing further text references. I am not exactly sure about what you are trying to achieve so it's hard to offer strong advice. – Loic Mar 22 '18 at 18:04
  • Rather than looping through the text, have you considered scripted search/replace? this would allow you to find text with a given format and replace it with the same (or different) text with new formatting – cybernetic.nomad Mar 22 '18 at 18:41
  • @cybernetic.nomad Yes that's an option to consider if there are no others. The code is borrowed from a web based script and has multi-stage find/replace. Will need lots of refactoring. – Himanshu Mar 26 '18 at 05:22
  • @Loic Yes that's probably it but even saving the contents in an array before updating doesn't help. – Himanshu Mar 26 '18 at 05:23

2 Answers2

1

This is untested since I'm not able to test against your document, but try using getElements() like below:

var doc = app.activeDocument;
var stories = doc.stories;
var textStyleRanges = stories.everyItem().textStyleRanges.everyItem().getElements();

for (var i = textStyleRanges.length-1; i >= 0; i--) {
  var myText = textStyleRanges[i];
  var converted = C2Unic(myText.contents, myText.appliedFont.fontFamily);
  if (myText.contents != converted)
    myText.contents = converted;        

  if (myText.appliedFont.fontFamily == 'Chanakya' 
  || myText.appliedFont.fontFamily ==  'DevLys 010' 
  || myText.appliedFont.fontFamily ==  'Walkman-Chanakya-905') {          
    myText.appliedFont = app.fonts.item("Utsaah");
    myText.composer="Adobe World-Ready Paragraph Composer";
  }
}
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
  • Thanks, it traverses all text ranges. Just leaves some font unchanged. I have added a link to the file if you'd like to try. – Himanshu Mar 27 '18 at 07:21
1

A valid approach is to use hyperlink text sources as they stick to the genuine text object. Then you can edit those source texts even if they were actually moved elsewhere in the flow.

//Main routine
var main = function() {
 
  //VARS
 var doc = app.properties.activeDocument,
 fgp = app.findGrepPreferences.properties,
 cgp = app.changeGrepPreferences.properties,
 fcgo = app.findChangeGrepOptions.properties,
 text, str,
 found = [], srcs = [], n = 0;
 
  //Exit if no documents
 if ( !doc ) return;
 
  app.findChangeGrepOptions = app.findGrepPreferences = app.changeGrepPreferences = null;
  
  //Settings props
 app.findChangeGrepOptions.properties = {
  includeHiddenLayers:true,
  includeLockedLayersForFind:true,
  includeLockedStoriesForFind:true,
  includeMasterPages:true,
 }
 app.findGrepPreferences.properties = {
  findWhat:"\\w",
 }

  //Finding text instances
 found = doc.findGrep();
 n = found.length;
  //Looping through instances and adding hyperlink text sources
  //That's all we do at this stage
 while ( n-- ) {
  srcs.push ( doc.hyperlinkTextSources.add(found[n] ) );
 }

  //Then we edit the stored hyperlinks text sources 's texts objects contents
 n = srcs.length;
 while ( n-- ) {
  text = srcs[n].sourceText;
  str = text.contents;
  text.contents = str+str+str+str; 
 }
 
  //Eventually we remove the added hyperlinks text sources
 n = srcs.length;
 while ( n-- ) srcs[n].remove();
 
 //And reset initial properties
 app.findGrepPreferences.properties = fgp;
 app.changeGrepPreferences.properties = cgp;
 app.findChangeGrepOptions.properties =fcgo;
}

//Running script in a easily cancelable mode
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Loic
  • 2,173
  • 10
  • 13