Copy link to clipboard
Copied
I want to randomly apply styles to my ArtLayers. To do this, I tested the applyStyle function.
It works!)
But to solve my task I need all styles. I can't find it in the documentation (photoshop-javascript-ref-2020).
Can you suggest something?
Copy link to clipboard
Copied
A track where you must know the path of your PS presets :
var stylesFound = false;
main();
if (!stylesFound) {
alert("No styles found. You should verify the path to your Photoshop style presets.");
}
function main() {
var styleFolder = Folder('/c/Program Files (x86)/Adobe/Adobe Photoshop CS5/Presets/Styles');
// Set all style names in an array
var styleList = styleFolder.getFiles(/\.(asl|)$/i);
//styleList = styleFile.substring( 0, docRef.name.indexOf('.') );
// STOP if not any ASL file found
stylesFound = true;
if (styleList.length==0) {
stylesFound = false;
return;
}
// Random
var rnd = Math.floor(Math.random() * styleList.length);
var styleName = styleList[rnd].name;
alert(styleName);
}
Copy link to clipboard
Copied
Did @michelr31372089 ’s post help you in this task?
Copy link to clipboard
Copied
activeDocument.suspendHistory('Apply random style', 'main()');
function main() {
activeDocument.artLayers.add();
var stylesIndexes = applyStyles();
activeDocument.activeLayer.remove();
applyStyles(stylesIndexes[Math.floor(Math.random() * stylesIndexes.length)])
}
function applyStyles(idx) {
var s2t = stringIDToTypeID,
err = 0,
cur = 0,
stylesIdx = [];
do {
(r = new ActionReference()).putIndex(s2t("style"), idx ? idx : cur);
(d = new ActionDescriptor()).putReference(s2t("null"), r);
(r1 = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
d.putReference(s2t("to"), r1);
try {
executeAction(s2t("applyStyle"), d, DialogModes.NO);
if (idx) break;
err = 0
stylesIdx.push(cur)
} catch (e) {
err++
}
cur++
} while (err < 5)
return stylesIdx;
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now