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

Help writing a script

New Here ,
May 05, 2022 May 05, 2022

Copy link to clipboard

Copied

Hi,


I want to make a script, but I am unsure of how to write it. 

 

I want the script to choose the first sentence in a text-box, changes the swatch, duplicates the page, select the next sentence, change the swatch, repeat. I have attached an example of the result I want to acheive. 

 

Any tips or resources would be greatly appreciated!

TOPICS
Scripting

Views

222

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, 2022 May 05, 2022

Hi @printperson22, this was an intriguing exercise! I've written a script that I think does what you are asking for, but it is designed to be used in a particular set up:

1. a single page document with text contained in the first text frame on the page.

2. document should have character styles for "foreground" and "background" text colors.

3. document should have paragraph style with two Grep styles (see screenshot below) that set those character styles.

Screen Shot 2022-05-05 at 11.41.40 pm.png

The script finds all sentences and, afte

...

Votes

Translate

Translate
Community Expert ,
May 05, 2022 May 05, 2022

Copy link to clipboard

Copied

Hi @printperson22, this was an intriguing exercise! I've written a script that I think does what you are asking for, but it is designed to be used in a particular set up:

1. a single page document with text contained in the first text frame on the page.

2. document should have character styles for "foreground" and "background" text colors.

3. document should have paragraph style with two Grep styles (see screenshot below) that set those character styles.

Screen Shot 2022-05-05 at 11.41.40 pm.png

The script finds all sentences and, after duplicating the first page, drops a marker character at the start and end of the current page's sentence. The paragraph style handles the formatting—all the script needs to do is drop those markers. If the script gets the sentence positions wrong—which will happen sometimes (see caveat in script)— you can just move them manually by cutting and pasting the zero width spaces (show invisibles to see them). See screenshot.

Screen Shot 2022-05-05 at 11.43.02 pm.png

 

IMPORTANT: Please also download this sample indesign file (attached to this post) to see my set up.

- Mark

 

/*
    Mark Sentences and Duplicate Pages

    by m1b,
    here: https://community.adobe.com/t5/indesign-discussions/help-writing-a-script/m-p/12923892

    Known limitations:
    Grep used to determine sentence extents
    is *very basic* and will fail in many cases.
    Script is designed to be used in conjunction
    with an indesign document containing paragraph
    style with Grep Style "\x{200B}[^\x{200B}]+\x{200B}"
*/

function main() {

    var doc = app.activeDocument,
        page = doc.pages[0],
        textFrame = page.textFrames[0],

        // NOTE: grep to find sentences is *very*
        // naive and won't work in many cases
        sentenceGrepString = "[^ \\r\\n][^!?\\.\\r\\n]+[\\w!?\\.]+",

        // zero width space is added
        // as a marker between sentences
        marker = '\u200B';

    // find sentences in text frame
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = sentenceGrepString;
    var found = textFrame.findGrep();

    // for each sentence, duplicate page
    // and set markers at start and end
    for (var i = found.length - 1; i >= 0; i--) {
        var pg = (i == 0)
            ? page
            : page.duplicate(LocationOptions.AFTER, page);
        var tf = pg.textFrames[0],
            start = found[i].characters[0].index,
            end = found[i].characters[found[i].characters.length - 1].index;
        tf.characters[end + 1].insertionPoints[0].contents = marker;
        tf.characters[start].insertionPoints[0].contents = marker;
    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Mark Sentences and Duplicate Pages");

 

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
New Here ,
May 05, 2022 May 05, 2022

Copy link to clipboard

Copied

Wow, this is amazing! Thanks!

I will try it out with your file and then see if I can make it work with my text.

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
New Here ,
May 05, 2022 May 05, 2022

Copy link to clipboard

Copied

It works perfectly, thank you so much!

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, 2022 May 05, 2022

Copy link to clipboard

Copied

LATEST

Cool!

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