Search code examples
javascriptcopygroupingpasteadobe-indesign

InDesign - Javascript - Copy Object, Paste in Place, Group New Object with Original Selection


EDITED FOR CLARITY

I've created a script for InDesign to improve my workflow.

I select an image, then run the script, which performs as follows:

  1. Copies selected image (for use in step #3).
  2. Applies "multiply" effect to the selected image.
  3. Pastes-in-place the original image (with no multiply effect).
  4. Looks for a specific photoshop path name (either 'X', 'FCP', or 'x').
  5. Applies first path it finds and alerts which was selected or alerts "no valid path exists" if none are available.
  6. Script Ends with the pasted image selected.

Everything functions as intended; however, I would like to select BOTH images (the image with a clipping path and the image with the multiply effect) and group them. I'm getting caught up on how to select both images.

Here's the original code (I intend to make Yuri's edits to the copy and paste commands):

app.menuActions.item("$ID/Copy").invoke();

var myProperties = {  
    blendMode : BlendMode.MULTIPLY,  
    opacity : 100 
    };  
for (i=0; i<app.selection.length; i++)
{
try {
app.selection[i].images[0].transparencySettings.blendingSettings.properties = myProperties;  
    }catch(e){}
}
app.menuActions.item("$ID/Paste in Place").invoke();
for (j=0; j<app.selection.length; j++)
{
try {
    var myPathName = app.selection[0].images[0].clippingPath.photoshopPathNames; 
    var myPath = app.selection[0].images[0].clippingPath;
    function working() { 
        for (var i = 0; i < myPathName.length; i++) { 
        if (myPathName[i] === "X") {
            myPath.appliedPathName = myPathName[i];
            alert ("X path activated");
            return i; 
            } else if (myPathName[i] === "FCP") {
                myPath.appliedPathName = myPathName[i];
                alert ("FCP path activated");
                return i; 
            } else if (myPathName[i] === "x") {
                myPath.appliedPathName = myPathName[i];
                alert ("x path activated");
                return i; 
            }
        }
        alert ("No valid path exists");
    }
    working();
} catch (e) {}
}

Apologies if it's not the cleanest code. I started learning javascript in my free time and this is one of the first scripts I wrote.


Solution

  • Still not sure if I understand the problem right. If you need to add some item to current selection it can be done this way:

    var item1 = app.selection[0]; // some selected item
    
    app.paste(); // paste item which is selected now
    
    item1.select(SelectionOptions.ADD_TO) // add the item1 to current selection
    
    app.menuActions.itemByName("$ID/Group").invoke(); // group selected items
    

    edited to change app.selected to app.selection because selected returned an error