InDesign Text Modification Script Skips Content

社会主义新天地 提交于 2019-12-13 03:09:43

问题


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?

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


回答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";
  }
}



回答2:


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" );


来源:https://stackoverflow.com/questions/49429634/indesign-text-modification-script-skips-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!