Group text layers based on font size

Engaged ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

Hello and greetings to all

I have a file that contains text layers with different font sizes
I want code that groups text layers based on input font size
For example (photo attached)
White text layers with a font size of 4.75 points
Yellow text layers with a font size of 3.5 points
I want when I write the number 4.75 it sums up the layers that have a size of 4.75

1.png

https://drive.google.com/file/d/1qrs6eFbpaqIma-MVdSFuV9ANNYbwl-Pq/view?usp=sharing 

TOPICS
Actions and scripting , SDK

Views

91

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Adobe Community Professional , Feb 22, 2022 Feb 22, 2022
/**Group text layers based on font size * https://community.adobe.com/t5/photoshop-ecosystem-discussions/group-text-layers-based-on-font-size/td-p/12769300 */ #target photoshop var s2t = stringIDToTypeID, fontSizeList = {}; (r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers')); r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum')); var len = executeActionGet(r).getInteger(p); for (var i = 1; i <= len; i++) { (r = new ActionReference()).put...

Likes

Translate

Translate
LEGEND , Feb 22, 2022 Feb 22, 2022
objct = {}, aLs = [].slice.call (activeDocument.artLayers); while(aLs.length) (shft = aLs.shift()).kind == 'LayerKind.TEXT' && ((!objct[sze = shft.textItem.size.value] && objct[sze] = []), objct[sze].push(shft.id)) sTT = stringIDToTypeID; while(objct.__count__) { array = objct[zro = objct.reflect.properties[0]] ref = new ActionReference(); dsc = new ActionDescriptor() while(array.length) ref.putIdentifier(sTT('layer'), array.shift()) dsc.putReference(sTT('null'), ref), executeAction(sTT(...

Likes

Translate

Translate
Adobe Community Professional ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

/**Group text layers based on font size
 * https://community.adobe.com/t5/photoshop-ecosystem-discussions/group-text-layers-based-on-font-size/td-p/12769300
 */
#target photoshop

var s2t = stringIDToTypeID,
    fontSizeList = {};

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p);

for (var i = 1; i <= len; i++) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('textKey'));
    r.putIndex(s2t('layer'), i);
    if (executeActionGet(r).hasKey(p)) {
        var sList = executeActionGet(r).getObjectValue(p).getList(s2t('textStyleRange'));
        for (var x = 0; x < sList.count; x++) {
            var k = sList.getObjectValue(x).getObjectValue(s2t('textStyle'))
            if (k.hasKey(s2t('impliedFontSize'))) {
                var size = Math.round(k.getUnitDoubleValue(s2t('impliedFontSize')) * 100) / 100
                if (fontSizeList[size]) fontSizeList[size].push(i) else fontSizeList[size] = [i]
            }
        }
    }
}

if (fontSizeList.toSource() != '({})') {
    var w = new Window("dialog {text: 'Group by font size'}"),
        dl = w.add("dropdownlist{preferredSize: [200,-1]}"),
        g = w.add("group"),
        ok = g.add("button", undefined, "Group", { name: "ok" }),
        cancel = g.add("button", undefined, "Cancel", { name: "cancel" });

    for (var a in fontSizeList) dl.add('item', a)
    dl.selection = 0

    ok.onClick = function () {
        var r = new ActionReference(),
            cur = fontSizeList[dl.selection.text];
        do { r.putIndex(s2t("layer"), cur.shift()) } while (cur.length)
        (d = new ActionDescriptor()).putReference(s2t("target"), r)
        executeAction(s2t("select"), d, DialogModes.NO);
        (r = new ActionReference()).putClass(s2t("layerSection"));
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        (r1 = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        d.putReference(s2t("from"), r1);
        (d1 = new ActionDescriptor()).putString(s2t("name"), dl.selection.text);
        d.putObject(s2t("using"), s2t("layerSection"), d1);
        executeAction(s2t("make"), d, DialogModes.NO);
        w.close()
    }
    w.show();
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

@jazz-y 

Thank you very much and I am very happy with this help
I wish you good luck and success always

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

LATEST
objct = {}, aLs = [].slice.call
(activeDocument.artLayers); while(aLs.length)
	(shft = aLs.shift()).kind == 'LayerKind.TEXT'
	&& ((!objct[sze = shft.textItem.size.value] &&
	objct[sze] = []), objct[sze].push(shft.id))

sTT = stringIDToTypeID; while(objct.__count__) {
	array = objct[zro = objct.reflect.properties[0]]
	ref = new ActionReference(); dsc = new ActionDescriptor()
	while(array.length) ref.putIdentifier(sTT('layer'), array.shift())
	dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc);

	(ref1 = new ActionReference()).putClass(sTT('layerSection'));
	(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1);
	(ref2 = new ActionReference()).putEnumerated(sTT('layer'), sTT('ordinal'), sTT('targetEnum'))
	dsc1.putReference(sTT('from'), ref2); (dsc2 = new ActionDescriptor()).putString(sTT('name'), zro.name)
	dsc1.putObject(sTT('using'), sTT('layerSection'), dsc2), executeAction(sTT('make'), dsc1); delete objct[zro]
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines