Action Management

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

xbytor

Action Management

Post by xbytor »

Here's some code I've written for running, loading and deleting Actions. I've only tested it in CS2 so far, but I don't see anything in here to prevent it from running in CS.

ciao,
-X
Code: Select allcTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

Stdlib = function Stdlib() {};

//
// runAction
// attempt to execute an action. return true if OK, false if not available
// re-throws unknown exceptions.
//
Stdlib.runAction = function(atn, atnSet) {
  try {
    app.doAction(atn, atnSet);
  } catch (e) {
    if (e.toString().match(/action.+is not currently available/)) {
      return false;
    } else {
      throw e;
    }
  }
  return true;
};
runAction = Stdlib.runAction;

Stdlib.deleteActionStep = function(index, atn, atnSet) {
  function _ftn() {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putIndex(cTID("Cmnd"), index);
    ref.putName(cTID("Actn"), atn);
    ref.putName(cTID("ASet"), atnSet);
    desc.putReference(cTID("null"), ref);
    executeAction(cTID("Dlt "), desc, DialogModes.NO);
  }

  _ftn();
};
Stdlib.deleteAction = function(atn, atnSet) {
  function _ftn() {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName(cTID("Actn"), atn);
    ref.putName(cTID("ASet"), atnSet);
    desc.putReference(cTID("null"), ref);
    executeAction(cTID("Dlt "), desc, DialogModes.NO);
  }

  _ftn();
};
Stdlib.deleteActionSet = function(atnSet) {
  function _ftn() {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName(cTID("ASet"), atnSet);
    desc.putReference(cTID("null"), ref);
    executeAction(cTID("Dlt "), desc, DialogModes.NO);
  }

  _ftn();
};

//
// loadActionFile
//   load an Action file (.atn) into the runtime Action Palette
//   Note that if you load the same file multiple times, you will have multiple Action Sets
//  in the Action Palette with the same name.
//  The loaded Actions will not be available for use until after the script terminates
//   f = File.openDialog(); if (f) Stdlib.loadActionFile(f);
//
Stdlib.loadActionFile = function(file) {
  var str = '';
  str += '\"' + app.path.fsName + "\\photoshop.exe\"";
  str += ' \"' + file.fsName + '\"';
  var cmdFile = new File(Folder.temp + "/loadAction.bat");
  cmdFile.remove();  // delete any old copy laying around
  Stdlib.writeToFile(cmdFile, str);
  cmdFile.execute();
};
xbytor

Action Management

Post by xbytor »

Here's an updated version.

Changes:
Added a loadActionFiles interface so that you can do the very useful
Code: Select allStdlib.loadActions(folder.getFiles("*.atn");Added 'deleteAllActionSets' function to clean out all of those annoying Actions out of the Actions Palette. Handle with care. Added 'getActionSets' the returns an array with the names of all of the action sets currently loaded.Added missing writeToFile function.Fixed a bug or two.Here ya go...
Code: Select all//
// Action Management functions
//
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

Stdlib = function Stdlib() {};

// attempt to execute an action. return true if OK, false if not available
// re-throws unknown exceptions.
Stdlib.runAction = function(atn, atnSet) {
  try {
    app.doAction(atn, atnSet);
  } catch (e) {
    if (e.toString().match(/action.+is not currently available/)) {
      return false;
    } else {
      throw e;
    }
  }
  return true;
};

Stdlib.deleteActionStep = function(index, atn, atnSet) {
  function _ftn() {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putIndex(cTID("Cmnd"), index);
    ref.putName(cTID("Actn"), atn);
    ref.putName(cTID("ASet"), atnSet);
    desc.putReference(cTID("null"), ref);
    executeAction(cTID("Dlt "), desc, DialogModes.NO);
  }

  _ftn();
};
Stdlib.deleteAction = function(atn, atnSet) {
  function _ftn() {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName(cTID("Actn"), atn);
    ref.putName(cTID("ASet"), atnSet);
    desc.putReference(cTID("null"), ref);
    executeAction(cTID("Dlt "), desc, DialogModes.NO);
  }

  _ftn();
};
Stdlib.deleteActionSet = function(atnSet) {
  function _ftn() {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName(cTID("ASet"), atnSet);
    desc.putReference(cTID("null"), ref);
    executeAction(cTID("Dlt "), desc, DialogModes.NO);
  }

  try {
    _ftn();
  } catch (e) {
    // if this action is the currently executing action,
    // we can't delete it, so we return false. All other
    // exceptions are rethrown
    if (!e.toString().match(/action that is playing or recording/)) {
      throw e;
    }
    return false;
  }
  return true;
};

//
//  f = File.openDialog(); Stdlib.loadActionFile(f);
//
Stdlib.loadActionFile = function(file) {
  Stdlib.loadActionFiles([file]);
};
//
// Stdlib.loadActionFiles(folder.getFiles("*.atn"))'
//
Stdlib.loadActionFiles = function(files) {
  var str = '';
  var ts = new Date().getTime();

  var cmdFile = new File(Folder.temp + "/loadAction-" + ts + ".bat");

  var psPath = '\"' + app.path.fsName + "\\photoshop.exe\"";

  for (var i = 0; i < files.length; i++) {
    str += psPath;
    str += ' \"' + files.fsName + '\"\r\n';
  }
   
  str += "del" + ' \"' + cmdFile.fsName + '\"\r\n';

  Stdlib.writeToFile(cmdFile, str);

  cmdFile.execute();
};

Stdlib.getActionSets = function() {
  var i = 1;
  var names = [];
 
  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  "))) {
      names.push(desc.getString(cTID("Nm  ")));
    }
    i++;
  }

  return names;
};

//
// Very dangerous unless you _want_ to empty your Actions Palette.
//
Stdlib.deleteAllActionSets = function() {
  var setNames = Stdlib.getActionSets();

  for (var i = setNames.length-1; i >= 0; i--) {
    Stdlib.deleteActionSet(setNames);
  }
};

Stdlib.writeToFile = function(file, str) {
  file.open("w") || throwError("Unable to open output file \"" +
                               file + "\".\r" + file.error);
  file.write(str);
  file.close();
};
Guest

Action Management

Post by Guest »

Tryed it but unforunately it did not work in CS, but it might be because I run it like it is written included the DELETE ALL .atn in PS.
Do I have to modify some code?
This script can come in very handy when invoked with an action. Some folks have trouble to load actions using the little arrow and Replace Actions.[/b]
xbytor

Action Management

Post by xbytor »

Can you be more specific on what happens when it doesn't run? I don't have access to CS at the moment so I can't test it myself.
Also, what function are you calling and how?
Mrali

Action Management

Post by Mrali »

Hello X,
I have tired your code with CS but it did not work.
I don't need an interface, so I tried the first one.

I have saved the code in a file and calling it from within VB form.
The error in Visual Basic is # 91 (Object variable or with block variable not set).

I am still not able to clear out the Action List since long time I had tried it. I even couldn't do it in VB

I don't mind calling JavsScript to do just what I want. I hope you can help me again with this.

Thanks
jeff_hanna

Action Management

Post by jeff_hanna »

Is there any way with scripting (CS4) to create a new action and start/stop recording it?
xbytor

Action Management

Post by xbytor »

The JS DOM in PS does not have anything for controlling the recording of Actions. The best you can do is have a some scripts that
1) delete the contents of the Scripting Listener Log via a script or user interaction.
2) converts the SL log file to an action/action-file (see xtools/xapps/ActionFileFromSLCode) when the user is finished 'recording' the script.

There are somethings that won't get work (like printing) and you get some extra stuff (like tool selection) but it's a fairly close replacement.