Skip to main content
Jimmy Hartington
Participant
December 2, 2011
Answered

Insert character in front of text set with certain paragraph style

  • December 2, 2011
  • 3 replies
  • 3518 views

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.

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

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!


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 ";

            }

        }

    }

 

 

3 replies

csm_phil
Legend
December 3, 2011

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

Jimmy Hartington
Participant
December 5, 2011

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.

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
February 16, 2024

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!


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 ";

            }

        }

    }

 

 

John Hawkinson
Inspiring
December 2, 2011

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?

Jimmy Hartington
Participant
December 5, 2011

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.

Inspiring
December 2, 2011

Hi Jimmy,

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

Jimmy Hartington
Participant
December 5, 2011

Hi Wiseloc

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

Thanks.