Hi Everybody,
I am very new at scripting for photoshop. I hope that somebody can help me with this.
I want to create a action that looks like this:
1. Create document
2. Select random 3 images from a folder and place these images in 3 separate layers on the canvas. Each layer for each image.
3. Select all layers
4. Randomize horizontal and vertical flip
5. Randomize the rotation from 0 to 360 degrees
6. Turn the modes for all 3 layers to darken
To complete this I need 3 scripts. 1. for randomize select and place 3 images from a folder. 2. for randomize horizontal and vertical flip. and 3. for randomize the rotation.
I understand something about javascripting but I have a problem with the randomize function. Can somebody help me where to begin?
Randomizing scripts
-
pfaffenbichler
Randomizing scripts
Maybe this helps.
It only flips horizontally but you should be able to figure it out.
At the end you could set the Blend Mode of the three SOs individually (they are listed in »theLayers«), but you could also include that in the placing function right away.
Code: Select all// select folder, create document, place three of the images in the folder randomly;
// 2014, use it at your own risk;
#target photoshop
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
if (theFiles.length > 2) {
// shuffle files;
var theFiles = arrayShuffle(theFiles);
// set to pixels;
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theImage = app.documents.add(2000, 2000, 72, "image", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
var theLayers = new Array;
// place images;
for (var m = 0; m <= 2; m++) {
var theLayer = placeScaleFile (theFiles[m], 1000 - (Math.random() * 2000), 1000 - (Math.random() * 2000), 100* (1-(Math.floor(Math.random()*2))*2), 100, Math.random()*360);
theLayers.push (theLayer);
};
// reset;
app.preferences.rulerUnits = originalUnits;
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(jpg|tif|psd|pdf|)$/i)) {
return true
};
};
////// place //////
function placeScaleFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
// =======================================================
var idPlc = charIDToTypeID( "Plc " );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc5.putPath( idnull, new File( file ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc5.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idHrzn, idPxl, xOffset );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idVrtc, idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idWdth, idPrc, theYScale );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idHght, idPrc, theXScale );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc5.putUnitDouble( idAngl, idAng,theAngle );
var idLnkd = charIDToTypeID( "Lnkd" );
desc5.putBoolean( idLnkd, true );
executeAction( idPlc, desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
// http://www.hardcode.nl/subcategory_1/ar ... e-function
function arrayShuffle(theArray) {
var len = theArray.length;
var i = len;
while (i--) {
var p = parseInt(Math.random()*len);
var t = theArray;
theArray = theArray[p];
theArray[p] = t;
};
return theArray
};
It only flips horizontally but you should be able to figure it out.
At the end you could set the Blend Mode of the three SOs individually (they are listed in »theLayers«), but you could also include that in the placing function right away.
Code: Select all// select folder, create document, place three of the images in the folder randomly;
// 2014, use it at your own risk;
#target photoshop
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
if (theFiles.length > 2) {
// shuffle files;
var theFiles = arrayShuffle(theFiles);
// set to pixels;
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theImage = app.documents.add(2000, 2000, 72, "image", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
var theLayers = new Array;
// place images;
for (var m = 0; m <= 2; m++) {
var theLayer = placeScaleFile (theFiles[m], 1000 - (Math.random() * 2000), 1000 - (Math.random() * 2000), 100* (1-(Math.floor(Math.random()*2))*2), 100, Math.random()*360);
theLayers.push (theLayer);
};
// reset;
app.preferences.rulerUnits = originalUnits;
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(jpg|tif|psd|pdf|)$/i)) {
return true
};
};
////// place //////
function placeScaleFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
// =======================================================
var idPlc = charIDToTypeID( "Plc " );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc5.putPath( idnull, new File( file ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc5.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idHrzn, idPxl, xOffset );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idVrtc, idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idWdth, idPrc, theYScale );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idHght, idPrc, theXScale );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc5.putUnitDouble( idAngl, idAng,theAngle );
var idLnkd = charIDToTypeID( "Lnkd" );
desc5.putBoolean( idLnkd, true );
executeAction( idPlc, desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
// http://www.hardcode.nl/subcategory_1/ar ... e-function
function arrayShuffle(theArray) {
var len = theArray.length;
var i = len;
while (i--) {
var p = parseInt(Math.random()*len);
var t = theArray;
theArray = theArray[p];
theArray[p] = t;
};
return theArray
};
-
salmonickatelier
Randomizing scripts
Thank you so much for your fast reply!!! You are a life saver. It completely works.
I only have 2 questions:
1. I am searching for a way not to have a popup screen for selecting the folder. But just a path in the script. How can I do this?
2. The randomize is not random enough. For each session the rotation is different. That is cool. But each session the 3 images have the same rotation. I want that each image each session have a different rotation. How can I fix this?
It is already great, but with these two points makes this script so COOL!
I only have 2 questions:
1. I am searching for a way not to have a popup screen for selecting the folder. But just a path in the script. How can I do this?
2. The randomize is not random enough. For each session the rotation is different. That is cool. But each session the 3 images have the same rotation. I want that each image each session have a different rotation. How can I fix this?
It is already great, but with these two points makes this script so COOL!
-
pfaffenbichler
Randomizing scripts
1) Instead of
Code: Select allvar theFolder = Folder.selectDialog ("select folder");
use something like
Code: Select allvar theFolder = new Folder ("…");
and insert the correct path of the folder in the brackets and inverted commas.
2)
But each session the 3 images have the same rotation.
I cannot confirm this, the rotation is pseudo-random in each individual placing.
Could you post a couple of examples?
Code: Select allvar theFolder = Folder.selectDialog ("select folder");
use something like
Code: Select allvar theFolder = new Folder ("…");
and insert the correct path of the folder in the brackets and inverted commas.
2)
But each session the 3 images have the same rotation.
I cannot confirm this, the rotation is pseudo-random in each individual placing.
Could you post a couple of examples?
-
tyr
Randomizing scripts
salmonickatelier wrote:2. The randomize is not random enough. For each session the rotation is different. That is cool. But each session the 3 images have the same rotation. I want that each image each session have a different rotation. How can I fix this?
Just curious: are you on PC or Mac? I've been struggling with Math.random() on Mac because the values seemed like sinus with very slight variations: from 0 to 1 and then from 1 to 0.
Just curious: are you on PC or Mac? I've been struggling with Math.random() on Mac because the values seemed like sinus with very slight variations: from 0 to 1 and then from 1 to 0.
-
pfaffenbichler
Randomizing scripts
I’m working on a Mac.
This is a sequence on 20 random numbers
0.47964477539062
0.45396423339844
0.36909484863281
0.67100524902344
0.49856567382812
0.15451049804688
0.00003051757812
0.81681823730469
0.14234924316406
0.02035522460938
0.24200439453125
0.3863525390625
0.131591796875
0.47386169433594
0.96826171875
0.08393859863281
0.22401428222656
0.24136352539062
0.71525573730469
0.42996215820312
and I notice no terribly obvious pattern in it.
This is a sequence on 20 random numbers
0.47964477539062
0.45396423339844
0.36909484863281
0.67100524902344
0.49856567382812
0.15451049804688
0.00003051757812
0.81681823730469
0.14234924316406
0.02035522460938
0.24200439453125
0.3863525390625
0.131591796875
0.47386169433594
0.96826171875
0.08393859863281
0.22401428222656
0.24136352539062
0.71525573730469
0.42996215820312
and I notice no terribly obvious pattern in it.
-
salmonickatelier
Randomizing scripts
Hello everybody,
Thank you for the comments.
Yes I am on PC. Somewhere I heard the same thing but I can't comfirm it.
But at this moment the rotation is not the big problem. After a slitly random rotation I can do the other rotation by hand.
But the simpel thing doesn't work. I also thouth that the way to place a folder in the script is by placing "new Folder". But it doesn't work. When I execute the script in photoshop, nothing happens. Do you have any idea what the problem is?
With a slitly alteration the script looks like this:
Code: Select all// select folder, create document, place three of the images in the folder randomly;
// 2014, use it at your own risk;
#target photoshop
var theFolder = new Folder ("E:\WORK\PARTICLE_PAINTINGS\Sessie456_Finals\Layers\INVERT_Layers\RANDOMIZING");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
if (theFiles.length > 2) {
// shuffle files;
var theFiles = arrayShuffle(theFiles);
// set to pixels;
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theImage = app.documents.add(1920, 1080, 72, "image", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
var theLayers = new Array;
// place images;
for (var m = 0; m <= 2; m++) {
var theLayer = placeScaleFile (theFiles[m], 0, 0, 100* (1-(Math.floor(Math.random()*2))*2), 100, Math.random()*360);
theLayers.push (theLayer);
};
// reset;
app.preferences.rulerUnits = originalUnits;
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(jpg|tif|psd|pdf|)$/i)) {
return true
};
};
////// place //////
function placeScaleFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
// =======================================================
var idPlc = charIDToTypeID( "Plc " );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc5.putPath( idnull, new File( file ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc5.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idHrzn, idPxl, xOffset );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idVrtc, idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idWdth, idPrc, theYScale );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idHght, idPrc, theXScale );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc5.putUnitDouble( idAngl, idAng,theAngle );
var idLnkd = charIDToTypeID( "Lnkd" );
desc5.putBoolean( idLnkd, true );
executeAction( idPlc, desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
// http://www.hardcode.nl/subcategory_1/ar ... e-function
function arrayShuffle(theArray) {
var len = theArray.length;
var i = len;
while (i--) {
var p = parseInt(Math.random()*len);
var t = theArray;
theArray = theArray[p];
theArray[p] = t;
};
return theArray
};
Thank you for the comments.
Yes I am on PC. Somewhere I heard the same thing but I can't comfirm it.
But at this moment the rotation is not the big problem. After a slitly random rotation I can do the other rotation by hand.
But the simpel thing doesn't work. I also thouth that the way to place a folder in the script is by placing "new Folder". But it doesn't work. When I execute the script in photoshop, nothing happens. Do you have any idea what the problem is?
With a slitly alteration the script looks like this:
Code: Select all// select folder, create document, place three of the images in the folder randomly;
// 2014, use it at your own risk;
#target photoshop
var theFolder = new Folder ("E:\WORK\PARTICLE_PAINTINGS\Sessie456_Finals\Layers\INVERT_Layers\RANDOMIZING");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
if (theFiles.length > 2) {
// shuffle files;
var theFiles = arrayShuffle(theFiles);
// set to pixels;
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theImage = app.documents.add(1920, 1080, 72, "image", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
var theLayers = new Array;
// place images;
for (var m = 0; m <= 2; m++) {
var theLayer = placeScaleFile (theFiles[m], 0, 0, 100* (1-(Math.floor(Math.random()*2))*2), 100, Math.random()*360);
theLayers.push (theLayer);
};
// reset;
app.preferences.rulerUnits = originalUnits;
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(jpg|tif|psd|pdf|)$/i)) {
return true
};
};
////// place //////
function placeScaleFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
// =======================================================
var idPlc = charIDToTypeID( "Plc " );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc5.putPath( idnull, new File( file ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc5.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idHrzn, idPxl, xOffset );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idVrtc, idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idWdth, idPrc, theYScale );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( idHght, idPrc, theXScale );
var idAngl = charIDToTypeID( "Angl" );
var idAng = charIDToTypeID( "#Ang" );
desc5.putUnitDouble( idAngl, idAng,theAngle );
var idLnkd = charIDToTypeID( "Lnkd" );
desc5.putBoolean( idLnkd, true );
executeAction( idPlc, desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
// http://www.hardcode.nl/subcategory_1/ar ... e-function
function arrayShuffle(theArray) {
var len = theArray.length;
var i = len;
while (i--) {
var p = parseInt(Math.random()*len);
var t = theArray;
theArray = theArray[p];
theArray[p] = t;
};
return theArray
};
-
tyr
Randomizing scripts
Hm, interesing! Here's my result, I rounded it up a bit (Math.round(Math.random()*1000))
http://pastebin.com/hiPjrdCF
http://pastebin.com/hiPjrdCF
-
salmonickatelier
Randomizing scripts
That is more random! But how can I implecate the Math.round in the script that it is part of the rotation? I tried to put your code instead of this code Math.random()*360). Is that correct?
-
pfaffenbichler
Randomizing scripts
Salmonick, have you made sure the path is correct?
You could try
var theFolder = Folder.selectDialog ("select folder");
alert (theFolder);
to verify the path.
You could try
var theFolder = Folder.selectDialog ("select folder");
alert (theFolder);
to verify the path.