Indesign JavaScript 使用 findGrep 循环

Indesign JavaScript Loop with findGrep

我正在尝试结合现有的脚本,该脚本剪切 selected 文本并将其粘贴到主文本框架旁边的新锚定框架中,并带有 GREP Find/Replace 查询 I必须 select 相应的文本。 (由两个 ##before 和 after## 构成的每个文本都应放在单独的锚定文本框中。# 应删除,文本不应留在前一个文本框中。(cut/paste 而不是 copy/paste) 用于正文旁边的旁注。)

我几乎可以正常工作了,但由于重复

,我收到了错误消息

var mySelection=app.activeDocument.findGrep ()[0];

在我必须放入的“for 循环”的末尾,以便它重复查询。

我尝试稍微更改一下计数器,但实际上下面的脚本是迄今为止效果最好的。但我当然会收到一条错误消息,因为它并没有真正计算出来。

有人知道如何解决这个问题吗?我是脚本编写的绝对初学者,我已经束手无策了。

    app.findGrepPreferences = NothingEnum.nothing; 
    app.findGrepPreferences.findWhat="##(.+?)##( ?)";
    app.changeGrepPreferences.changeTo="";
    
    var mySelection = app.activeDocument.findGrep ()[0];

    var theOStyles = app.activeDocument.objectStyles.everyItem().name;   
    var myOStyleName = myDisplayDialog();   
    var myOStyle = app.activeDocument.objectStyles.item(myOStyleName); 
    
    
    for (var i = 0; i < mySelection.length; i++) {
      
    var myBounds = [ "0mm", "0mm", "30mm", "20mm"];
    mySelection.changeGrep();
    var myContent = mySelection.contents;
    
    var myFrame = mySelection.insertionPoints[0].textFrames.add(); // should this be [i]?
    myFrame.parent.parentTextFrames[0].characters.length;   // should this be [i]?
    myFrame.geometricBounds = myBounds;   
    myFrame.applyObjectStyle(myOStyle,  true);    
    myFrame.contents = myContent; //
    
    myFrame.fit (FitOptions.FRAME_TO_CONTENT);
    mySelection.contents = "";
    
    var mySelection=app.activeDocument.findGrep ()[0];  // this is probably what is wrong. Tried i+1 here
    }
       
    function myDisplayDialog()    
    {    
        var myFieldWidth = 150;    
        var myDialog = app.dialogs.add({name:"Objektstil für verankerten Rahmen"});    
        with(myDialog.dialogColumns.add())    
        {    
            with(dialogRows.add())    
            {    
                with(dialogColumns.add())    
                {    
                    staticTexts.add({staticLabel:"Objektstil:",     minWidth:myFieldWidth});    
                }    
                with(dialogColumns.add())    
                {    
                    var myOStyleDropDown = dropdowns.add({stringList:theOStyles,     selectedIndex:theOStyles.length-1});    
                }    
            }    
        }    
        var myResult = myDialog.show();    
        if(myResult == true)    
        {    
            var aOStyle = theOStyles[myOStyleDropDown.selectedIndex]    
            myDialog.destroy();    
        }    
        else    
        {    
            myDialog.destroy();    
            exit();    
        }    
        return aOStyle ;    
    } 

如果你几乎成功了,我有点惊讶,因为我没有。

所以我重写了主要部分,现在或多或少可以工作了:

// find all the patterns in the document
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "##(.+?)##( ?)";
var finds = app.activeDocument.findGrep();
if (finds.length == 0) { alert('Nothing was found'); exit()}

var obj_style = myDisplayDialog();

/* wrong solution ----------------------------------------------------
// loop through all the results and insert anchored frames
// for (var i=0; i<finds.length; i++) {
//    var anchored_frame = finds[i].insertionPoints[i].textFrames.add();
//    anchored_frame.contents = finds[i].contents.replace(/##/g, '');
//    anchored_frame.applyObjectStyle(obj_style, true);
//    anchored_frame.geometricBounds = ["0mm", "0mm", "30mm", "20mm"];
//    // anchored_frame.fit(FitOptions.FRAME_TO_CONTENT); // it doesn't // make much sense if you're defining 'geometricBounds'
// }
------------------------------------------------------------------- */

// loop through all the results and insert anchored frames
var i = finds.length;
while (i--) {
    var ip = finds[i].insertionPoints[0].index;
    var anchored_frame = finds[i].parentStory.insertionPoints[ip].textFrames.add();
    anchored_frame.contents = finds[i].contents.replace(/##/g, '');
    anchored_frame.applyObjectStyle(obj_style, true);
    anchored_frame.geometricBounds = ["0mm", "0mm", "15mm", "30mm"];
}

// find and remove all '##' from the document
// app.findGrepPreferences.findWhat = "##";
// app.changeGrepPreferences.changeTo = "";
// app.activeDocument.changeGrep();

// find and remove all '#...#' from the document
app.findGrepPreferences.findWhat = "##(.+?)##( ?)";
app.changeGrepPreferences.changeTo = "";
app.activeDocument.changeGrep();


// no changes below, except two new lines
// --------------------------------------

function myDisplayDialog() {
    var myFieldWidth = 150;
    var myDialog = app.dialogs.add({
        name: "Objektstil für verankerten Rahmen"
    });
    var theOStyles = app.activeDocument.objectStyles.everyItem().name; // the new line
    with(myDialog.dialogColumns.add()) {
        with(dialogRows.add()) {
            with(dialogColumns.add()) {
                staticTexts.add({
                    staticLabel: "Objektstil:",
                    minWidth: myFieldWidth
                });
            }
            with(dialogColumns.add()) {
                var myOStyleDropDown = dropdowns.add({
                    stringList: theOStyles,
                    selectedIndex: theOStyles.length - 1
                });
            }
        }
    }
    var myResult = myDialog.show();
    if (myResult == true) {
        var aOStyle = theOStyles[myOStyleDropDown.selectedIndex]
        myDialog.destroy();
    } else {
        myDialog.destroy();
        exit();
    }
    return app.activeDocument.objectStyles.item(aOStyle); // the new line
}