2 Replies Latest reply on Nov 1, 2014 9:00 PM by bstrx

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

    bstrx

      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)

        • 1. Re: Is it possible to check if action from some action set exists?
          xbytor2 Level 4

          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[i];
              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;
          };
          
          • 2. Re: Is it possible to check if action from some action set exists?
            bstrx Level 1

            Thank you for such detailed answer!