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

Get parent (paragraph) of text

New Here ,
Sep 28, 2016 Sep 28, 2016

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

TOPICS
Scripting

Views

570

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

Advisor , Sep 28, 2016 Sep 28, 2016

contentToChange = app.selection[0].paragraphs[0]

is what you are looking for

Votes

Translate

Translate
Engaged ,
Sep 28, 2016 Sep 28, 2016

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.

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 ,
Sep 28, 2016 Sep 28, 2016

Copy link to clipboard

Copied

LATEST

contentToChange = app.selection[0].paragraphs[0]

is what you are looking for

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