Skip to main content
Participant
May 5, 2023
Answered

copy/paste 4 digits at the beginning of a paragraph

  • May 5, 2023
  • 2 replies
  • 830 views

Hi All,

Here's the translation in English:

"The AI has failed, so I'm turning to the HI for a small script: in an open document, I want to copy four digits that already have a character style called 'red' located anywhere within a paragraph (except at the beginning), and paste them at the beginning of each paragraph."

Thanks for your help

This topic has been closed for replies.
Correct answer m1b

A tiny change does it.

/**
 * Repeat 4 digit codes at start of paragraphs.
 * Codes are identified by their character style.
 * @7111211 m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/copy-paste-4-digits-at-the-beginning-of-a-paragraph/m-p/13773037
 */
function main() {

    var settings = {
        characterStyleName: 'red',
        afterCode: ' ',
        ignoreParagraph: /^\d{4}/,
    };

    var doc = app.activeDocument;

    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.appliedCharacterStyle = settings.characterStyleName;

    var found = doc.findGrep();

    for (var i = found.length - 1; i >= 0; i--) {

        if (settings.ignoreParagraph.test(found[i].paragraphs[0].contents))
            continue;

        found[i].paragraphs[0].insertionPoints[0].contents = found[i].texts[0].contents + settings.afterCode;

    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Codes To Start');

2 replies

m1b
Community Expert
Community Expert
May 5, 2023

Hi @tournierlaurent, this is what I had in mind. Does this do what you need? You can adjust the settings if you like. It's quite straightforward. The only little fiddle is telling script to ignore a paragraph that already starts with 4 digits (see the settings.ignoreParagraph RegExp). Hope it works for you.

- Mark

 

/**
 * Repeat 4 digit codes at start of paragraphs.
 * Codes are identified by their character style.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/copy-paste-4-digits-at-the-beginning-of-a-paragraph/m-p/13773037
 */
function main() {

    var settings = {
        characterStyleName: 'red',
        afterCode: ' ',
        ignoreParagraph: /^\d{4}/,
    };

    var doc = app.activeDocument;

    if (
        doc.selection.length == 0
        || app.selection[0].findGrep == undefined
    ) {
        alert('Please make a selection and try again.')
        return;
    }

    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.appliedCharacterStyle = settings.characterStyleName;

    var found = doc.selection[0].findGrep();

    for (var i = found.length - 1; i >= 0; i--) {

        if (settings.ignoreParagraph.test(found[i].paragraphs[0].contents))
            continue;

        found[i].paragraphs[0].insertionPoints[0].contents = found[i].texts[0].contents + settings.afterCode;

    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Codes To Start');

 

 

Participant
May 5, 2023

I just have one last request: how can the script be adapted to work on the entire open document, without needing to select a story?

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
May 5, 2023

A tiny change does it.

/**
 * Repeat 4 digit codes at start of paragraphs.
 * Codes are identified by their character style.
 * @7111211 m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/copy-paste-4-digits-at-the-beginning-of-a-paragraph/m-p/13773037
 */
function main() {

    var settings = {
        characterStyleName: 'red',
        afterCode: ' ',
        ignoreParagraph: /^\d{4}/,
    };

    var doc = app.activeDocument;

    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.appliedCharacterStyle = settings.characterStyleName;

    var found = doc.findGrep();

    for (var i = found.length - 1; i >= 0; i--) {

        if (settings.ignoreParagraph.test(found[i].paragraphs[0].contents))
            continue;

        found[i].paragraphs[0].insertionPoints[0].contents = found[i].texts[0].contents + settings.afterCode;

    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Codes To Start');
Community Expert
May 5, 2023

Can you share some screenshots to explain the problem statement

-Manan

-Manan
Participant
May 5, 2023

Sure