Search code examples
javascriptgrepadobe-indesign

Indesign JavaScript Loop with findGrep


I'm tryin to combine an existing script I have which cuts the selected text and pastes it in a new anchored frame next to the main text frame with a GREP Find/Replace Query I have to select the respective text. (Every text framed by two ##before and after## should be placed in a separate anchored textframe. The # should be removed and the text should not be left in the previous textframe. (cut/paste instead of copy/paste) Its for marginalia next to the main text.)

I nearly got it to work but I get error messages because of the repeating

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

at the end of the "for loop" which I have to put in so that it repeats the query.

I tried with changing the counter a bit but the script below actually is what works best so far. But I of course get an error message because it's not really counting up.

Has anyone an idea how to fix this? I am an absolute beginner with scripting and I am at my wits end.

    app.findGrepPreferences = NothingEnum.nothing; 
    app.findGrepPreferences.findWhat="##(.+?)##( ?)";
    app.changeGrepPreferences.changeTo="$1";
    
    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 ;    
    } 

Solution

  • I'm a bit surprised if you managed nearly got it to work, since I didn't.

    So I just rewrote the main part and now it works more or less:

    // 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
    }