Skip to main content
Known Participant
November 3, 2016
Answered

Randomizing parameter values in an action

  • November 3, 2016
  • 2 replies
  • 2171 views

Hi to all,

I have an action that executes a filter from the filter gallery in Photoshop with certain parameter values. What I need is to randomize those values on each execution of the action in a range between certain min and max values. Here is an example of the action in JavaScript made from xtools:

function Action1() {

  // Filter Gallery

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putEnumerated(cTID('GEfk'), cTID('GEft'), cTID('Txtz'));

    desc1.putEnumerated(cTID('TxtT'), cTID('TxtT'), cTID('TxCa'));

    desc1.putInteger(cTID('Scln'), 97);

    desc1.putInteger(cTID('Rlf '), 17);

    desc1.putEnumerated(cTID('LghD'), cTID('LghD'), cTID('LDTL'));

    desc1.putBoolean(cTID('InvT'), false);

    executeAction(1195730531, desc1, dialogMode);

  };

  step1();      // Filter Gallery

};

In

desc1.putInteger(cTID('Scln'), 97);

desc1.putInteger(cTID('Rlf '), 17);

are the parameter values I need to randomize on each execution of the script, I know there must be something with Math.random(), but please if someone could guide me or preferably provide me the exact code part that makes the randomization so I can edit it in the future. I am working on a project of my own and need fast execution of certain filters with applied ranged randomization of the values. If this is possible it would be of great value to me.

Thank you in advance for anyone's help that will come.

TB

This topic has been closed for replies.
Correct answer pixxxelschubser

What I meant was about placing the code syntactically right.

Is this correct if I replace 97 with Math.random() in its simplest form:

desc1.putInteger(cTID('Scln'), Math.random()); ?

Please correct me if I am wrong.

Thank you

P.S. As I stated, I am not an expert in this field, actually I know very little about coding.


teslaball​,

You are not totally wrong.

But you have to define the range of the random value e.g. like this:

function Action1() {

    // Filter Gallery

    function step1(enabled, withDialog) {

        if (enabled != undefined && !enabled) {

            return;

        }

        var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

        var desc1 = new ActionDescriptor();

        desc1.putEnumerated(cTID('GEfk'), cTID('GEft'), cTID('Txtz'));

        desc1.putEnumerated(cTID('TxtT'), cTID('TxtT'), cTID('TxCa'));

        desc1.putInteger(cTID('Scln'), Math.floor((Math.random() * 150) + 51));

        desc1.putInteger(cTID('Rlf '), Math.floor((Math.random() * 50) + 1));

        desc1.putEnumerated(cTID('LghD'), cTID('LghD'), cTID('LDTL'));

        desc1.putBoolean(cTID('InvT'), false);

        executeAction(1195730531, desc1, dialogMode);

    };

    step1(true, true);      // Filter Gallery

};

Have fun

2 replies

Karry J.
Participant
February 20, 2018

Long time passed till I realized that I need randomizing the cTID. For example here is a part of a code that sets 'Technique' to Smooth under 'Structure' and all under 'Bevel & Emboss' in Layer Style creation:

desc6.putEnumerated(cTID('bvlT'), cTID('bvlT'), cTID('SfBL'));

I realized cTID('SfBL') is the Smooth option selected, but ho to random select between Chisel Hard, Smooth and Chisel Soft?

This does not work:

desc6.putEnumerated(cTID('bvlT'), cTID('bvlT'), Math.floor((Math.random() * cTID('SfBL')) + 3));?

Thank you in advance

JJMack
Community Expert
Community Expert
November 3, 2016

You would need to change those hard coded setting JavaScript random() Method

JJMack
teslaballAuthor
Known Participant
November 3, 2016

Hello JJMack,

how to implement in desc1.putInteger(cTID('Scln'), 97);, so the number is to be randomized?

Please, can you point me exact code how it is to be done, I am not an expert in this field.

Thank you.

JJMack
Community Expert
Community Expert
November 3, 2016

You posted a Photoshop script you created using xtools to convert an action to javascript.  You want to change that javascript so the Photoshop javascript will use random values rather than the current hard coded values coded in the Photoshop Script.  You showed the two statement you want to change in the script to use random values instead of the card coded values.  You need to replace the 97 and 17 in the two statement with random numbers within some range of values.   You must decide what those ranges are  and replace the 97 and 17 with the random() functions you want to use.  I have do idea of the exact code you would want to use. You know the ranges you want and you would need to code the the javascript code to produce numbers within the ranges you want.

JavaScript random() Method

JJMack