SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
     function step1(enabled, withDialog) {
        if (enabled != undefined && !enabled)
          return;
        var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
        var desc1 = new ActionDescriptor();
        desc1.putInteger(cTID('0001'), 215);
        desc1.putInteger(cTID('0002'), 172);
    etc etc (goes through many other variables)
        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

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    function step1(enabled, withDialog, width, height) {
      if (enabled != undefined && !enabled)
        return;
      
      var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
      var desc1 = new ActionDescriptor();
      desc1.putInteger(cTID('0001'), width);
      desc1.putInteger(cTID('0002'), height);
      
      //etc etc (goes through many other variables)
      executeAction(sTID('Flaming Pear'), desc1,
    Is this what you're talking about?
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Ah, perhaps you want to perform those action in what's called a for loop

    So we could do something like this in your function:

    Code javascript:
    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:

    Code javascript:
    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 )
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    In this case it would be:

    Code javascript:
    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:

    Code javascript:
    var counter = 100;
    while (counter > 0) {
        counter -= 1;
        width += 1;
        height -= 1;
        // here is where you do stuff with the adjusted width and height
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by hemipemi View Post
    ++ means +1 I take it? What if it needed to be +2 or even +0.1? or *2 perhaps.
    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.

    Quote Originally Posted by hemipemi View Post
    Would there need to be something extra to force the script to round it off to an integer?
    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(), Math.round() and Math.ceil() so that you can perform rounding according to whatever fits your requirements.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •