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

Get physical width of character

Participant ,
Mar 10, 2017 Mar 10, 2017

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?

tab-width.jpg

TOPICS
Scripting

Views

932

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

Community Expert , Mar 10, 2017 Mar 10, 2017

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

Votes

Translate

Translate
Community Expert ,
Mar 10, 2017 Mar 10, 2017

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

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
LEGEND ,
Mar 11, 2017 Mar 11, 2017

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;

    }

(^/) 

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
Participant ,
Mar 14, 2017 Mar 14, 2017

Copy link to clipboard

Copied

Sorry for my late response! This is exactly what I need!

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
Guru ,
Mar 12, 2017 Mar 12, 2017

Copy link to clipboard

Copied

See also this thread.

— Kas

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
Participant ,
Mar 14, 2017 Mar 14, 2017

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.

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 Expert ,
Mar 14, 2017 Mar 14, 2017

Copy link to clipboard

Copied

> the tab I measure is just one character - but has two insertionPoints.

All characters have two insertion points.

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 Expert ,
Mar 14, 2017 Mar 14, 2017

Copy link to clipboard

Copied

LATEST

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.

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