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

Insert character in front of text set with certain paragraph style

New Here ,
Dec 02, 2011 Dec 02, 2011

Copy link to clipboard

Copied

Hi

I am a beginner in the art of scriptning InDesign and I have a challenged which I know can be solved by javascript in InDesign.

This is in InDesign CS5.5.

Simply I want to insert a "S" in front of all paragraphs set with the paragraph style "Silkeborg".

I have struggled a bit with it and have made this:

var doc = app.activeDocument;

var intSelect = app.selection.length;

var arrSelection = [];

arrSelection = app.selection[0];

app.selection[0].insertionPoints[0].contents = "S ";

But this only inserts in the first paragraph.

TOPICS
Scripting

Views

2.1K

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 2 Correct answers

Community Beginner , Dec 04, 2011 Dec 04, 2011

Hi Jimmy,

here is a quick and dirty scripting solution to your problem. The code could be nicer but it gives you an impression about what needs to be done:

1. Loop through all stories and then through all paragraphs in each story

2. Determine if a paragraph has the applied style "Silkeborg"

3. If yes then select it and insert "S " before:

var oDoc = app.activeDocument;

//First loop through all stories then through all paragraphs in each story

for(n=0; n< oDoc.stories.length;n++){

    for(i=0;i<oDoc.stor

...

Votes

Translate

Translate
Community Expert , Feb 16, 2024 Feb 16, 2024

Doing it on my phone:

 

 

var oDoc = app.activeDocument;

//First loop through all stories then through all paragraphs in each story

for(n=0; n< oDoc.stories.length;n++){

    for(i=0;i<oDoc.stories[n].paragraphs.length;i++){

        // If paragraph style is "Silkeborg" then select paragraph and insert "S " before

        if  (oDoc.stories[n].paragraphs[i].appliedParagraphStyle.name == "Silkeborg") {

            oDoc.stories[n].paragraphs[i].insertionPoints[0].contents = "S ";

            }
...

Votes

Translate

Translate
Community Beginner ,
Dec 02, 2011 Dec 02, 2011

Copy link to clipboard

Copied

Hi Jimmy,

not a direct answer to your scripting question, but why not using a Numbering for your "Silkeborg" paragraph style?

numberingStyle.jpg

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 ,
Dec 04, 2011 Dec 04, 2011

Copy link to clipboard

Copied

Hi Wiseloc

Yes, that could work. Did not think of that.

Thanks.

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
LEGEND ,
Dec 02, 2011 Dec 02, 2011

Copy link to clipboard

Copied

Jimmy: What do you intend your script to do? Much of it doesn't make sense.

In particular:

var doc = app.activeDocument;
var intSelect = app.selection.length;
var arrSelection = [];
arrSelection = app.selection[0];
app.selection[0].insertionPoints[0].contents = "S ";

Only the last line does anything. The first 4 lines set variables that you never use!

Your last line inserts an 'S ' at the beginning of the currenty selected paragraph. Why would you expect it to do anything else?

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 ,
Dec 04, 2011 Dec 04, 2011

Copy link to clipboard

Copied

Hi John

I tried other things but that would only have looked worse in code.

As I said I am a new comer to scripting. So this was the beginning of my script.

Hopefully I will become better and can contribute to this forum.

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
Advocate ,
Dec 03, 2011 Dec 03, 2011

Copy link to clipboard

Copied

Hi Jimmy,

I think you expected this, You want to insert a "S" character for every Paragraphs of selected ares only.

Please try the below js code its working fine.

var doc = app.activeDocument;
var intSelect = app.selection[0].parentStory.paragraphs.everyItem();
intSelect.insertionPoints[0].contents = "Superb  ";

thx

csm_phil

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 ,
Dec 04, 2011 Dec 04, 2011

Copy link to clipboard

Copied

Hi csm_phil

Thanks. This does indeed do what I was thinking of.

What I also would like was to run through the paragraphs and only insert the "S " in front of paragraphs set with the paragraph style "Silkeborg".

The story can contain other paragraph Styles, which should not have "S " in the beginning.

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 ,
Dec 04, 2011 Dec 04, 2011

Copy link to clipboard

Copied

Hi Jimmy,

here is a quick and dirty scripting solution to your problem. The code could be nicer but it gives you an impression about what needs to be done:

1. Loop through all stories and then through all paragraphs in each story

2. Determine if a paragraph has the applied style "Silkeborg"

3. If yes then select it and insert "S " before:

var oDoc = app.activeDocument;

//First loop through all stories then through all paragraphs in each story

for(n=0; n< oDoc.stories.length;n++){

    for(i=0;i<oDoc.stories.paragraphs.length;i++){

        // If paragraph style is "Silkeborg" then select paragraph and insert "S " before

        if  (oDoc.stories.paragraphs.appliedParagraphStyle.name == "Silkeborg") {

            oDoc.stories.paragraphs.select();

            app.selection[0].insertionPoints[0].contents = "S ";

            }

        }

    }

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
Advisor ,
Dec 05, 2011 Dec 05, 2011

Copy link to clipboard

Copied

Hi Wiseloc,

This will work, but there is no need to

select paragraph if you want to insert the text.

Replace this two lines:

oDoc.stories.paragraphs.select();

app.selection[0].insertionPoints[0].contents = "S ";

with this one:

oDoc.stories.paragraphs.insertionPoints[0].contents = "S ";

Hope that helps.

--

Marijan (tomaxxi)

http://tomaxxi.com

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
Explorer ,
Feb 15, 2024 Feb 15, 2024

Copy link to clipboard

Copied

Hi! I know this is more than 10 years old but in case anyone's still looking at this I have the same issue and tried to use this code to create a script but got this error:

EubhaLiath_0-1708017804929.png

I'm guessing the term paragraphs is no longer used in scripts? Anyone got any ideas?

 

Thanks!

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 ,
Feb 15, 2024 Feb 15, 2024

Copy link to clipboard

Copied

It should be:

 

for(i=0;i<oDoc.stories[(n)].paragraphs.length;i++){ 

 

And then:

oDoc.stories.paragraphs[(i)].

 

As per @Peter Kahrel next comment - there should be square brackets [ ].

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 15, 2024 Feb 15, 2024

Copy link to clipboard

Copied

You need parentheses, not brackets, and an index is needed on stories as well:

 

oDoc.stories[n].paragraphs[i]

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
Explorer ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

Thanks guys! I'm a bit lost of how to integrate that though - I'm really new to scripting. Could you please show me in the script as a whole? Thanks again!

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 ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

Doing it on my phone:

 

 

var oDoc = app.activeDocument;

//First loop through all stories then through all paragraphs in each story

for(n=0; n< oDoc.stories.length;n++){

    for(i=0;i<oDoc.stories[n].paragraphs.length;i++){

        // If paragraph style is "Silkeborg" then select paragraph and insert "S " before

        if  (oDoc.stories[n].paragraphs[i].appliedParagraphStyle.name == "Silkeborg") {

            oDoc.stories[n].paragraphs[i].insertionPoints[0].contents = "S ";

            }

        }

    }

 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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
Explorer ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

That's working 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 ,
Feb 16, 2024 Feb 16, 2024

Copy link to clipboard

Copied

LATEST
quote

That's working perfectly, thank you so much!!!


By @EubhaLiath

 

You are welcome. 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Dec 05, 2011 Dec 05, 2011

Copy link to clipboard

Copied

Hi wiseloc and Marijan

Waooh.

That was a quick reply.

And it works just the way I wanted. Thanks.

And it has helped my better to understand the javascript syntax and looping.

Kind regards

Jimmy Hartington

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