Skip to main content

3 replies

Robert at ID-Tasker
Legend
May 1, 2025

@Lux_65

 

If you work on Windows and have to do it a lot - then you could use my ID-Tasker tool - isn't free but you have direct access to all 400+ text properties - so you can create CharStyles based on any of those properties. And you can also automatically process multiple INDD documents.

 

Plus 1000+ object properties - so you can process only texts from specific layers, pages - ranges / left / right, (X, Y) locations, width, height, etc. 

 

You can play with a free version - but you'll have to do a bit of extra clicking. 

 

Of course, I can give you access to the full version for free for a few days. 

 

Community Expert
May 1, 2025

I've tested this a bit and seems to work 

 

if (app.documents.length == 0) exit();

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Prep Text Styles");

function main() {
    var sel = app.selection[0];
    if (!sel || !(sel.hasOwnProperty("characters") || sel.constructor.name === "Text" || sel.constructor.name === "TextFrame")) {
        alert("You must select some text.");
        return;
    }

    var textRange;
    try {
        textRange = sel.hasOwnProperty("parentStory") ? sel.parentStory : sel;
        if (!textRange.findText) throw "Not valid text";
    } catch (e) {
        alert("Selected object does not support findText. Please select some real text.");
        return;
    }

    var myProgressPanel = myCreateProgressPanel(64, 384);
    myProgressPanel.show();

    for (var b = 0; b < 2; b++) {
    for (var i = 0; i < 2; i++) {
    for (var s = 0; s < 2; s++) {
    for (var u = 0; u < 2; u++) {
    for (var k = 0; k < 2; k++) {
    for (var c = 0; c < 2; c++) {
    for (var ac = 0; ac < 2; ac++) {
        if ((b + i + s + u + k + c + ac) && !(ac & c)) {
            var val = 64 * b + 32 * i + 16 * s + 8 * u + 4 * k + 2 * c + ac;
            myProgressPanel.myProgressBar.value = val;
            findAttr(b, i, s, false, u, k, c, ac, textRange);
            if (s) findAttr(b, i, false, s, u, k, c, ac, textRange);
        }
    }
    }
    }
    }
    }
    }
    }

    myProgressPanel.hide();
}

function myCreateProgressPanel(maxVal, barWidth) {
    var w = new Window('window', 'Prepping text');
    w.myProgressBar = w.add('progressbar', [12, 12, barWidth, 24], 0, maxVal);
    return w;
}

function findAttr(bold, italic, superscript, subscript, underline, strikeout, smallcaps, allcaps, textRange) {
    app.findTextPreferences = NothingEnum.nothing;
    app.changeTextPreferences = NothingEnum.nothing;

    var prefs = app.findTextPreferences;
    prefs.appliedCharacterStyle = app.activeDocument.characterStyles[0];
    prefs.fontStyle = "Regular";
    prefs.position = Position.normal;
    prefs.capitalization = Capitalization.normal;
    prefs.underline = false;
    prefs.strikeThru = false;

    if (bold && italic) prefs.fontStyle = "Bold Italic";
    else if (bold) prefs.fontStyle = "Bold";
    else if (italic) prefs.fontStyle = "Italic";

    if (superscript) prefs.position = Position.superscript;
    if (subscript) prefs.position = Position.subscript;
    if (underline) prefs.underline = true;
    if (strikeout) prefs.strikeThru = true;
    if (allcaps) prefs.capitalization = Capitalization.allCaps;
    else if (smallcaps) prefs.capitalization = Capitalization.smallCaps;

    var foundItems = textRange.findText();
    for (var i = 0; i < foundItems.length; i++) {
        var ch = foundItems[i];
        var fillColour = "None";
        try {
            fillColour = ch.fillColor.name.replace(/\[|\]/g, "");
        } catch (e) {}

        var styleNameParts = [];
        if (bold) styleNameParts.push("Bold");
        if (italic) styleNameParts.push("Italic");
        if (superscript) styleNameParts.push("Sup");
        if (subscript) styleNameParts.push("Sub");
        if (underline) styleNameParts.push("Und");
        if (strikeout) styleNameParts.push("Stk");
        if (allcaps) styleNameParts.push("Caps");
        else if (smallcaps) styleNameParts.push("Scaps");

        if (fillColour && fillColour !== "None") styleNameParts.push(fillColour);

        var styleName = styleNameParts.join("_");
        if (!styleName) continue;

        var charStyles = app.activeDocument.characterStyles;
        var cstyle;
        try {
            cstyle = charStyles.itemByName(styleName);
            if (!cstyle.isValid) throw "nope";
        } catch (e) {
            try {
                cstyle = charStyles.add({
                    name: styleName,
                    fontStyle: prefs.fontStyle,
                    underline: prefs.underline,
                    strikeThru: prefs.strikeThru,
                    position: prefs.position,
                    capitalization: prefs.capitalization,
                    fillColor: app.activeDocument.colors.itemByName(fillColour)
                });
            } catch (e2) {
                alert("Failed to create style " + styleName + ": " + e2);
                continue;
            }
        }

        app.changeTextPreferences.appliedCharacterStyle = cstyle;
        ch.changeText();
    }
}

 

Community Expert
May 1, 2025

Except plain text that's coloured doesn't get a character style of it's own - unless you want that to happen let us know.