Change Letter Case

JavaScript for Adobe InDesign

Change matching text to uppercase, lowercase, title case, or sentence case. A recent project I had to convert a directory created in InDesign to HTML for a website. In the layout, the categories and company names used a paragraph style set to all-caps, and the client wanted the same look online. But the Content Management System where it was going didn't have a way to set up classes for the all-caps text decoration. So I made this script to convert the text in the particular paragraph styles to actual capital letters, not just a styling assignment. Then I figured add the character style option and match with GREP, too. Could come in handy in the future.

  • Match with GREP
  • Match by character style
  • Match by paragraph style
  • Adapt open source to create other scripts
Change Letter Case screen
Download
Change Letter Case

You decide. Reward the author an
amount the solution is worth to you.

Instructions for use

The interface is one input field and three drop-down lists. Set desired options, and then click the OK button to begin.

GREP — enter a GREP expression just as in the InDesign Find/Change dialog. Characters that match the GREP expression are changed to the letter case set below.

Character Style — select a character style and characters assigned the style are changed to the letter case set below.

Paragraph Style — select a paragraph style and paragraphs assigned the style are changed to the letter case set below. Changes the entire paragraph.

Change to — the desired letter case. Choices are UPPERCASE, lowercase, Title Case, and Sentence case.

Source code

(download button below)

/*

Change Letter Case
Copyright 2021 William Campbell
All Rights Reserved
william@marspremedia.com
willcampbell7@gmail.com
https://www.marspremedia.com/contact

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

//@target indesign

(function () {

    var title = "Change Letter Case";

    // Preserve preferences.
    var findChangeTextOptions = app.findChangeTextOptions.properties;
    var findGrepPreferences = app.findGrepPreferences.properties;
    var findTextPreferences = app.findTextPreferences.properties;

    // Script variables.
    var changecaseModes = [
        ChangecaseMode.UPPERCASE,
        ChangecaseMode.LOWERCASE,
        ChangecaseMode.TITLECASE,
        ChangecaseMode.SENTENCECASE
    ];
    var charStyleNames;
    var doc;
    var i;
    var letterCase = [
        "UPPERCASE",
        "lowercase",
        "Title Case",
        "Sentence case"
    ];
    var paraStyleNames;

    // Reusable UI variables.
    var g; //  group
    var g1; // group
    var g2; // group
    var p; //  panel
    var w; //  window

    // Permanent UI variables.
    // Prefixes:
    //   btn  = "button"
    //   inp  = "edittext"
    //   list = "dropdownlist"
    var btnCancel;
    var btnOk;
    var inpGrep;
    var listCharStyle;
    var listLetterCase;
    var listParaStyle;
    var panelMargins = [18, 18, 18, 12];

    // SETUP

    // Check for valid doc.
    if (!app.documents.length) {
        alert("Open a document.", title, false);
        return;
    }
    doc = app.activeDocument;

    // Load character styles.
    charStyleNames = doc.characterStyles.everyItem().name;
    charStyleNames.sort();

    // Load paragraph styles.
    paraStyleNames = doc.paragraphStyles.everyItem().name;
    paraStyleNames.sort();

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";
    p = w.add("panel");
    p.orientation = "column";
    p.alignChildren = "left";
    p.margins = panelMargins;
    // Group labels and drop down lists.
    g1 = p.add("group");
    // Group labels.
    g2 = g1.add("group");
    g2.orientation = "column";
    g2.alignChildren = "left";
    g2.spacing = 18;
    g2.add("statictext", undefined, "GREP:");
    g2.add("statictext", undefined, "Character Style:");
    g2.add("statictext", undefined, "Paragraph Style:");
    g2.add("statictext", undefined, "Change to:");
    // Group drop down lists.
    g2 = g1.add("group");
    g2.orientation = "column";
    g2.alignChildren = "left";
    g2.spacing = 6;
    inpGrep = g2.add("edittext");
    inpGrep.preferredSize = [250, 25];
    listCharStyle = g2.add("dropdownlist", undefined, undefined, {
        items: charStyleNames
    });
    listCharStyle.preferredSize = [250, 25];
    listParaStyle = g2.add("dropdownlist", undefined, undefined, {
        items: paraStyleNames
    });
    listParaStyle.preferredSize = [250, 25];
    listLetterCase = g2.add("dropdownlist", undefined, undefined, {
        items: letterCase
    });
    listLetterCase.preferredSize = [250, 25];

    // Action Buttons
    g = w.add("group");
    g.alignment = "center";
    g.margins = [0, 0, 0, 12];
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");

    // Action Buttons
    btnOk.onClick = function () {
        w.close(1);
    };
    btnCancel.onClick = function () {
        w.close(0);
    };

    // DISPLAY THE DIALOG

    if (w.show() === 1) {
        try {
            app.doScript(process, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, title);
        } catch (e) {
            alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);
        }
    }

    // Restore preferences.
    app.findChangeTextOptions.properties = findChangeTextOptions;
    app.findGrepPreferences.properties = findGrepPreferences;
    app.findTextPreferences.properties = findTextPreferences;

    //====================================================================
    //               END PROGRAM EXECUTION, BEGIN FUNCTIONS
    //====================================================================

    function process() {
        var changecaseMode;
        var doc = app.activeDocument;
        var m;
        var matches;
        var story;
        // Set find change text options.
        app.findChangeTextOptions.properties = {
            includeFootnotes: true,
            includeHiddenLayers: true,
            includeLockedLayersForFind: true,
            includeLockedStoriesForFind: true,
            includeMasterPages: false
        };
        app.findGrepPreferences.findWhat = inpGrep.text;
        if (listCharStyle.selection) {
            app.findGrepPreferences.appliedCharacterStyle = listCharStyle.selection.text;
        }
        if (listParaStyle.selection) {
            app.findGrepPreferences.appliedParagraphStyle = listParaStyle.selection.text;
        }
        if (listLetterCase.selection) {
            changecaseMode = changecaseModes[listLetterCase.selection.index];
            for (i = 0; i < doc.stories.length; i++) {
                story = doc.stories[i];
                matches = story.findGrep();
                for (m = 0; m < matches.length; m++) {
                    matches[m].texts[0].changecase(changecaseMode);
                }
            }
        }
    }

})();
Download
Change Letter Case

License details included in download

For help installing scripts, see How to Install and Use Scripts in Adobe Creative Cloud Applications.

IMPORTANT: by downloading the script you agree that the software is provided without any warranty, express or implied. USE AT YOUR OWN RISK. Always make backups of important data.