Skip to main content
Participant
November 1, 2014
Answered

Is it possible to check if action from some action set exists?

  • November 1, 2014
  • 1 reply
  • 982 views

In photoshop script (CC 2014) I'm executing doAction with action and action set that can possibly be not added yet. I wonder if i can somehow check if action exist first.

all code i have:

document = app.open(new File(inputFile));

// some check if there is such action ?

app.doAction(actionToRun, actionSetToRun)

This topic has been closed for replies.
Correct answer xbytor2

The simplest way is to run the action. If it throws an exception, check to see if it's the exception for a missing action.

document = app.open(new File(inputFile));

try {

    app.doAction(actionToRun, actionSetToRun);

} catch (e) {

   // Check e to see if it's the missing action exception.

}

Or you can use this function extracted from xtools/xlib/stdlib.js.

function hasAction(atn, atnSet) {

  function cTID(s) { return app.charIDToTypeID(s); };
  var asetDesc;
  var rc = false;
  var i = 1;

  var asMatches = [];

  while (true) {
    var ref = new ActionReference();
    ref.putIndex(cTID("ASet"), i);
    var desc;
    try {
      desc = executeActionGet(ref);
    } catch (e) {
      break;    // all done
    }
    if (desc.hasKey(cTID("Nm  ")) &&
        desc.getString(cTID("Nm  ")) == atnSet) {
      asetDesc = desc;
      asMatches.push({ index: i, desc: desc});
      //break;
    }
    i++;
  }

  if (asMatches.length == 0) {
    return false;
  }

  for (var i = 0; i < asMatches.length; i++) {
    var asmatch = asMatches;
    var asetIndex = asmatch.index;
    asetDesc = asmatch.desc;

    if (!asetDesc.hasKey(cTID("NmbC"))) {
      continue;
    }
    var max = asetDesc.getInteger(cTID("NmbC"));
    for (var j = 1; j <= max; j++) {
      var ref = new ActionReference();
      ref.putIndex(cTID("Actn"), j);           // Action index
      ref.putIndex(cTID("ASet"), asetIndex);   // ActionSet index

      var desc;
      try {
        desc = executeActionGet(ref);
      } catch (e) {
        break;   // all done
      }
      if (desc.hasKey(cTID("Nm  ")) &&
          desc.getString(cTID("Nm  ")) == atn) {
        return true;
      }
    }
  }
  return rc;
};

1 reply

xbytor2Correct answer
Inspiring
November 1, 2014

The simplest way is to run the action. If it throws an exception, check to see if it's the exception for a missing action.

document = app.open(new File(inputFile));

try {

    app.doAction(actionToRun, actionSetToRun);

} catch (e) {

   // Check e to see if it's the missing action exception.

}

Or you can use this function extracted from xtools/xlib/stdlib.js.

function hasAction(atn, atnSet) {

  function cTID(s) { return app.charIDToTypeID(s); };
  var asetDesc;
  var rc = false;
  var i = 1;

  var asMatches = [];

  while (true) {
    var ref = new ActionReference();
    ref.putIndex(cTID("ASet"), i);
    var desc;
    try {
      desc = executeActionGet(ref);
    } catch (e) {
      break;    // all done
    }
    if (desc.hasKey(cTID("Nm  ")) &&
        desc.getString(cTID("Nm  ")) == atnSet) {
      asetDesc = desc;
      asMatches.push({ index: i, desc: desc});
      //break;
    }
    i++;
  }

  if (asMatches.length == 0) {
    return false;
  }

  for (var i = 0; i < asMatches.length; i++) {
    var asmatch = asMatches;
    var asetIndex = asmatch.index;
    asetDesc = asmatch.desc;

    if (!asetDesc.hasKey(cTID("NmbC"))) {
      continue;
    }
    var max = asetDesc.getInteger(cTID("NmbC"));
    for (var j = 1; j <= max; j++) {
      var ref = new ActionReference();
      ref.putIndex(cTID("Actn"), j);           // Action index
      ref.putIndex(cTID("ASet"), asetIndex);   // ActionSet index

      var desc;
      try {
        desc = executeActionGet(ref);
      } catch (e) {
        break;   // all done
      }
      if (desc.hasKey(cTID("Nm  ")) &&
          desc.getString(cTID("Nm  ")) == atn) {
        return true;
      }
    }
  }
  return rc;
};

bstrxAuthor
Participant
November 2, 2014

Thank you for such detailed answer!