Question

I found a JavaScript that will make every layer in Photoshop a smart object, which is awesome, but I am wondering if anyone can tweak it for me. I use this injunction with AppleScript. Due to not know much about Javascript I was hoping some kind person can tweak this to select all layers and make one smart object out of all layers in the current file.

The code I am using:

// MAIN

var doc = app.activeDocument;
var layers = doc.layers; //array of parent layer/layersets present in active document

for(var i = 0; i < layers.length; i++)
{
   if(!layers[i].isBackgroundLayer) createSmartObject(layers[i]);
}


//FUNCTIONS 

// create smartobject from specified layer (default is active layer)
function createSmartObject(layer)
{
   var doc = app.activeDocument;
   var layer = layer != undefined ? layer : doc.activeLayer;

   if(doc.activeLayer != layer) doc.activeLayer = layer;

   try
   {
      var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
      executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
      return doc.activeLayer;
   }
   catch(e)
   {
      return undefined;
   }
}// JavaScript Document

Update — Clarification

The above script takes each layer and one layer a time changes it to a smart object. I want it to select every layer in the document and make it into one smart object without flattening it.

So if I were to do it by hand for an example. I would make a PS doc with as many layers as I want. For argument sake lets say there is 3 layers. I would select the 3 layers(which is all the layer in the doc) and right click and select make smart object. Now I have one smart object with ever layer inside it.

Was it helpful?

Solution

I've re-written the code now that I understand that you don't want the layers to be flattened. This should do the trick!

app.preferences.rulerUnits = Units.PIXELS;

getThoseLayers();
createSmartObject();

function getThoseLayers ()
{
  var srcDoc = app.activeDocument;
  var numOfLayers = srcDoc.layers.length;

  // get the top layer
  srcDoc.activeLayer = srcDoc.layers[0];

  for (var i = numOfLayers -1; i >= 0; i--)
  {
    if(!srcDoc.layers[i].isBackgroundLayer)
    {
      addToSelection(srcDoc.layers[i].name)
    }
  }
}

function addToSelection (alayername)
{
  if (alayername == null) return

  try
  {
    var id66 = charIDToTypeID( "slct" );
    var desc14 = new ActionDescriptor();
    var id67 = charIDToTypeID( "null" );
    var ref12 = new ActionReference();
    var id68 = charIDToTypeID( "Lyr " );
    ref12.putName( id68, alayername );
    desc14.putReference( id67, ref12 );
    var id69 = stringIDToTypeID( "selectionModifier" );
    var id70 = stringIDToTypeID( "selectionModifierType" );
    var id71 = stringIDToTypeID( "addToSelection" );
    desc14.putEnumerated( id69, id70, id71 );
    var id72 = charIDToTypeID( "MkVs" );
    desc14.putBoolean( id72, false );
    executeAction( id66, desc14, DialogModes.NO );
  }
  catch(e)
  {
      alert("Whoops! Problem getting layer " + alayername)
  }
}

// create smartobject
function createSmartObject()
{
   try
   {
      var id325 = stringIDToTypeID( "newPlacedLayer" );
      executeAction( id325, undefined, DialogModes.NO );
   }
   catch(e)
   {
      alert("Whoops! Failed to make smart object " + e)
   }
}

It works on grabbing all the layers (apart from the background) and selects those layers. I haven't tested it extensively, but it seems to work within groups and even if you have two layers named the same. If it does fail, it will tell you. Hope you find it useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top