// randomly place smart objects;
// 2015, use it at your own risk;
#target "photoshop-70.032"
if (app.documents.length > 0) {
app.activeDocument.suspendHistory("place", "main ()");
};
////////////////////////////////////
function main () {
// the file;
var myDocument = app.activeDocument;
var theFiles = selectFile(true);
if (theFiles) {
// the blend modes;
var blendModes = [BlendMode.NORMAL, BlendMode.SCREEN, BlendMode.MULTIPLY, BlendMode.OVERLAY];
// set to pixels;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get half width and height;
var hori = myDocument.width/2;
var vert = myDocument.height/2;
var theLayers = new Array;
// rotation between -60˚ and 60˚;
var rot = new Array;
for (var n = 0; n < theFiles.length; n++) {
rot.push((0.5-Math.random())*120)
};
// blend modes;
var mod = new Array;
for (var o = 0; o < theFiles.length; o++) {
mod.push(blendModes[Math.floor(Math.random()*blendModes.length)])
};
// place;
for (var m = 0; m < theFiles.length; m++) {
// set scale between 50 and 100%;
var theScale = 50+(Math.random()*50);
var thisLayer = placeScaleRotateFile (theFiles, (0.5-Math.random())*hori, (0.5-Math.random())*vert, theScale, theScale, rot, false);
myDocument.activeLayer.blendMode = mod;
theLayers.push(thisLayer);
};
// reset;
app.preferences.rulerUnits = startRulerUnits;
}
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle, linked) {
// =======================================================
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 );
if (linked == true) {
var idLnkd = charIDToTypeID( "Lnkd" );
desc5.putBoolean( idLnkd, true );
};
executeAction( idPlc, desc5, DialogModes.NO );
// get layerid;
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("layerID"));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var layerID = layerDesc.getInteger (stringIDToTypeID ("layerID"));
// =======================================================
return [app.activeDocument.activeLayer, layerID];
};
////// select file //////
function selectFile (multi) {
if (multi == true) {var theString = "please select files"}
else {var theString = "please select one file"};
if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog (theString, '*.jpg;*.tif;*.psd;*.png', multi)}
else {var theFiles = File.openDialog (theString, getFiles, multi)};
////// filter files for mac //////
function getFiles (theFile) {
if (theFile.name.match(/\.(jpg|tif|psd|png)$/i) || theFile.constructor.name == "Folder") {
return true
};
};
return theFiles
};