Skip to main content
Participant
April 13, 2021
Answered

script that converts layers to separate smart objects

  • April 13, 2021
  • 5 replies
  • 5533 views

Hi I have a photoshop file that has alot of layers, and I would like to be able to convert all the selected layers in one go to separate Smar Objects, the problem is that when you do that it flatens all the layers in to one Smart obeject layer, but I want to keep all the layers but have them as Smart Objects instead. 

 

Is there a script that I can run to do this, I have been searching but with out any luck. 
Hope some one has a script that can do this. 🙂 

Correct answer Kukurykus

 

if (documents.length) {
	sTT = stringIDToTypeID; (ref  = new ActionReference()).putProperty(sTT('property'), json = sTT('json'))
	ref.putEnumerated(sTT('document'), sTT('ordinal'), sTT('targetEnum')), (dsc = new ActionDescriptor()).putReference(sTT('null'), ref);
	dsc.putBoolean(sTT('includeAncestors'), false), dsc.putBoolean(sTT('selectedLayers'), true), evl = eval('(' + executeAction(sTT('get'), dsc).getString(json) + ')')
	function slct(layerSetValue) {(ref = new ActionReference()).putIdentifier(sTT('layer'), layerSetValue.id), dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)}
	(function(v){while(v && lngth = v.length) {var lst = lngth - 1, layerSet = v[lst]; callee(layerSet.layers), slct(layerSet), executeAction(sTT('newPlacedLayer')), v.length = lst}})(evl.layers)
}

After changing 'document' to 'layer' only top "layer(Sets)" of selected layers will be turned into smart objects.

5 replies

Inspiring
January 24, 2025

I frequently have to convert layers to smart objects, and sometimes I'd like to convert many layers at once into separate smart objects. If I select all of these layers and convert to smart objects, I end up with a single smart object layer. I'd like the option to convert them into separate objects, rather than what I do now: do each layer one at a time. It's tedious and wastes time.

c.pfaffenbichler
Community Expert
Community Expert
January 24, 2025

Have you tried @Kukurykus ’ Script? 

edited

Stephen Marsh
Community Expert
Community Expert
January 4, 2022
Stephen Marsh
Community Expert
Community Expert
November 20, 2022

A related link here to convert all or selected layers to *linked* smart object layers, rather than embedded:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/converting-to-linked-smart-object/td-p/13357539

 

pentagramwookie
Inspiring
December 31, 2021

I don't understand why this isn't a built in function in Photoshop yet. It should be right under 'Convert to Smart Object', 'Convert Selected Layers to Individual Smart Objects' if more than one layer is selected. Anyone know if there's a feature request for this?

 

Thanks for the script though!! This is the 3rd or possibly 4th computer I have installed this script on or iterations of it over the years.

 

Uploading it as a .txt for anyone else looking for it. Resave as a .js file for Photoshop to recognize.

Kukurykus
KukurykusCorrect answer
Legend
April 13, 2021

 

if (documents.length) {
	sTT = stringIDToTypeID; (ref  = new ActionReference()).putProperty(sTT('property'), json = sTT('json'))
	ref.putEnumerated(sTT('document'), sTT('ordinal'), sTT('targetEnum')), (dsc = new ActionDescriptor()).putReference(sTT('null'), ref);
	dsc.putBoolean(sTT('includeAncestors'), false), dsc.putBoolean(sTT('selectedLayers'), true), evl = eval('(' + executeAction(sTT('get'), dsc).getString(json) + ')')
	function slct(layerSetValue) {(ref = new ActionReference()).putIdentifier(sTT('layer'), layerSetValue.id), dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)}
	(function(v){while(v && lngth = v.length) {var lst = lngth - 1, layerSet = v[lst]; callee(layerSet.layers), slct(layerSet), executeAction(sTT('newPlacedLayer')), v.length = lst}})(evl.layers)
}

After changing 'document' to 'layer' only top "layer(Sets)" of selected layers will be turned into smart objects.

c.pfaffenbichler
Community Expert
Community Expert
April 13, 2021

It’s not elegant (and therefore not as fast as possible) but you can give it a try. 

//2018, use it at your own risk
if (app.documents.length > 0) {
smartifyAndGetSelectedLayersIdxEtc()
};
////// get array of arrays of smart objects witrh index, center and half-dimensions //////
function smartifyAndGetSelectedLayersIdxEtc(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
	desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
	 var c = desc.count;
	 var selectedLayers = new Array();
	 for(var i=0;i<c;i++){
		try{ 
			activeDocument.backgroundLayer;
			selectedLayers.push(  desc.getReference( i ).getIndex() );
		}catch(e){
			selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
		};
	 }
 }else{
	var ref = new ActionReference();
	ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
	ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
	try{ 
		activeDocument.backgroundLayer;
		selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
	}catch(e){
		selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
	};
};
////////////////////////////////////
var theArray = new Array;
var theIDs = new Array;
for (var m = 0; m < selectedLayers.length; m++) {
var thisIndex = selectedLayers[m];
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), thisIndex); 
var layerDesc = executeActionGet(ref);
var thisID = layerDesc.getInteger(stringIDToTypeID("layerID"));
var theKind = layerDesc.getInteger(stringIDToTypeID("layerKind"));
var theName = layerDesc.getString(stringIDToTypeID("name"));
var theVisibility = layerDesc.getInteger(stringIDToTypeID("visible"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var halfWidth = theBounds.getUnitDoubleValue(stringIDToTypeID("width")) / 2;
var halfHeight = theBounds.getUnitDoubleValue(stringIDToTypeID("height")) / 2;
var theX = theBounds.getUnitDoubleValue(stringIDToTypeID("left")) + halfWidth;
var theY = theBounds.getUnitDoubleValue(stringIDToTypeID("top")) + halfHeight;
// is normal, shape, smart object, pattern, gradiet, solid color;
if (theKind == 1 || theKind == 4 || theKind == 5 || theKind == 9 || theKind == 10 || theKind == 11) {
if (theVisibility == true) {
theIDs.push ([thisID, theX, theY, halfWidth, halfHeight, theName])
}
}
};
////////////////////////////////////
for (var n = 0; n < theIDs.length; n++) {
if (hasSmartObject(theIDs[n][0]) == false) {
	try {
		selectLayerByID(theIDs[n][0], false);
		var id557 = charIDToTypeID( "slct" );
		var desc108 = new ActionDescriptor();
		var id558 = charIDToTypeID( "null" );
		var ref77 = new ActionReference();
		var id559 = charIDToTypeID( "Mn  " );
		var id560 = charIDToTypeID( "MnIt" );
		var id561 = stringIDToTypeID( "newPlacedLayer" );
		ref77.putEnumerated( id559, id560, id561 );
		desc108.putReference( id558, ref77 );
		executeAction( id557, desc108, DialogModes.NO );
		theArray.push([getLayerId(app.activeDocument.activeLayer), theIDs[n][1], theIDs[n][2], theIDs[n][3], theIDs[n][4], theIDs[n][5]]);
		} catch (e) {}
	}
else {theArray.push(theIDs[n])};
};
////////////////////////////////////
// select;
selectLayerByID(theArray[0][0],false);
for (var a = 1; a < theArray.length; a++) {selectLayerByID(theArray[a][0], true)};
//return;
return theArray
////////////////////////////////////
};
// by mike hale, via paul riggott;
function getLayerId(theLayer){
// http://forums.adobe.com/message/1944754#1944754
app.activeDocument.activeLayer = theLayer;
//Assumes activeDocument and activeLayer	
    var ref = new ActionReference(); 
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); 
    d = executeActionGet(ref); 
return d.getInteger(charIDToTypeID('LyrI')); 
};
////// smart object or not //////
function hasSmartObject(idx){
var ref = new ActionReference(); 
ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "smartObject" )); 
//ref.putIndex( charIDToTypeID( "Lyr " ), idx);
ref.putIdentifier( charIDToTypeID( "Lyr " ), idx);
var desc = executeActionGet(ref);
if(desc.hasKey(stringIDToTypeID('smartObject'))) {return true}
else {return false};
};
////// based on code by mike hale and paul riggott //////
function selectLayerByID(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};

 

DalleDJAuthor
Participant
April 13, 2021

Hi and thanks for your fast respons, but when I run your script it dos not seem to do anything. 🙂 

But I just found this following script and that seems to do the job but it is not very farst but it gets the job down.

 

s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var sel = executeActionGet(r).hasKey(p) ? executeActionGet(r).getList(p) : null;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasBackgroundLayer'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var shift = executeActionGet(r).getBoolean(p) ? 0: 1

if (sel) {
for (var i = 0; i < sel.count; i++) {
(r = new ActionReference()).putIndex(s2t("layer"), sel.getReference(i).getIndex() + shift);
(d = new ActionDescriptor()).putReference(s2t("null"), r);
executeAction(s2t("select"), d, DialogModes.NO)
executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
}
}

 

But thanks. 🙂 

c.pfaffenbichler
Community Expert
Community Expert
April 13, 2021

Could you post a screenshot including the Layers Panel of the file the previous Script failed to operate on?