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. 😞
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? 😞
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
...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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
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?
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.
Copy link to clipboard
Copied
I think I figured it out:
app.findGrepPreferences.findWhat = '^(\\d |.*? )0+(\\d+|-\\d+, )';
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.
Hope this makes sense and helps the next person coming to this thread.
-Manan
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.
Copy link to clipboard
Copied
Thanks for chiming in @Peter Kahrel! I'm not sure what you mean by
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!
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.
Copy link to clipboard
Copied
Ah, I think I understand now. 🙂 Thanks so much for the explanation and solutions!