InDesign文本修改脚本跳过内容

时间:2018-03-22 13:12:20

标签: adobe-indesign

这个InDesign Javascript迭代textStyleRanges并使用一些特定的appliedFont转换文本,然后分配一个新的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";
  }
}

但总有一些范围不会发生这种情况。我尝试向前方向或向后方向迭代或在转换之前将元素放入数组中或在同一次迭代中更新appliedFont或将其更新为另一个迭代。某些范围仍未完全转换。

我这样做是为了将基于字形的非Unicode编码编码的梵文文本转换为Unicode。其中一些涉及重新定位元音符号等,并且更改代码以使用查找/替换机制可能是可能的,但是很多返工。

发生了什么事?

enter image description here

另请参阅:http://cssdk.s3-website-us-east-1.amazonaws.com/sdk/1.0/docs/WebHelp/app_notes/indesign_text_frames.htm#Finding_and_changing_text

此处示例:https://www.dropbox.com/sh/7y10i6cyx5m5k3c/AAB74PXtavO5_0dD4_6sNn8ka?dl=0

2 个答案:

答案 0 :(得分:1)

这是未经测试的,因为我无法对您的文档进行测试,但请尝试使用getElements(),如下所示:

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

答案 1 :(得分:1)

有效的方法是使用超链接文本源,因为它们坚持使用真正的文本对象。然后,您可以编辑这些源文本,即使它们实际上已移动到流程中的其他位置。

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