skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    With Yuri's guidance I was able to solve my issue. I had to reorder the process slightly to account for how I was adding to the selection (maybe I didn't but it made sense to me and IT WORKS!). Here is the final code for anyone interested.

    //look for clipping path 'X', 'FCP', or 'x' and apply it
    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) {}
    }
    //copy image with clipping path
    app.copy();
    
    //remove clipping path
    app.selection[0].images[0].clippingPath.clippingType = ClippingPathType.NONE; 
    
    //apply Multiply blend mode
    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){}
    }
    
    //paste in place and add to current selection
    var clippedImage = app.selection[0];
    app.pasteInPlace();
    clippedImage.select(SelectionOptions.ADD_TO)
    
    //group selected images
    app.menuActions.itemByName("$ID/Group").invoke();
    

    What happens and how does it work? User selects an image and runs the script

    1. It searches for a clipping path to match a specific string.
    2. User is alerted to which path has been applied or if no match is found.
    3. Image is copied (for step 6).
    4. Path is removed.
    5. Multiply blending mode is applied.
    6. Image copied in step 3 is pasted in place (with clipping path).
    7. Pasted image selection is added to current selection (image with multiply blending mode).
    8. Selection is grouped.

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search