InDesign 脚本 - Table 页脚和插入点

InDesign Script - Table footer and insertion point

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

实例胜于空谈,举个例子:

在代码之后,它插入了这张 black/grey 小图片。这对每个代码都非常有效,除了最后 table 行的代码。

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

现在这是脚本失败的部分

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

        }
        }

我猜脚本没有在最后一行的 table 中找到插入点...但为什么呢? 又或许我没猜对...

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

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

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

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

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

我还选择了 Grep over text 进行文本搜索,因为它可以避免通过 ref 循环。但是,如果 refs 具有可变模式,则可能不是很好的匹配。

首先感谢您的回答! 然后我终于找到了解决办法,我post如下:

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

解释了一会儿,我发现增加行的高度是不够的,它还需要适应父文本块。