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.
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
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 ";
}
...
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?
Copy link to clipboard
Copied
Hi Wiseloc
Yes, that could work. Did not think of that.
Thanks.
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?
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.
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
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.
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 ";
}
}
}
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)
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:
I'm guessing the term paragraphs is no longer used in scripts? Anyone got any ideas?
Thanks!
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 [ ].
Copy link to clipboard
Copied
You need parentheses, not brackets, and an index is needed on stories as well:
oDoc.stories[n].paragraphs[i]
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!
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 ";
}
}
}
Copy link to clipboard
Copied
That's working perfectly, thank you so much!!!
Copy link to clipboard
Copied
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
Find more inspiration, events, and resources on the new Adobe Community
Explore Now