Highlighted

random action selection script

Explorer ,
Feb 26, 2016

Copy link to clipboard

Copied

Hello,

I have made 10 actions with brush definitions. I need a script for random action execution(instead of executing them on the actions panel) if it is possible. Can someone show me how to do it or post a final example here?

I have very low knowledge for scripting, please be gentle with me.

Thank you for your kindness,

Nick

This version requires you change the variable to suit your LayerSet name.

#target photoshop;

if(documents.length){

    main();

    }else{

        alert("You need a document open to run this script!");

        }

function main(){

//////////////////////////////////////////////////////////////////////

// Change "Default Actions" to the name of your LayerSet

var ASname = "Default Actions";

/////////////////////////////////////////////////////////////////////

if(!checkActionExists(ASname.toString())) {

    alert("\"" +ASname+ "\"" + " does not exist!\nPlease check your spelling\nIt is case sensitive!")

    return;

    }

var ActionSets =getActionSets();

for(var a in ActionSets){

    if(ActionSets.toString() == ASname.toString()){

        var Actions = getActions(ActionSets.toString());

        var RA = Actions[Math.floor(Math.random() * Actions.length)];

        try{

        app.doAction(RA.toString(), ASname.toString());

        }catch(e){};

        break;

        }

}

};

function checkActionExists( setName, actionName ){

   var res = false;

   try{

      var ref = new ActionReference();

            if(actionName != undefined){

      ref.putName( charIDToTypeID( 'Actn' ), actionName );

      }

      ref.putName( charIDToTypeID( "ASet" ), setName );

      executeActionGet( ref );

      res = true;

   }catch(e){return false}

   return res;

};

function getActionSets(){

var aSets=[];

var z = 1;

while(true){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID('ASet'), z);

try{

var desc = executeActionGet(ref);

var actName = desc.getString(charIDToTypeID('Nm  '));

aSets.push(actName);

z++;

}catch(e){return aSets;}

    }

};

function getActions(aSet){

var names = [];

var z =1;

while(true){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID('Actn'), z);

ref.putName(charIDToTypeID('ASet'), aSet);

try{

var adesc = executeActionGet(ref);

var actName = adesc.getString(charIDToTypeID('Nm  '));

names.push(actName);

z++;

}catch(e){return names;}

    }

};

TOPICS
Actions and scripting

Views

2.5K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

random action selection script

Explorer ,
Feb 26, 2016

Copy link to clipboard

Copied

Hello,

I have made 10 actions with brush definitions. I need a script for random action execution(instead of executing them on the actions panel) if it is possible. Can someone show me how to do it or post a final example here?

I have very low knowledge for scripting, please be gentle with me.

Thank you for your kindness,

Nick

This version requires you change the variable to suit your LayerSet name.

#target photoshop;

if(documents.length){

    main();

    }else{

        alert("You need a document open to run this script!");

        }

function main(){

//////////////////////////////////////////////////////////////////////

// Change "Default Actions" to the name of your LayerSet

var ASname = "Default Actions";

/////////////////////////////////////////////////////////////////////

if(!checkActionExists(ASname.toString())) {

    alert("\"" +ASname+ "\"" + " does not exist!\nPlease check your spelling\nIt is case sensitive!")

    return;

    }

var ActionSets =getActionSets();

for(var a in ActionSets){

    if(ActionSets.toString() == ASname.toString()){

        var Actions = getActions(ActionSets.toString());

        var RA = Actions[Math.floor(Math.random() * Actions.length)];

        try{

        app.doAction(RA.toString(), ASname.toString());

        }catch(e){};

        break;

        }

}

};

function checkActionExists( setName, actionName ){

   var res = false;

   try{

      var ref = new ActionReference();

            if(actionName != undefined){

      ref.putName( charIDToTypeID( 'Actn' ), actionName );

      }

      ref.putName( charIDToTypeID( "ASet" ), setName );

      executeActionGet( ref );

      res = true;

   }catch(e){return false}

   return res;

};

function getActionSets(){

var aSets=[];

var z = 1;

while(true){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID('ASet'), z);

try{

var desc = executeActionGet(ref);

var actName = desc.getString(charIDToTypeID('Nm  '));

aSets.push(actName);

z++;

}catch(e){return aSets;}

    }

};

function getActions(aSet){

var names = [];

var z =1;

while(true){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID('Actn'), z);

ref.putName(charIDToTypeID('ASet'), aSet);

try{

var adesc = executeActionGet(ref);

var actName = adesc.getString(charIDToTypeID('Nm  '));

names.push(actName);

z++;

}catch(e){return names;}

    }

};

TOPICS
Actions and scripting

Views

2.5K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2016 0
Explorer ,
Feb 26, 2016

Copy link to clipboard

Copied

Please, if someone could answer my question.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 26, 2016 0
Guide ,
Feb 27, 2016

Copy link to clipboard

Copied

This version requires you change the variable to suit your LayerSet name.

#target photoshop;

if(documents.length){

    main();

    }else{

        alert("You need a document open to run this script!");

        }

function main(){

//////////////////////////////////////////////////////////////////////

// Change "Default Actions" to the name of your LayerSet

var ASname = "Default Actions";

/////////////////////////////////////////////////////////////////////

if(!checkActionExists(ASname.toString())) {

    alert("\"" +ASname+ "\"" + " does not exist!\nPlease check your spelling\nIt is case sensitive!")

    return;

    }

var ActionSets =getActionSets();

for(var a in ActionSets){

    if(ActionSets.toString() == ASname.toString()){

        var Actions = getActions(ActionSets.toString());

        var RA = Actions[Math.floor(Math.random() * Actions.length)];

        try{

        app.doAction(RA.toString(), ASname.toString());

        }catch(e){};

        break;

        }

}

};

function checkActionExists( setName, actionName ){

   var res = false;

   try{

      var ref = new ActionReference();

            if(actionName != undefined){

      ref.putName( charIDToTypeID( 'Actn' ), actionName );

      }

      ref.putName( charIDToTypeID( "ASet" ), setName );

      executeActionGet( ref );

      res = true;

   }catch(e){return false}

   return res;

};

function getActionSets(){

var aSets=[];

var z = 1;

while(true){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID('ASet'), z);

try{

var desc = executeActionGet(ref);

var actName = desc.getString(charIDToTypeID('Nm  '));

aSets.push(actName);

z++;

}catch(e){return aSets;}

    }

};

function getActions(aSet){

var names = [];

var z =1;

while(true){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID('Actn'), z);

ref.putName(charIDToTypeID('ASet'), aSet);

try{

var adesc = executeActionGet(ref);

var actName = adesc.getString(charIDToTypeID('Nm  '));

names.push(actName);

z++;

}catch(e){return names;}

    }

};

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2016 2
Advisor ,
Feb 27, 2016

Copy link to clipboard

Copied

cTID = function(s) { return app.charIDToTypeID(s); };

function getActions(aset) {
  var i = 1;
  var names = [];

  if (!aset) {
    throw Error.runtimeError(9001, "Action set must be specified");
  }

  while (true) {
    var ref = new ActionReference();
    ref.putIndex(cTID("ASet"), i);
    var desc;
    try {
      desc = executeActionGet(ref);
    } catch (e) {
      names = undefined;
      break;    // all done
    }
    if (desc.hasKey(cTID("Nm  "))) {
      var aname = desc.getString(cTID("Nm  "));
      if (aname == aset) {
        var count = desc.getInteger(cTID("NmbC"));
        for (var j = 1; j <= count; j++) {
          var ref = new ActionReference();
          ref.putIndex(cTID('Actn'), j);
          ref.putIndex(cTID('ASet'), i);
          var adesc = executeActionGet(ref);
          var actName = adesc.getString(cTID('Nm  '));
          names.push(actName);
        }
        break;
      }
    }
    i++;
  }

  return names;
};

function randomElement(ary) {

  return ary[Math.floor(Math.random(ary.length) * ary.length)];

};

function getRandomAction(aset) {

   return randomElement(getActions(aset));

};

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2016 2
Advocate ,
Feb 26, 2016

Copy link to clipboard

Copied

var actionSet = "myActionSet"; // name of your ActionSet

var actions = ["Action 01","Action 02","Action 03","Action 04","Action 05","Action 06","Action 07","Action 08"]; // names of your actions

var randomAction = actions[Math.floor(Math.random() * actions.length)];

app.doAction(actions[randomAction], actionSet );

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 26, 2016 1
New Here ,
Sep 06, 2018

Copy link to clipboard

Copied

Hey Tom,

I use your script to load every few action i get an error : see image
please advice

Clip1.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 06, 2018 0
New Here ,
Oct 30, 2020

Copy link to clipboard

Copied

Did you Get Solution for this??

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 30, 2020 0
LEGEND ,
Feb 27, 2016

Copy link to clipboard

Copied

In case the number/names of the Actions changes in this thread

Re: Photoshop list actions with script

xbytor provided code to get a list of ActionsSets’ and the contains Actions’ names.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2016 0
Explorer ,
Feb 27, 2016

Copy link to clipboard

Copied

Wow! So many answers by great people here! I will study the complete code of SuperMerlin how it is made.

Thanks a bunch, especially to SuperMerlin!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2016 0
Explorer ,
Feb 28, 2016

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 28, 2016 0
Guide ,
Feb 29, 2016

Copy link to clipboard

Copied

Here is the basics of running a random script:

var scriptsFolder = Folder("your path to folder");

var Filelist = scriptsFolder.getFiles("*.jsx");

var RS = Actions[Math.floor(Math.random() * Filelist.length)];

$.evalfile(Filelist[RS]);

There is another method of running an Action that is NOT loaded in Photoshop, details can be found:-

JsDoc Reference - jamActions

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 29, 2016 0