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

app.selection[0] as variable cause error

Community Beginner ,
Jun 22, 2017 Jun 22, 2017

Copy link to clipboard

Copied

Hello all,

I have here a test function and I don't know why Indesign brings here a error:

function textBlock(myObject) { 
    app.changeGrepPreferences = NothingEnum.nothing; 
    app.findGrepPreferences = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = "\\[(.*)\\]";
    app.changeGrepPreferences.changeTo = "$1";
    numberList = myObject.changeGrep();
}

var selText = app.selection[0];

for (var x = 0; x < selText.paragraphs.length; x++) {
    textBlock(selText.paragraphs.item(x));
}

When I remove the variable: selText and use directly app.selection[0] everything works correct.

Have you an Idea why this is happen? To find this out I spend hours...

In my real script I call a function with:

myProcessText(app.selection[0]);

function myProcessText(selText) {

     // do something ...

}

That is why I want to work with the variable.

TOPICS
Scripting

Views

947

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
Guide ,
Jun 22, 2017 Jun 22, 2017

Copy link to clipboard

Copied

Hi jb,

The problem comes from the fact that the find/change function alters the selection. So your selText variable—which is based on the original selection—becomes invalid as soon as its internal indices no longer match the current text range. In your loop, this likely happens when textBlock returns from its very first call (x==0). You could check my hypothesis using alert(selText.isValid), which I suspect to prompt false.

The above issue seems to magically disappear when you explicitly reuse app.selection[0] rather than selText. That's because app.selection is a command that forces InDesign to recalculate the selection each time the interpreter meets it, so it takes into account changes introduced by the latest find/change.

However this is a terribly time-consuming approach. Since you want to process every paragraph associated to the original text selection, why don't you just pass app.selection[0].paragraphs.everyItem() to your textBlock function? Something like:

function textBlock(tx)

{

    app.changeGrepPreferences = NothingEnum.nothing; 

    app.findGrepPreferences = NothingEnum.nothing;

    app.findGrepPreferences.findWhat = "\\[(.+)\\]";

    app.changeGrepPreferences.changeTo = "$1";

    numberList = tx.changeGrep();

}

var selText = app.selection[0]; // some text selected

textBlock(selText.paragraphs.everyItem());

The key idea is, since the changeGrep method is supported by the Paragraph object, you may call it from a plural paragraph as well.

[There are cases where this trick doesn't perfectly work, depending on the regular expression in use. Find/replace is not 100% safe when working on plural text specifiers.]

@+

Marc

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 ,
Jun 22, 2017 Jun 22, 2017

Copy link to clipboard

Copied

LATEST

Thank you Mark for your very good and helpful explanation! This script snip is part of my very fist indesign script, and yesterday I was getting crazy about this. Because I always thought my paragraph index is not changing, so way I get a invalid object. But now it makes totally sense!

The everyItem commend I didn't know before, I will take a look in that to. But now with your point about time consuming operation I thought more in depth about my script, and many of my grep search/replace functions can work just with the hole selection and not per paragraph. I only need some text formats based on paragraphs. So this I will change.

Regrads

Jonathan

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