Why does my selection shrink when I run a script on it?

Explorer ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

I'm running a script on a selection of text. This is the code I'm running: 

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '^((\\d |).*? )([0]{1})([^0]{1}[0-9](-\\d+|),  )';
app.changeGrepPreferences.changeTo = '$1$4';
app.selection[0].changeGrep();

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '^((\\d |).*? )([0]{2})([0-9](-\\d+|),  )';
app.changeGrepPreferences.changeTo = '$1$4';
app.selection[0].changeGrep();

(It's finding Scripture references and removing the padding zeroes that were needed for proper sorting.)

But the problem is that as the extra "00" characters are removed, they seem to be "shortening" my selection so that when the script gets near the end, it skips the last few lines. 😞 

Screenshot 2022-03-03 185205.png

As you can see, the selection has shrunk, so that it completely missed "Revelation 021"... When I first ran the script, all of the text was selected, but when the script ended, those lines were deselected, and the script didn't remove the zeros from in front of the 21 and 22 like it was supposed to. Why is this happening? And how do I fix it? 😞 

TOPICS
Scripting

Views

208

Likes

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

Adobe Community Professional , Mar 06, 2022 Mar 06, 2022
This is what happens: when you select the text, those two Revelations aren't visible (what an apt book name for remaining invisible!). Then you run your script against the selection, which deletes leading zeros, which causes some line breaks to disappear, and the Revelations are, well, revealed. To apply your script to all books, target the whole story that the selection is part of: app.selection[0].parentStory.changeGrep(); Your expressions seem overly complex. Wouldn't it be enough simply to...

Likes

Translate

Translate
Adobe Community Professional ,
Mar 03, 2022 Mar 03, 2022

Copy link to clipboard

Copied

Hi @23121922udmu,

I tried but could not replicate the issue. Would it be possible for you to share a sample document that manifests this issue.

-Manan

Likes

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
Explorer ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

Thanks for looking into this for me! Attached is an indd doc with the exact text I'm working with. If you select all the text and run the script, it quits without affecting the last 2 lines.

Likes

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
Explorer ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

This is my script (saved as a text file). 

Likes

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
Adobe Community Professional ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

 I think the issue is with your regular expression. You are searching for two 0 with the expression 0{2}, the last two lines which does seem to be out of selection after the changegrep do not meet the criteria(have only a single preceding 0) and hence these lines don't get updated. Try the following it seems to work for me

 

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '^(\\d |.*? )0+(\\d+,  )';
app.changeGrepPreferences.changeTo = '$1$2';
var a = app.selection[0].getElements()[0]
a.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;

 

I hope the problem is to just remove the preceding 0 be it any number of 0's.

-Manan

Likes

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
Explorer ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

Well, I have 2 different expressions: one for removing one 0, and one for removing two 00. My original post with the code block contains both of them. Somehow I missed the second one in my attached script though. 😞 Thanks for the suggestion though, I think that will work better! I'm a complete novice with GREP and jsx, so I'm definitely open to learning how to more efficiently write these scripts. 🙂

 

Your code seems to work wonderfully, except that it's missing my OR statements (which are needed to catch references like 'Nehemiah 1-2', and references with numbers preceding them, like '1 John', '2 John', etc). What is the best way to write an OR statement like that?

Likes

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
Explorer ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

Oops, I realize on looking closer at your code that you did include the first OR statement. Sorry! The second one is still missing though. 

Likes

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
Explorer ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

I think I figured it out: 

app.findGrepPreferences.findWhat = '^(\\d |.*? )0+(\\d+|-\\d+,  )';

Likes

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
Adobe Community Professional ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

Well now that you have verified they script, and have added the missing parts to your grep expression. We can summarise the key points as below.

  • Using the selection object for consecutive change queries could cause an issue as the selected text length may be altered by a change query and hence the query following it would not have the complete text to work on. This can be handled in the following ways
    • Using some other object instead of selection to run our change query, like textframe. This way we always target the complete text of the textframe and should be shielded from the side effects of previous change queries.
    • Combine multiple change queries into one, as we have done with our approach.

Hope this makes sense and helps the next person coming to this thread.

-Manan

Likes

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
Adobe Community Professional ,
Mar 06, 2022 Mar 06, 2022

Copy link to clipboard

Copied

This is what happens: when you select the text, those two Revelations aren't visible (what an apt book name for remaining invisible!). Then you run your script against the selection, which deletes leading zeros, which causes some line breaks to disappear, and the Revelations are, well, revealed.

 

To apply your script to all books, target the whole story that the selection is part of:

app.selection[0].parentStory.changeGrep();

Your expressions seem overly complex. Wouldn't it be enough simply to delete all leading zeros? If that's ok, these expressions will do:

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\b0+';
app.selection[0].parentStory.changeGrep();

P.

Likes

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
Explorer ,
Mar 14, 2022 Mar 14, 2022

Copy link to clipboard

Copied

Thanks for chiming in @Peter Kahrel! I'm not sure what you mean by

quote

when you select the text, those two Revelations aren't visible


By @Peter Kahrel


 Are you referring to overset text? Or just that it's "invisible" in some way to the script? If I could wrap my head around why the script didn't work properly, then maybe I'll have a better understanding of how it should work properly. 🙂 

Thank you so much for your suggestions, that's definitely a better way of doing things! I appreciate your time and expertise to help me with my little problem!

Likes

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
Adobe Community Professional ,
Mar 14, 2022 Mar 14, 2022

Copy link to clipboard

Copied

Suppose you have a text that spans four text frames. When you select a text frame and target app.selection[0], you target only the text you see in that selected text frame. On the other hand, when you select a frame and target app.selection[0].parentStory, you target the text in all the threaded frames.

 

By "those two Revelations aren't visible" I meant that they were probably in the second text frame. So when you selected the first text frame your script saw just the text in the first, selected, frame, nothing else. Then you ran your script, which made the text in the first frame run shorter, which in turn pulled those two revelations back into the first frame.

 

By targetting the selected frame's parent story you avoid those problems.

 

P.

 

Likes

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
Explorer ,
Mar 14, 2022 Mar 14, 2022

Copy link to clipboard

Copied

LATEST

Ah, I think I understand now. 🙂 Thanks so much for the explanation and solutions! 

Likes

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