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?

 

Bedazzled532
Inspiring
March 14, 2025

@Bedazzled532 

 

It's great to hear, that this script would help you.

 

But if I may.

 

I was just thinking and if you don't need fully automated solution - as I've mentioneed in my other post, you could get the same effect with my IDT - but it should be a bit more handy - and completely free.

 

 

As per the screenshot above (*) - all texts in between numbers can be loaded into IDT. Most likely a better GREP expression would be needed.

 

Then, you would've to create a few simple Tasks - free version of IDT won't let you load more than one - and just use up / down arrows to switch between found results (*) - IDT will automatically select corresponding text, including switching pages - and expand / contract texts using keyboard shortcuts assigned to those Tasks.

 

Of course, each Task can do whatever you want - just invoke those built-in keyboard shortcuts - or set / increase by whatever values you wan't. 

 

And, the same as in the InDesign - you can use Ctrl / Shift / Alt modifiers to use the same base key - with different "step values".

 

100% mouseless solution 😉 so should be much quicker 😉

 

And, if at any point, you would need to select only part of the original text (*) - when it spans multiple lines, but manipulating all lines at once won't get you correct result - or you simply don't want to "touch" them - and you would need to select only one last line of the selection - you can create additional Task, that will select first or last line, assing a kyboard shortcut - and still avoid using mouse.

 

 

(*) screenshot shows the last (*) I'm mentioning - all found results has been already split into separate lines.

But it's not the end of the world 😉 because, when working in IDT - when user use SHIFT and up/down arrows - IDT will automatically select BOTH "objects" on the list - as loong as it's possible - pieces of text, objects on the same spread / within group, cells in the Table. Of course selection can be expanded to further elements on the list.

 


@Robert at ID-Tasker Thanks. A sample video with my example will be helpful and easy for me to understand. It sounds good that all of this can be done without mouse.

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.