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?
1 Correct answer
Related topics include:
Explore related tutorials & articles
Copy link to clipboard
Copied
Why isn't this script functioning properly? What might be the issue with my script?
By Pubg32486011zfgs
if (layer.selected)
Copy link to clipboard
Copied
Ah, I typed so long that you already pointed out the issue in the meantime.
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;
};
Copy link to clipboard
Copied

