Adding a photo from a file as a clipping mask JavaScript

New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Hello!

I have a .psd file which has a rectangle (layername=rec). I don't understand how to modify my script so that the inserted image is centered in the rectangle and inserted as a clipping mask.

 

Below is my script for adding a photo from a folder to the active layer of my document. I'm sorry, I'm quite new to working with scripts in Photoshop. Thank you!

And as each photo is added, the "No Embedded Profile" dialog box pops up, waiting for me to choose a treatment. How can I set the default value in the script so that this window does not appear?

#target photoshop
app.bringToFront();
myDocName=app.activeDocument.name

function getFolder() {
    return Folder.selectDialog('Select Folder Import:', Folder('~'));  
} 

function importFolderAsLayers(selectedFolder) { 
    if (selectedFolder) { 
        var docList = selectedFolder.getFiles(); 
        for (var i = 0; i < docList.length; i++) { 
            open(docList[i]); // open documents from list 
            var docRef = app.activeDocument; 
            var docName = docRef.name; // get document name 
            if (docRef.layers.length > 1) { // merge multilayer documents 
                docRef.artLayers.add(); 
                docRef.mergeVisibleLayers(); 
            } 
            docRef.changeMode(ChangeMode.RGB); 
            // duplicate the layer into the new document 
            docRef.activeLayer.name='img'
            docRef.activeLayer.duplicate(documents[myDocName],ElementPlacement.PLACEATBEGINNING); 
            docRef.close(SaveOptions.DONOTSAVECHANGES); 
            // name duplicate layer using the original document name 
        } 
    } 
}
    
importFolderAsLayers(getFolder())

 

TOPICS
Actions and scripting , Windows

Views

52

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

@Alevtina M. 

 

A common approach is to save the current dialogs and disable them... then at the end of the script restore the original dialogs:

 

 

// Save the current dialog display settings
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
// Do stuff...
// Restore the dialogs
app.displayDialogs = savedDisplayDialogs;

 

https://gist.github.com/MarshySwamp/898ae3237305787d0683125166aeb480

 

Perhaps the easiest way to align is to load the rectangle layer as a selection, then centre align the image to the selection. The following example uses select all, which you would change:

 

// Align Active Layer to Select All.jsx

// Change as required
align2SelectAll('AdCH');
align2SelectAll('AdCV'); 

function align2SelectAll(method) {
    //www.ps-scripts.com/viewtopic.php?f=66&t=7036&p=35273&hilit=align+layer#p35273
    /* 
    //macscripter.net/viewtopic.php?id=38890
    AdLf = Align Left
    AdRg = Align Right
    AdCH = Align Centre Horizontal
    AdTp = Align Top
    AdBt = Align Bottom
    AdCV = Align Centre Vertical
    */

    app.activeDocument.selection.selectAll();

    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
    try {
        executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
    } catch (e) {}

    app.activeDocument.selection.deselect();

}

 

Here is the code to load the layer transparency as a selection, rather than using select all:

 

loadLayerTrans();

function loadLayerTrans() {
    // Active layer transparency to selection 
    var idset = stringIDToTypeID("set");
    var desc323 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var ref75 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idselection = stringIDToTypeID("selection");
    ref75.putProperty(idchannel, idselection);
    desc323.putReference(idnull, ref75);
    var idto = stringIDToTypeID("to");
    var ref76 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idchannel = stringIDToTypeID("channel");
    var idtransparencyEnum = stringIDToTypeID("transparencyEnum");
    ref76.putEnumerated(idchannel, idchannel, idtransparencyEnum);
    desc323.putReference(idto, ref76);
    executeAction(idset, desc323, DialogModes.NO);
}

 

https://gist.github.com/MarshySwamp/df372e342ac87854ffe08e79cbdbcbb5

 

Perhaps you could post a screenshot of your layers panel with the rectangle and image layer added by the script?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

LATEST

To create a clipping group on the upper active layer:

 

// Run menu item for Create Clipping Mask
app.runMenuItem(stringIDToTypeID('groupEvent'));

 

Or with the dialog off:

 

var idgroupEvent = stringIDToTypeID("groupEvent");
executeAction(idgroupEvent, undefined, DialogModes.NO);

 

Or raw Scripting Listener recorded code:

 

var idgroupEvent = stringIDToTypeID( "groupEvent" );
    var desc316 = new ActionDescriptor();
    var idnull = stringIDToTypeID( "null" );
        var ref73 = new ActionReference();
        var idlayer = stringIDToTypeID( "layer" );
        var idordinal = stringIDToTypeID( "ordinal" );
        var idtargetEnum = stringIDToTypeID( "targetEnum" );
        ref73.putEnumerated( idlayer, idordinal, idtargetEnum );
    desc316.putReference( idnull, ref73 );
executeAction(idgroupEvent, desc316, DialogModes.NO);

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines