Copy link to clipboard
Copied
In my current projects many paragraphs end with a right flushed cross-reference. I'm using the shift+tab to separate the text from the cross reference and to keep it to the right. As the basic formatting I added two shift+tabs because otherwise the main paragraph would sometimes be justified even on the last line. So now I'm making a script to loop through it all and remove that extra tab where it is not needed. In the screenshot the selected two tabs should be replaced with one, but the reference to the top left needs two tabs (because of the justified text alignment).
Is there a way to get the width of a character? If there is I could easily determine where two tabs are needed and where I should remove one. Right now I have a script that makes an estimation based on the number of characters in each line. Its decent but requires a lot of manual confirmation. Any suggestions?
You can measure the difference between the beginning and the end of the found items. Assuming that you've been looking for those double tabs, do this:
// Assumes a myFound array of found sequences of two tabs
start = myFound.insertionPoints[0].horizontalOffset;
end = myFound.insertionPoints[-1].horizontalOffset;
distance = end - start;
if (distance > [some value]) {
// replace the two tabs with one
}
Peter
Copy link to clipboard
Copied
You can measure the difference between the beginning and the end of the found items. Assuming that you've been looking for those double tabs, do this:
// Assumes a myFound array of found sequences of two tabs
start = myFound.insertionPoints[0].horizontalOffset;
end = myFound.insertionPoints[-1].horizontalOffset;
distance = end - start;
if (distance > [some value]) {
// replace the two tabs with one
}
Peter
Copy link to clipboard
Copied
Hi Peter,
Clever!
Editors like to make corrections on text! Layout changes could trigger a real mess if no precaution! …
app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Double-Tab To One! …");
function main()
{
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "~y~y";
myFound = myDoc.findGrep();
var F = myFound.length;
for ( f = F-1; f >= 0; f-- ) {
start = myFound
.insertionPoints[0].horizontalOffset; end = myFound
.insertionPoints[-1].horizontalOffset; distance = end - start;
distance > 10 && myFound
.contents = 1397909876; }
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<!~y)~y(?!~y)";
myFound = myDoc.findGrep();
var F = myFound.length;
for ( f = F-1; f >= 0; f-- ) {
start = myFound
.insertionPoints[0].horizontalOffset; end = myFound
.insertionPoints[-1].horizontalOffset; distance = end - start;
distance < 0 && myFound
.insertionPoints[-1].contents = 1397909876; }
app.findGrepPreferences = app.changeGrepPreferences = null;
}
(^/)
Copy link to clipboard
Copied
Sorry for my late response! This is exactly what I need!
Copy link to clipboard
Copied
See also this thread.
— Kas
Copy link to clipboard
Copied
Thanks - I actually searched for this issue but didn't find that thread. I noticed that in my case I must use insertionPoints instead of characters because the tab I measure is just one character - but has two insertionPoints.
Copy link to clipboard
Copied
> the tab I measure is just one character - but has two insertionPoints.
All characters have two insertion points.
Copy link to clipboard
Copied
pkahrel wrote
> the tab I measure is just one character - but has two insertionPoints.
All characters have two insertion points.
To expand on that: the first insertionpoint is with the cursor to the left, the second with the cursor to the right. That way, you can use 'aCharacter.insertionPoints[0].contents = "(";' and aCharacter.insertionPoints[1].contents = ")";' to add something either to the left or the right of any character, which would otherwise not be possible.