Looking for help with Photoshop javascript (changing variables)

Hi,

I’m very new to using javascript to program Photoshop, and was looking for some help with changing the variables in an action.

The basic end result I’m looking for (in laymens terms) is:

Set variables
Perform action
Change variables (iteratively, ie a=a+1)
Repeat until condition met (ie, after 100 iterations, the script would end).

I used a script that converts Photoshop actions to Javascript, so I have this as a base:

 function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    desc1.putInteger(cTID('0001'), [COLOR="Red"]215[/COLOR]);
    desc1.putInteger(cTID('0002'), [COLOR="Red"]172[/COLOR]);
[I]etc etc (goes through many other variables)[/I]
    executeAction(sTID('Flaming Pear'), desc1, 

I’ve put the properties that would need to become variables in red. I have edited out a bunch of lines in this post as these seem to be the significant ones in terms of what I need to do (obviously keeping them in the script itself!).

Other than that I think that’s all I need to know for now - just that little thing would enhance my workflow no end.

Also, I’m a graphic designer so if anyone would be interested in doing some skillshare or work swap - I’d be happy to provide help and service with graphics in exchange for small tidbits about javascript.

Cheers,
Jonathan

If you wanted to pass the variables in to the function, that might be helpful for you I imagine.
(I’m assuming they’re either dimensions or color values, you could rename them appropriately)

Something along the lines of:


function step1(enabled, withDialog, [COLOR=#ff0000]width[/COLOR], [COLOR=#ff0000]height[/COLOR]) {
  if (enabled != undefined && !enabled)
    return;
  
  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
  var desc1 = new ActionDescriptor();
  desc1.putInteger(cTID('0001'), [COLOR=#ff0000]width[/COLOR]);
  desc1.putInteger(cTID('0002'), [COLOR=#ff0000]height[/COLOR]);
  
  //etc etc (goes through many other variables)
  executeAction(sTID('Flaming Pear'), desc1,

Is this what you’re talking about?

Something like this yes - I’m just not really familiar with how to get those values to change (like to increase by 1 or 2 each step), and then end when a condition is reached.

In BASIC it would be like:
width = width + 1
height = height -1
counter = counter + 1
if counter = 100 then end else goto start

Ah, perhaps you want to perform those action in what’s called a for loop :slight_smile:

So we could do something like this in your function:


var max = 100; //however many times you want to loop.
for (var i = 0; i < max; i++) { //i++ increments i by 1 each iteration
  //do something
}

So you might end up with something like this:


function step1(enabled, withDialog, startWidth, startHeight, iterationCount) {
  if (enabled != undefined && !enabled)
    return;
  
  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
  var desc1 = new ActionDescriptor();

  for (var i = 0; i < iterationCount; i++) {
    desc1.putInteger(cTID('0001'), startingWidth + i );
    desc1.putInteger(cTID('0002'), startingHeight + i );
    //etc etc (goes through many other variables)
  }
  
  executeAction(sTID('Flaming Pear'), desc1,
  // ...

You can then call your step1 function with something like:
step1(true, true, 215, 172, 100);

(if you wanted to increase the width and height by 1 for each iteration - the first iteration will have i at it’s original value that you specified at the start of the for loop. So in the first iteration you would be adding 0 (and thus using the original values that you passed in).

What is it exactly you’re doing (I’m curious ;))

In this case it would be:


var i;
for (i = 0; i < 100; i += 1) {
    width += 1;
    height -= 1;
    // here is where you do stuff with the adjusted width and height
}

Or it could be:


var counter = 100;
while (counter > 0) {
    counter -= 1;
    width += 1;
    height -= 1;
    // here is where you do stuff with the adjusted width and height
}

Basically I’m making animations - they’re geometric/abstract style so it’s important that everything is mathematically perfect, so it makes more sense to be scripting it. Also I use Photoshop all the time, so it keeps me in my comfort zone.

Once I get the hang of the basic method I might explore some more complex maths.

I will try what you’ve mentioned so far and see if it works. ++ means +1 I take it? What if it needed to be +2 or even +0.1? or *2 perhaps. Would there need to be something extra to force the script to round it off to an integer?

Thanks again for the help,
Jonathan

It is for this precise reason why I use +=1 and -=1 instead of ++ or –
You can change the 1 to be any other value that you require.

Photoshop should do any rounding off that’s needed. If you do that yourself, you’re likely to introduce inconsistencies due to values always being rounded.

Having said that though, JavaScript does provide Math.floor(), [url=“https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/round”]Math.round() and [url=“https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/ceil”]Math.ceil() so that you can perform rounding according to whatever fits your requirements.