Search code examples
javascriptadobe-indesign

InDesign scripts: paste into frames


I'm working on a script which pastes my clipboard in every selected frame. After searching around I didn't figure out how to paste something into a frame (or polygon).

I'm stuck at something like this:

function pasteSelection(mySelection) {
    for (var i = mySelection.length - 1; i > -1; i--) {
        mySelection[i].contents = app.paste();
    }
}

What should mySelection[i].contents = app.paste() be?


Solution

  • Here's something that will help, based on another answer I provided not long ago. For this snippet to paste anything, you MUST HAVE SELECTED TEXT in your Document. That's how this snippet know where to paste.

    var myDoc = app.activeDocument;
    
    if(app.documents.length != 0){
        if(app.selection.length == 1){
            try{           
                var frame1 = app.selection[0];
                frame1 = app.paste();//works
                //app.pasteWithoutFormatting(frame1);;works too
             }
            catch(e){
                alert ("Exception : " + e, "Exception");
            }
        }
     else{
        alert ("Please select text", "Selection");
      }  
    }
    else{
        alert("Something wrong");
    }
    

    Updated following comment: For this snippet I created an indesign document into which I created 2 objects. One object is a textBox into which I typed a bunch of text, and the second item is simply just a polygon I drew below the textBox. I did not set the content type of the polygon, I simply drew the polygon. In order to effectively find the pageItems I actually want and need, I used Script Labels, though using labels is not mandatory. As long as you have a mechanism to know you're dealing with the right object.

    The idea behind this snippet is really simple:

    1. Select the Source object
    2. Copy the selected object
    3. Select the Destination object
    4. Paste into the selected object

    .

    var myDoc = app.activeDocument;
    var source;
    var destination;
    
    for(var i = 0; i < myDoc.pageItems.length; i++)
    {
        if(myDoc.pageItems[i].label == "source") 
        {
            source = myDoc.pageItems[i];
            app.selection = myDoc.pageItems[i];
            app.copy();
            }
        else if(myDoc.pageItems[i].label == "destination")
        {
            destination = myDoc.pageItems[i];
            }
    
        if(source !== undefined && destination !== undefined)
        {
            break;
            }
    }
    app.selection = destination;
    app.pasteInto();