InDesign脚本 - 表格页脚和插入点

时间:2016-04-01 15:08:17

标签: javascript scripting adobe-indesign

我为inDesign开发了一个脚本,它逐页循环设计文件,扫描文本框架,查找文本中的特定代码,并检查代码是否在列表中的每个代码,如果是,脚本将在代码后插入一个小图像,如果不是脚本传递到下一个文本框架。

因为示例比大话更好,这是一个例子:

enter image description here

在代码之后插入了这个小的黑色/灰色图像。除了最后一行表格上的代码外,每个代码都能完美地工作。

在请求帮助之前,我已经检查了几个问题: -Script正确检测代码(我甚至可以使用脚本编辑文本以确保)。 - 表格单元格有足够的空间来插入图像(我也试图为行提供更高的高度)。 - 我试图调整图像大小,使图像变小。 - 如果我在最后一行之后添加一个空行,脚本会正确添加图像...所以这真的是最后一行问题......

现在这是脚本失败的部分

        app.findTextPreferences.findWhat = currentREF;
        var myFoundItems = app.activeDocument.findText();

        for ( var bh = 0; bh < myFoundItems.length; bh++ ) {


        myLastIns = myFoundItems[bh].insertionPoints.length;
        myInsPointOk = myFoundItems[bh].insertionPoints[myLastIns - 1];

        if ( bh == 0 ) {
            myTf = myInsPointOk.parentTextFrames[0];
        }

        try {
            //Insert/place the logo
            var myImgPath = logoImage;
            var myBlockImg = myInsPointOk.rectangles.add( {strokeWeight: 0, strokeColor: "None", fillColor: "None", geometricBounds: [ 0, 0, 1, 1 ]} );
            myBlockImg.contentType = ContentType.graphicType;
            myBlockImg.anchoredObjectSettings.anchoredPosition = AnchorPosition.INLINE_POSITION;
            myBlockImg.anchoredObjectSettings.anchorXoffset = 0;
            myBlockImg.anchoredObjectSettings.anchorYoffset = 0;
            myBlockImg.place( File( myImgPath ) );
            myBlockImg.fit( FitOptions.FRAME_TO_CONTENT );
        } catch ( e ) {

            //Warning, the code has been find but the script didn't success to insert it
            $.writeln( "La référence " + normalFormatRef + " à été trouvée dans le fichier " + app.activeDocument.name + " mais le script n'à pas réussit à insérer le picto." );
            arrError.push( "La référence " + normalFormatRef + " à été trouvée dans le fichier " + app.activeDocument.name + " mais le script n'à pas réussit à insérer le picto." );

        }
        }

我猜是脚本没有在最后一行的表格中找到插入点...但为什么呢? 或许我猜错了......

最后,每次尝试在最后一行添加图像时,脚本都会停在该行上...

       var myBlockImg = myInsPointOk.rectangles.add( {strokeWeight: 0, strokeColor: "None", fillColor: "None", geometricBounds: [ 0, 0, 1, 1 ]} );

这就是为什么我猜它是插入点失败的原因。

2 个答案:

答案 0 :(得分:1)

首先,谢谢你的回答! 然后我终于找到了解决方案,我发布在下面:

&#13;
&#13;
if ( myFoundItems[bh].parent.constructor.name == "Cell" ) {
         //Only for text include in cells, temporary increase the height of the parent's row
         var previousRowHeight = myFoundItems[bh].parent.minimumHeight;
         myFoundItems[bh].parent.minimumHeight = "15mm";
         myFoundItems[bh].parent.parent.parent.fit( FitOptions.FRAME_TO_CONTENT );
}

         var myBlockImg = myFoundItems[bh].parent.insertionPoints[-1].rectangles.add( {strokeWeight: 0,  strokeColor: "None", fillColor: "None", geometricBounds: [ 0, 0, 0.1, 0.1 ]} );
         myBlockImg.contentType = ContentType.graphicType;
         myBlockImg.anchoredObjectSettings.anchoredPosition = AnchorPosition.INLINE_POSITION;
         myBlockImg.anchoredObjectSettings.anchorXoffset = 0;
         myBlockImg.anchoredObjectSettings.anchorYoffset = 0;
         myBlockImg.place( File( myImgPath ) );
         myBlockImg.fit( FitOptions.FRAME_TO_CONTENT );

if ( myFoundItems[bh].parent.constructor.name == "Cell" ) {
        //Only for text include in cells, reset the height of the parent's row
        myFoundItems[bh].parent.minimumHeight = previousRowHeight;
        myFoundItems[bh].parent.parent.parent.fit( FitOptions.FRAME_TO_CONTENT );
}
&#13;
&#13;
&#13;

要解释一下,我发现的是增加行的高度是不够的,它还需要适合父母的文本块。

答案 1 :(得分:0)

没什么明显的。让我提出这种方法。我通常选择对象样式而不是编辑脚本中的对象属性。这样,用户可以在脚本运行后轻松编辑对象外观和位置。它也避免了适合的呼叫。

&#13;
&#13;
var main = function(){
	var doc, fgp, cgp, found = [], n = 0, text, parent, rect, os,
	osProps = {
			name:"picto",
	},
	picto = File ( Folder.desktop+"/picto.ai"), 
	uip = app.scriptPreferences.properties;
	
	if ( !app.properties.activeDocument) return;
	
	if ( !picto.exists ) {
		alert("The picto file could't be found!" );
		return;
	}

	doc = app.activeDocument;
	fgp = app.findGrepPreferences.properties;
	cgp = app.changeGrepPreferences.properties;
	
	app.findGrepPreferences = app.changeGrepPreferences = null;
	
	app.findGrepPreferences.findWhat = "(\\d{2}\\.){4}\\d{2}";
	
	found = doc.findGrep();
	n = found.length;
	
	if ( !n ) return;
	
	app.scriptPreferences.enableRedraw = false;
	app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
	
	os = doc.objectStyles.item ("picto");
	!os.isValid && os = doc.objectStyles.add(osProps);
	
	while ( n-- ) {
		text = found[n];
		parent = text.parent;
		if ( parent instanceof Cell 
		&& parent.parentRow.cells[-1]==parent ) {
			rect = text.insertionPoints[-1].rectangles.add({geometricBounds: [ 0, 0, 1, 1 ]});
			rect.appliedObjectStyle = os;
			rect.place ( picto );
		}
	}
	
	app.findGrepPreferences.properties = fgp;
	app.changeGrepPreferences.properties = cgp;
	app.scriptPreferences.properties = uip;
}

var u;
app.doScript ( main, u, u, UndoModes.ENTIRE_SCRIPT, "Add picto" );
&#13;
&#13;
&#13;

我也选择Grep over text进行文本搜索,因为它可以避免循环参考。但是,如果引用具有可变模式,那么它可能不是一个很好的匹配。

The result…