Copy link to clipboard
Copied
Hi all,
I ran into a problem and can't seem to find the solution myself. It is like this:
Let's say you have a story with two paragraphs. One paragraph has 'hello' in it and the other paragraph has 'world' in it as text.
You can select for example 'hello', then click a button and then after you click the button it has to change to for example 'greetings'.
So then you have one paragraph with 'greetings' and the other still with 'world'.
Now I have the following code which I need to change:
var selectedContent = app.selection[0].contents; // this has the selection, 'hello'.
var contentToChange = app.selection[0].parent; // this has the story.
I want contentToChange to be the paragraph in which the text resides.
.parent gets the story, so everything disappears and becomes just 'greetings'.
Tried many things, but can't find the solution (what I should use instead of 'parent' to get the paragraph).
Can someone help please, thanks in advance,
Greetings
contentToChange = app.selection[0].paragraphs[0]
is what you are looking for
Copy link to clipboard
Copied
You could loop through each paragraph to check if a word in that paragraph is selected:
var sel = app.selection[0];
var para = sel.parentStory.paragraphs;
for(p=0;p<para.length;p++){
var words = para
.words;
for(w=0;w<words.length;w++){
if(app.selection == words
){ alert(para
.contents);
}
}
}
Note: This will only give the paragraph If a single word is selected. If a space or more than one word, {defined as "maximal range of characters that do not own any word separator (like space, tab, etc.)" at Indiscripts :: What Exactly is a Word?​,} then the script will not pick up the paragraph.
Copy link to clipboard
Copied
contentToChange = app.selection[0].paragraphs[0]
is what you are looking for