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

Convert selected layers into smart objects via script

Explorer ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

I have created the following Photoshop script designed to convert selected layers into smart objects: 

// Function to convert selected layers to smart objects
function convertToSmartObject() {
    var doc = app.activeDocument;
    var selectedLayers = getSelectedLayers(doc);

    // Iterate through selected layers
    for (var i = 0; i < selectedLayers.length; i++) {
        var layer = selectedLayers[i];
        // Convert the layer to a smart object
        convertLayerToSmartObject(doc, layer);
    }
}

// Function to get selected layers
function getSelectedLayers(doc) {
    var selectedLayers = [];
    var layers = doc.layers;
    for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];
        if (layer.selected) {
            selectedLayers.push(layer);
        }
    }
    return selectedLayers;
}

// Function to convert a layer to a smart object
function convertLayerToSmartObject(doc, layer) {
    doc.activeLayer = layer;
    createSmartObject();
}

// Function to create a smart object
function createSmartObject() {
    var idnewPlacedLayer = stringIDToTypeID('newPlacedLayer');
    executeAction(idnewPlacedLayer, undefined, DialogModes.NO);
}

// Call the function to convert selected layers to smart objects
convertToSmartObject();

 

Why isn't this script functioning properly? What might be the issue with my script?

TOPICS
Actions and scripting , Windows

Views

432

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
People's Champ ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

quote

Why isn't this script functioning properly? What might be the issue with my script?

By @Pubg32486011zfgs

 

Where did you see this property in a layer?
if (layer.selected)

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 ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

Ah, I typed so long that you already pointed out the issue in the meantime. 

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 ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

Did you create the Script or did you use ChatGPT or a comparable application? 

Because I wonder where you got the DOM-property »selected« for Layers. 

You may be better off using AM-code. (for an example see code below) 

 

Are Groups involved? Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible? 

 

// 2023, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
    alert (collectSelectedLayersBounds ())
};
////// collect bounds of selected layers //////
function collectSelectedLayersBounds () {
// set to pixels;
    var originalRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var desc = executeActionGet(ref);
    if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
    else {var theAdd = 1};
    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
    desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
    var c = desc.count;
    var selectedLayers = new Array();
    // run through selected layers;
    for(var i=0;i<c;i++){
    var theIndex = desc.getReference( i ).getIndex()+theAdd;
    // get id for solid color layers;
    try {
    var ref = new ActionReference();
    ref.putIndex( charIDToTypeID("Lyr "), theIndex ); 
    var layerDesc = executeActionGet(ref);
    var theName = layerDesc.getString(stringIDToTypeID('name'));
    var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
    var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
    var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
    selectedLayers.push([theName, theIdentifier, theseBounds]);
    } catch (e) {};
    };
    // if only one:
    }else{
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var layerDesc = executeActionGet(ref);
    try {
    var theName = layerDesc.getString(stringIDToTypeID('name'));
    var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
    var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
    var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
    selectedLayers = [[theName, theIdentifier, theseBounds]]
    } catch (e) {};
    };
// reset;
    app.preferences.rulerUnits = originalRulerUnits;
    return selectedLayers;
};

 

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 ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

LATEST

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