0

Having trouble specifically with artboards for Photoshop scripting.

What I want it to do:

I have a photoshop file with 8 artboards, and I want to apply changes (name changes) to the selected artboards via scripting.

What it actually does:

So far the script I have (that I found online) will rename ALL of the artboards in my file instead of the ones selected. I've tried editing/using Photoshop scripts that will work on regular layers, but can't figure out how to get it to work for selected artboards.

Is this possible to script?

0

Yes, it's possible. Since Artboards are just hacked Groups (which are just hacked layers) what you needed was a script to get data from the active layers — to be able to select them one by one and rename. Here's how you do it (in this example the function will rename all the selected artboards to "my name index"):

var newName = "my name";
var ids = getLayersIDs(); // getting ids of selected layers

//for each id in the list
for (var i = 0; i < ids.length; i++)
{
  // select the layer first (well, artboard in this case)
  selectById(ids[i]);

  //rename it to "my name 1", "my name 2", etc
  activeDocument.activeLayer.name = newName + " " + (i + 1);
}

// this will get IDs of selected layers/groups/artboards
function getLayersIDs()
{
  var lyrs = [];
  var lyr;
  var ref = new ActionReference();
  var desc;
  var tempIndex;
  var ref2;

  ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
  ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));

  var targetLayers = executeActionGet(ref).getList(stringIDToTypeID("targetLayers"));
  for (var i = 0; i < targetLayers.count; i++)
  {
    tempIndex = 0;
    ref2 = new ActionReference();
    try
    {
      activeDocument.backgroundLayer;
      ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex());
      try
      {
        desc = executeActionGet(ref2);
        tempIndex = desc.getInteger(stringIDToTypeID("itemIndex")) - 1;
      }
      catch (e)
      {
        tempIndex = 0;
      }
    }
    catch (o)
    {
      ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex() + 1);
      desc = executeActionGet(ref2);
      tempIndex = desc.getInteger(stringIDToTypeID("itemIndex"));
    }

    lyrs.push(desc.getInteger(stringIDToTypeID("layerID")));
  }

  return lyrs;
};

// this will select a layer by ID
function selectById(id)
{
  var desc1 = new ActionDescriptor();
  var ref1 = new ActionReference();
  ref1.putIdentifier(charIDToTypeID('Lyr '), id);
  desc1.putReference(charIDToTypeID('null'), ref1);
  executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
};
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.