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

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

Views

2.9K

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

Community Expert , 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
...

Votes

Translate

Translate
LEGEND , 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
...

Votes

Translate

Translate
Adobe
Community Expert ,
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); 
}
};

 

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

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 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? 

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

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

Copy link to clipboard

Copied

 

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.

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
Participant ,
Dec 31, 2021 Dec 31, 2021

Copy link to clipboard

Copied

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.

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 ,
Jan 03, 2022 Jan 03, 2022

Copy link to clipboard

Copied

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 ,
Nov 19, 2022 Nov 19, 2022

Copy link to clipboard

Copied

LATEST

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

 

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