Skip to main content
Participating Frequently
June 11, 2018
Pregunta

Random Layers scale and opacity

  • June 11, 2018
  • 2 respuestas
  • 3329 visualizaciones

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);

}

Este tema ha sido cerrado para respuestas.

2 respuestas

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); 

Inspiring
June 12, 2018

sorryyyy last one, im trying my luck but not successful..i want to add also rotation..

  activeDocument.layers[i].rotate = Random(30,60

i tried that but didnt work


function Random(min, max) {

    return Math.random() * (max - min) + min;

}

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

        try{

            if(activeDocument.layers.visible){

            activeDocument.layers.opacity = Random(20,100)

            var Scale = Random(20,100)

            activeDocument.layers.resize(Scale, Scale)

            activeDocument.layers.rotate(Random(0,360))

            }

            activeDocument.layers.visible = true

        }catch(er){}

    }

https://wwwimages2.adobe.com/content/dam/acom/en/devnet/photoshop/scripting/Photoshop-CS6-JavaScript-Ref.pdf