Skip to main content
Participating Frequently
June 11, 2018
Question

Random Layers scale and opacity

  • June 11, 2018
  • 2 replies
  • 3330 views

Hi All, i found this script which is great in illustrator, however i want to do the same but in Photoshop. Would it be possible? Thank you.

aDoc = app.activeDocument;

aSel = aDoc.selection;



opMin = Number(prompt("min (0-1000%)", 0));

opMax = Number(prompt("max (0-1000%)", 200));


if(opMin > opMax)

{

    temp = opMin;

    opMin = opMax;

    opMax = temp;

}

if(opMin<0)

    opMIn = 0;

if(opMax>1000)

    opMax = 1000;


for(i=0; i<nSel; i++)

{

    resScale = Math.floor(Math.random() * (opMax - opMin + 1)) + opMin;

    aSel[i].resize(resScale, resScale);

}

This topic has been closed for replies.

2 replies

Inspiring
June 12, 2018

function Random(max){

       return Math.round(Math.random()*(max-1))+1

    }

for (i=0;i<activeDocument.layers.length;i++){

        try{

        activeDocument.layers.opacity = Random(100)

        activeDocument.layers.resize(Random(100), Random(100))

        }catch(er){}

    }

sidpalas
Inspiring
June 11, 2018

Is the goal to apply a (different) random opacity and rescale to each layer?

I cleaned up the formatting of the code you pasted to make it easier to see what was going on:

#target photoshop

var aDoc = app.activeDocument;

var aSel = aDoc.selection;

var opMin = Number(prompt("min (0-1000%)", 0));

var opMax = Number(prompt("max (0-1000%)", 200));

if(opMin > opMax){

    temp = opMin;

    opMin = opMax;

    opMax = temp;

    }

if(opMin<0){

    opMIn = 0;

    }

if(opMax>1000){

    opMax = 1000;

    }

for(i=0; i<nSel; i++){

    var resScale = Math.floor(Math.random() * (opMax - opMin + 1)) + opMin;

    aSel.resize(resScale, resScale);

}

If my interpretation about what you are trying to do is correct, in line 4 you will want to use the aDoc.artLayers or aDoc.layerSets collections to grab the layers to loop through.

Another issue is in line 23 in which nSel is used (without having been declared). Presumably you would want to set this to the total number of layers you want to loop through.

The other confusing portion is line 24 in which opMin and opMax (the min and max for the desired opacity ranges?) are used to calculate the rescale factor. Should the rescaling depend on the opacity?

Hopefully this can get you started down the path to what you want to accomplish.

Participating Frequently
June 12, 2018

Hi Sidpalas thank you for quick response, i tried your code but it didn't work. I don't have any background in scripting but i tried to play with the code and i still cant get it to work.

Ps. i dont know why i dont have much options in pasting the code its always colored.

  • #target photoshop
  • var aDoc = app.activeDocument
  • var aSel = aDoc.layerSets
  • var opMin = Number(prompt("min (0-100%)", 50)); 
  • var opMax = Number(prompt("max (0-100%)", 100)); 
  • if(opMin > opMax){ 
  • temp = opMin
  • opMin = opMax
  • opMax = temp
  •   } 
  • if(opMin<0){ 
  • opMIn = 0
  •   } 
  • if(opMax>1000){ 
  • opMax = 1000
  •   } 
  • for(i=0; i<aSel; i++){ 
  • var resScale = Math.floor(Math.random() * (opMax - opMin + 1)) + opMin
  • aSel[i].resize(resScale, resScale); 

sidpalas
Inspiring
June 12, 2018

I didn't change the content of the code you pasted (only changed the formatting to make it easier to read ) so it makes sense that it still wouldn't work.

If you were to more clearly state what it is you are trying to accomplish, someone here might be able to help, I just wasn't sure what exactly the goal was.

When it comes to pasting code into the forum, if you paste it in as plain text and then click the "Use advanced editor" link, followed by ">>", and choose Syntax Highlighting for Javascript, it will look as mine does.