Manish Ranjan
Manish Ranjan

Reputation: 1

How to Rename Layer With Auto Identify Horizontal and Verticle Frame [Photoshop Scripting]

i am new to photoshop scripting. i want to load a PSD File which have 2 Verticle Photos and 3 Horizontal Photos,I select that Layer. Now i want that All selected layer has been renamed to Verticle with VFrame and Horizontal With HFrame And PSD Saved With That Name 2v3h_My Name_0001 How can this be done? Can somebody please share the javascript For Photoshop?

Upvotes: 0

Views: 484

Answers (1)

Ghoul Fool
Ghoul Fool

Reputation: 6937

This is your first question, welcome to StackOverflow. There are lots of Photoshop Scripting guides out there. This one cover the basics.

The script you want needs to:

  • Loop over the layers (we'll leave out groups, that gets complicated fast)
  • Identify if a layer is horizontal or vertical
  • Rename the layer appropriately
  • Duplicate the document with the new name according to the number of horizontal layers
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF 

// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;
var vCount = 0;
var hCount = 0;


// main loop
for (var i = numOfLayers -1; i >= 0  ; i--) 
{
  // get a referenec to each layer as we loop over them
  var thisLayer = srcDoc.layers[i];

  // get the layer bounds of each layer as you go
  srcDoc.activeLayer = thisLayer;
  var lb = get_layer_bounds();

  // set a variable to for portrait or landscape
  var portrait = true;

  // if layer is wider than it is long,
  // it's landscape
  if (lb[0] > lb[1]) portrait = false;
  //alert(lb[0] + "," + lb[1] + "\n" + portrait + "\n" +thisLayer.name);

  // ignore the background layer
  // if there is one
  if (thisLayer.isBackgroundLayer == false)
  {
    if (portrait)
      {
        srcDoc.artLayers[i].name = "HFrame";
        // add one to the horizontal count
        hCount +=1
      }
    else
      {
        srcDoc.artLayers[i].name = "VFrame";
        // add one to the vertical count
        vCount+=1
      }
  }
}
var docName = srcDoc.name.substring(0, srcDoc.name.length -4);
var imgName = vCount + "v" + hCount + "h_" + docName + "_0001.psd";
duplicateIt(imgName);


// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL



// function GET LAYER BOUNDS ()
// ----------------------------------------------------------------
function get_layer_bounds()
{
  var x = parseFloat(app.activeDocument.activeLayer.bounds[0]);
  var y = parseFloat(app.activeDocument.activeLayer.bounds[1]);
  var x1 = parseFloat(app.activeDocument.activeLayer.bounds[2]);
  var y1 = parseFloat(app.activeDocument.activeLayer.bounds[3]);

  var selW = parseFloat(x1-x);
  var selH = parseFloat(y1-y);

  // return the results as an array
  return [selW, selH];
}


function duplicateIt(str)
{
  // duplicate image into new document
  if (arguments.length == 0) str = "temp";

  var id428 = charIDToTypeID( "Dplc" );
  var desc92 = new ActionDescriptor();
  var id429 = charIDToTypeID( "null" );
  var ref27 = new ActionReference();
  var id430 = charIDToTypeID( "Dcmn" );
  var id431 = charIDToTypeID( "Ordn" );
  var id432 = charIDToTypeID( "Frst" );
  ref27.putEnumerated( id430, id431, id432 );
  desc92.putReference( id429, ref27 );
  var id433 = charIDToTypeID( "Nm  " );
  desc92.putString( id433, str ); // name
  executeAction( id428, desc92, DialogModes.NO );
}

Upvotes: 1

Related Questions