ActionManager: do * ByName()

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

undavide

ActionManager: do * ByName()

Post by undavide »

Hello,
I've set up a function to change an ArtLayer color (as it is in the Layers palette) by color and layer name:

Code: Select allvar c2t = function(c) {
  return app.charIDToTypeID(c);
};

var setLayerColorByName = function(color, layerName) {
  var d1, d2, layerColors, r1;
  layerColors = {
    none: "None",
    red: "rd  ",
    orange: "Orng",
    yellow: "Ylw ",
    green: "Grn ",
    blue: "Bl  ",
    violet: "Vlt ",
    gray: "Gry "
  };
  d1 = new ActionDescriptor();
  d2 = new ActionDescriptor();
  r1 = new ActionReference();
  if (layerName == null) {
    r1.putEnumerated(c2t("Lyr "), c2t("Ordn"), c2t("Trgt"));
  } else {
    r1.putName(c2t("Lyr "), layerName);
  }
  d1.putReference(c2t("null"), r1);
  d2.putEnumerated(c2t("Clr "), c2t("Clr "), c2t(layerColors[color]));
  d1.putObject(c2t("T   "), c2t("Lyr "), d2);
  return executeAction(c2t("setd"), d1, DialogModes.NO);
};

That I'd use like:

Code: Select allsetLayerColorByName("green", "first"); // there is a layer called "first" somewhere

As it is, it always work on the current layer/layerset.
Is there something that I don't get in the ActionReference.putName() as I've used it in the else statement?
Thank you,

Davide Barranca
http://www.davidebarranca.com
Mike Hale

ActionManager: do * ByName()

Post by Mike Hale »

I can't test now but there are some actions that require the layer to be active when using Action Manager. For those actions referencing by name, index, or ID doesn't work. So that may be what is happening here.
undavide

ActionManager: do * ByName()

Post by undavide »

Thank you Mike,
it might be the case (even if I assumed that, as you can change layer's opacity, name, etc the same would apply to color, but it must rely on AM code so perhaps it can't be done directly) - I've found this one in StdLib that might do the trick:

Code: Select all// via SzopeN
// These two vars are used by wrapLC/Layer and control whether or not
// the existing doc/layer should be restored after the call is complete
// If these are set fo false, the specified doc/layer will remain
// the active doc/layer
//
Stdlib._restoreDoc = true;
Stdlib._restoreLayer = true;

//
// ScriptingListener code operates on the "active" document.
// There are times, however, when that is _not_ what I want.
// This wrapper will make the specified document the active
// document for the duration of the ScriptingListener code and
// swaps in the previous active document as needed
//
Stdlib.wrapLC = function(doc, ftn) {
  var ad = app.activeDocument;
  if (doc) {
    if (ad != doc) {
      app.activeDocument = doc;
    }
  } else {
    doc = ad;
  }

  var res = undefined;
  try {
    res = ftn(doc);

  } finally {
    if (Stdlib._restoreDoc) {
      if (ad && app.activeDocument != ad) {
        app.activeDocument = ad;
      }
    }
  }

  return res;
};

//
// The same as wrapLC except it permits specifying a layer
//
Stdlib.wrapLCLayer = function(doc, layer, ftn) {
  var ad = app.activeDocument;
  if (doc) {
    if (ad != doc) {
      app.activeDocument = doc;
    }
  } else {
    doc = ad;
  }

  var al = doc.activeLayer;
  var alvis = al.visible;

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

  } else {
    layer = doc.activeLayer;
  }

  var res = undefined;

  try {
    res = ftn(doc, layer);

  } finally {
    if (Stdlib._restoreLayer) {
      if (doc.activeLayer != al) {
        doc.activeLayer = al;
      }
      if (!doc.activeLayer.isBackgroundLayer) {
        doc.activeLayer.visible = alvis;
      }
    }

    if (Stdlib._restoreDoc) {
      if (app.activeDocument != ad) {
        app.activeDocument = ad;
      }
    }
  }

  return res;
};

Davide