script that converts layers to separate smart objects

Community Beginner ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

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. 🙂 

TOPICS
Actions and scripting, How to

Views

366

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

correct answers 2 Correct Answers

Adobe Community Professional , Apr 13, 2021 Apr 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 = exec...

Likes

Translate

Translate
Adobe Community Professional , Apr 13, 2021 Apr 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()).putIdent...

Likes

Translate

Translate
Adobe Community Professional ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

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); 
}
};

 

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 Beginner ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

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. 🙂 

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
Adobe Community Professional ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

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

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 Beginner ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

Hhhmmmm its a bit weird but I just tried running the script again and now it seems to work with out any problems. Sorry about that Iám not sure what i did whong the first time. 🙂 

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
Adobe Community Professional ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

LATEST

 

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.

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