• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

copy/paste 4 digits at the beginning of a paragraph

Community Beginner ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

580

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 05, 2023 May 05, 2023

A tiny change does it.

/**
 * 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;

    app.findGrepPreferences = NothingEnum.NOTHI
...

Votes

Translate

Translate
Community Expert ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

Can you share some screenshots to explain the problem statement

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

Suredigits.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

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');

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

Hi m1b

Sorry, I hadn't seen your solution. It works perfectly.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

A tiny change does it.

/**
 * 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;

    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');

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2023 May 05, 2023

Copy link to clipboard

Copied

LATEST

Thanks a lot

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines