Skip to main content
Bedazzled532
Inspiring
March 13, 2025
Answered

Script equivalent of Indesign Command

  • March 13, 2025
  • 3 replies
  • 9517 views

Hi

I am looking for a script to carry out following command in Indesign. 

Ctrl + Alt + \     and    Ctrl+Alt+Shift+\   => to increase word space

Ctrl + Alt + Bksp     and    Ctrl+Alt+Shift+Bksp  => to decrease word space 

from selected text. Actually I need an interface where I can change the values and see the result live in the text.

 

Thanks

Correct answer m1b

@m1b Thank you so much for this wonderful script. This is actually what I was looking for. The keyboard shortcuts are taking long to show the effect. This script is faster than that. So yeah this is going to work for me.

@Robert at ID-Tasker @Peter Spier Thanks for your efforts also.

@m1b Can there be a text box where we can mention that start increasing space by this value? It would also help.Also there should be one more button with 4 <<<< and 4 >>>> to speedup the process. Or there should be be a button with "x" (x times increase or decrease) How is this idea?

Thanks a ton!!


Hi @Bedazzled532 I will update the script above with some extra buttons and also some improvements, such as better handling of Undo.

- Mark

3 replies

m1b
Community Expert
March 14, 2025

Hi @Bedazzled532, I have written a script that may help you. Let me know what you think. For instructions, read the script documentation below.

 

Note that it doesn't change "word spacing" per se, because that is a property of a *paragraph*, so instead this script targets the first space after a word boundary and sets its tracking value. To ensure that things don't get too messy, the script will set every word space to the same amount each time you click one of the buttons. I think this will work best.

 

In RTL text like Arabic, I think maybe the buttons should go RTL also, so I've put a setting in to set that. It is ON by default.

 

Let me know how it works.

- Mark

 

 

 

 

 

/**
 * @file Word Spacer.js
 *
 * Shows a modeless palette with 7 buttons.
 *
 * To use:
 * 1. Invoke this Script in the Scripts Panel, if palette is not already open.
 * 2. Select some text
 * 3. Click one of the buttons
 *    - the center button will reset the word spacing
 *    - the other buttons will increment or decrement the word spacing
 *
 * Notes:
 *    - if `settings.RTL` is true then the order of the buttons will
 *      be reversed, to work better with RTL text systems.
 *    - you can customize the `settings.spacings` to suit your preferences.
 *    - the center `settings.spacings` is the "reset" value.
 *
 * @author m1b
 * @version 2025-03-14
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-equivalent-of-indesign-command/m-p/15209276
 */

//@targetengine "wordSpacer"

(function () {

    var settings = {

        /**
         * if true, will reverse the button spacings
         * to work better with RTL text systems
         */
        RTL: true,

        /**
         * the spacing values, smallest to largest,
         * in the font's em-units
         *   - the center value is the "reset" value
         *   - the number of spacings will also effect
         *     how many buttons appear in the UI, so if
         *     spacings are [-50, -10, -1, 0, 1, 10, 50]
         *     then only 7 buttons will appear
         */
        spacings: [-250, -50, -10, -1, 0, 1, 10, 50, 250],

        /**
         * the button labels in the UI
         */
        buttonLabels: ['<<<<', '<<<', '<<', '<', '0', '>', '>>', '>>>', '>>>>'],

    };

    if (settings.RTL)
        settings.spacings.reverse();

    // UI button sizes
    var buttonSizes = [[52, 46], [46, 46], [40, 40], [35, 35], [135, 35], [35, 35], [40, 40], [46, 46], [52, 46],];

    // make the buttonLabels and sizes match the spacings
    while (settings.buttonLabels.length > settings.spacings.length) {
        settings.buttonLabels.pop();
        settings.buttonLabels.shift();
        buttonSizes.pop();
        buttonSizes.shift();
    }

    var doc = app.activeDocument,
        len = settings.spacings.length,
        middle = Math.floor(len / 2);

    // make the palette
    var w = new Window("palette", "Word Spacer"),
        buttons = w.add("Group {orientation:'row', alignment:['center','top'], margins:[0,0,0,0]}");


    // make the buttons
    for (var i = 0; i < len; i++) {

        (buttons.add("Button {text:'" + settings.buttonLabels[i] + "',preferredSize:[" + buttonSizes[i] + "]}"))
            .onClick = (middle === i)
                ? resetter(settings.spacings[i])
                : incrementer(settings.spacings[i]);

    }

    var resetButton = buttons.children[middle];

    w.show();

    function incrementer(n) {
        return function () {
            resetButton.text = wordSpace({
                text: doc.selection[0],
                increment: n,
            })
        };
    };

    function resetter(n) {
        return function () {
            resetButton.text = wordSpace({
                text: doc.selection[0],
                base: n,
            })
        };
    };

    /**
     * Changes the tracking of spaces in the `text`.
     * @author m1b
     * @version 2025-03-14
     * @param {Object} options
     * @param {Text} options.text - the text to change.
     * @param {Number} [options.base] - the absolute value to apply to spaces tracking.
     * @param {Number} [options.increment] - the change to apply to spaces tracking.
     * @returns {Number} - the tracking value applied.
     */
    function wordSpace(options) {

        var base = options.base,
            increment = options.increment;

        if (
            0 === base
            && 0 === increment
        )
            return;

        var text = doc.selection[0];

        if ('function' !== typeof text.findGrep)
            return;

        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;

        app.findGrepPreferences.findWhat = '\\s(?=\\b)';

        var spaces = text.findGrep();

        if (0 === spaces.length)
            return;

        if (undefined == base)
            base = spaces[0].insertionPoints[-1].kerningValue;

        var kerning = base + (increment || 0);

        app.doScript(function () {

            // do the word spacing
            for (var i = spaces.length - 1; i >= 0; i--)
                spaces[i].insertionPoints[-1].kerningValue = kerning;

        }, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Word Spacing');

        return kerning;

    };

})();

 

Edit 2025-03-14: improved script documentation.

Edit 2025-03-14: changed spacing method so that it is compatible with Indesign's method.
Edit 2025-03-15: added an extra increment button, both ways, added easier to edit button labels, and improved the script's interaction with undo.

Robert at ID-Tasker
Brainiac
March 14, 2025

@m1b

 

Out of curiosity - won't the effect be the same as hitting those original keyboard shortcuts?

 

m1b
Community Expert
March 14, 2025

@Robert at ID-Tasker 

> Out of curiosity - won't the effect be the same as hitting those original keyboard shortcuts?

 

Yes! But can you believe that I only just *right now from this thread* learned about that feature! Thanks @Bedazzled532 you have taught me something that will be quite handy! So yes what my script does is the same as the built-in feature (only very slightly different because instead of kerning after the space it tracks the space).

 

But the idea is to provide a more flexible interface, with customizable values. I made it with a certain idea of how OP is going to use it, but I may have totally missed the mark. In any case, it is a script that can easily be repurposed for other uses so it'll be good to have on hand. Also, it would be easy to add extra buttons for more granular control, for example.

 

@Bedazzled532 as for automating it over 2000 pages, my script isn't doing that at all, it is a manual helper only, sorry.

- Mark


Actually it makes sense to do the spacing the same as Indesign's way, so I've edited the script above to do it that way.

Peter Spier
Community Expert
March 13, 2025

Just a couple of questions for my edification...

Are the frames for each language discrete or do they thread? Is each one a complete paragraph? Which composer are you using?

I ask this because I wonder what's going to happen downstream as you adjust word spacing if this is flowing text.

Robert at ID-Tasker
Brainiac
March 13, 2025

@Peter Spier

 

Those are two separate Stories.

 

Peter Spier
Community Expert
March 13, 2025

@Robert at ID-Tasker  I understand that there are two separate stories for the two languages, so perhaps I wasn't clear in what I'm asking. Is every frame on the page a separate story, or do the two languages thread down the page, and more importantly, to the next page.

Bedazzled532
Inspiring
March 13, 2025

This is not working. Says wordSpacing does not exist. I have taken a value of 200 just for testing purpose:

    var selection = app.selection;

    if (selection.length > 0 && selection[0] instanceof Text) {
        var textRange = selection[0];
        textRange.wordSpacing += 200;
    } else {
        alert("Please select some text first.");
    }
Robert at ID-Tasker
Brainiac
March 13, 2025

Because thete is no "wordSpacing" property. 

 

ChatGPT? 

 

It would be rather kerningValue - but not for the whole text. 

 

Bedazzled532
Inspiring
March 13, 2025

@Robert at ID-Tasker Yes chatgpt. I have scratched all my hairs and not able to imitate this command using script. Yes kerning value does not work for whole text.

If I select the whole text and then run ctrl+alt+\   this increases the space between words nicely. I am not able to get this.