Skip to main content
Participant
December 18, 2022
Question

How to get all Photoshop styles using photoshop scripting?

  • December 18, 2022
  • 3 replies
  • 417 views

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?

This topic has been closed for replies.

3 replies

Legend
January 12, 2023

 

 

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

 

 

 

c.pfaffenbichler
Community Expert
Community Expert
January 7, 2023

Did @MR74270 ’s post help you in this task? 

MR74270
Inspiring
December 27, 2022

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