• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to use a script to find smart objects by name and replace images in that smart object

New Here ,
Apr 21, 2024 Apr 21, 2024

Copy link to clipboard

Copied

I tried the code below, it works but I want to hide the old layers in the smart object to avoid having 2 images overlap like in the photo (Layer smart object name "sm3" is overlapping 2 images and I want to delete it or hide old photos)
 
 
// Set the path to the input folder
var inputFolderPath = "C:\\Users\\Admin\\Downloads\\avt";

// Get a reference to the active document
var doc = app.activeDocument;

// Loop through each smart object layer and place the corresponding image
for (var i = 1; i <= 3; i++) {
    var smartObjName = "sm" + i;
    var smartObj = doc.layers.getByName(smartObjName);

    // Check if the smart object layer was found
    if (!smartObj) {
        alert(i + "\nCould not find layer '" + smartObjName + "'.");
        continue;
    }

    // Activate the smart object layer and open the contents
    doc.activeLayer = smartObj;
    //  replaceContents (inputFolderPath + "/artwork" + i + ".jpg");
    // Get a reference to the active document and place the corresponding image
    var subDoc = openSmartObject();
    var inputFilePath = inputFolderPath + "/a (" + i + ").png";
    var inputImgFile = new File(inputFilePath);
    placeScaleRotateFile(inputFilePath, 0, 0, 100, 100, 0)
    // Close and save the smart object contents
    subDoc.close(SaveOptions.SAVECHANGES);
};

// Close the original document without saving
//doc.close(SaveOptions.DONOTSAVECHANGES);
////// replace contents //////
function replaceContents(theName) {
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc244 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc244.putPath(idnull, new File(theName));
    executeAction(idplacedLayerReplaceContents, desc244, DialogModes.NO);
};
////// open smart object //////
function openSmartObject() {
    var desc2 = new ActionDescriptor();
    executeAction(stringIDToTypeID("placedLayerEditContents"), desc2, DialogModes.NO);
    return activeDocument;
};
////// place //////
function placeScaleRotateFile(file, xOffset, yOffset, theXScale, theYScale, theAngle) {
    var desc5 = new ActionDescriptor();
    desc5.putPath(charIDToTypeID("null"), new File(file));
    desc5.putEnumerated(charIDToTypeID("FTcs"), idQCSt = charIDToTypeID("QCSt"), charIDToTypeID("Qcsa"));
    var idOfst = charIDToTypeID("Ofst");
    var desc6 = new ActionDescriptor();
    var idPxl = charIDToTypeID("#Pxl");
    desc6.putUnitDouble(charIDToTypeID("Hrzn"), idPxl, xOffset);
    desc6.putUnitDouble(charIDToTypeID("Vrtc"), idPxl, yOffset);
    var idOfst = charIDToTypeID("Ofst");
    desc5.putObject(idOfst, idOfst, desc6);
    var idPrc = charIDToTypeID("#Prc");
    desc5.putUnitDouble(charIDToTypeID("Wdth"), idPrc, theYScale);
    desc5.putUnitDouble(charIDToTypeID("Hght"), idPrc, theXScale);
    desc5.putUnitDouble(charIDToTypeID("Angl"), charIDToTypeID("#Ang"), theAngle);
    desc5.putBoolean(charIDToTypeID("Lnkd"), false);
    executeAction(charIDToTypeID("Plc "), desc5, DialogModes.NO);
    return app.activeDocument.activeLayer;
};
TOPICS
Actions and scripting , Experiment , Windows

Views

113

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
Adobe
Community Expert ,
Apr 22, 2024 Apr 22, 2024

Copy link to clipboard

Copied

Here in the following section of code, I have added an alert to pause the script so that you can screenshot the layers panel to show which layers you are talking about:

 

// Activate the smart object layer and open the contents
doc.activeLayer = smartObj;
//  replaceContents (inputFolderPath + "/artwork" + i + ".jpg");
// Get a reference to the active document and place the corresponding image
var subDoc = openSmartObject();
var inputFilePath = inputFolderPath + "/a (" + i + ").png";
var inputImgFile = new File(inputFilePath);
placeScaleRotateFile(inputFilePath, 0, 0, 100, 100, 0)
alert("Take a screenshot of the layers panel of the open smart object")!
// Close and save the smart object contents
subDoc.close(SaveOptions.SAVECHANGES);

 

Votes

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
New Here ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

I tested it and it shows like this. Which code helps me hide or delete "Layer 1" in that smart object?

Votes

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 ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

@thanhN 

 

The following code will ensure that only the top layer is active (optional?) and visible and that all other top-level layers are invisible:

 

try {
    // Ensure that the top layer is active/selected, even if no layers are currently selected
    activeDocument.activeLayer.link(activeDocument.activeLayer);
    // Set the top layer's visibility to off
    app.activeDocument.layers[0].visible = false;
    // Toggle off the visibility of all other layers except the top layer
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    list.putReference(reference);
    descriptor.putList(s2t("null"), list);
    descriptor.putBoolean(s2t("toggleOptionsPalette"), true);
    executeAction(s2t("show"), descriptor, DialogModes.NO);
} catch (e) {
    alert("Error!" + "\r" + e + ' ' + e.line);
}

 

 

If you are not concerned about having the top layer active/selected, you can remove the first line from the previous code or use the following:

 

 

// Make all layers invisible
for (var i = 0; i < app.activeDocument.layers.length; i++) {
    app.activeDocument.layers[i].visible = false;
}
// Set the top layer's visibility to on
app.activeDocument.layers[0].visible = true;

 

 

Or:

 

 

for (var i = 0; i < app.activeDocument.layers.length; i++) {
    if (app.activeDocument.layers[i] != app.activeDocument.layers[0]) {
        app.activeDocument.layers[i].visible = false;
    }
}

 

 

Votes

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 ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

LATEST

@thanhN 

 

How did the suggested code work for you?

Votes

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