Copy link to clipboard
Copied
Hi.
I am trying to read the x-height of a selected character using InDesign script.
I can't seem to find the property.
Do you have any sugesstions?
Hi rasmussvanejensen,
you could do the following:
( function()
{
var testContents = "x";
if( app.selection.length != 1 ){ return };
if( !app.selection[0].hasOwnProperty( "baselineShift" ) ){ return };
if( app.selection[0].insertionPoints.length < 2 ){ return };
var doc = app.documents[0];
var character = app.selection[0].characters[0];
var tempFrame = doc.textFrames.add({ geometricBounds : doc.pages[0].bounds });
character.duplicate( LocationOptions.AT_BEGINNING , tempFrame.pare
...
Copy link to clipboard
Copied
Hi rasmussvanejensen,
you could do the following:
( function()
{
var testContents = "x";
if( app.selection.length != 1 ){ return };
if( !app.selection[0].hasOwnProperty( "baselineShift" ) ){ return };
if( app.selection[0].insertionPoints.length < 2 ){ return };
var doc = app.documents[0];
var character = app.selection[0].characters[0];
var tempFrame = doc.textFrames.add({ geometricBounds : doc.pages[0].bounds });
character.duplicate( LocationOptions.AT_BEGINNING , tempFrame.parentStory );
tempFrame.parentStory.contents = testContents ;
var outlinedX = tempFrame.characters[0].createOutlines( false )[0];
var gB = outlinedX.geometricBounds;
var height = gB[2] - gB[0] ;
alert( "Height "+testContents+": "+ height );
outlinedX.remove();
tempFrame.remove();
}() )
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe,
It was a long time! The Jedi is back.
What is the interest of the "tempFrame" text frame here?
Supposing such a script will be only used with a text selection, this could be enough:
var myOutline = app.selection[0].createOutlines(false)[0],
myGB = myOutline.geometricBounds,
mySelectedTextHeight = myGB[2] - myGB[0];
myOutline.remove();
alert( "Height of Selected Text:\r" + mySelectedTextHeight )
(^/) The Jedi
Copy link to clipboard
Copied
Hi Michel,
the user could have selected a white space character.
So I do not want to work with the selected text directly.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Or any character that is not lower case—the OP wants the x-height.
The x-height
Copy link to clipboard
Copied
No suggestions using FirstBaseline.X_HEIGHT on a text frame? Then it does not depend on the character in the selection.
Copy link to clipboard
Copied
Could you explain to me what this check does? I am not sure why this makes a difference for the result.
if( !app.selection[0].hasOwnProperty( "baselineShift" ) ){ return }
Thanks again
Copy link to clipboard
Copied
Here is an article with a link to a script for settting the x-height. It's old so I don't know if it works on newer ID versions.
https://indesignsecrets.com/set-the-size-of-text-exactly-based-on-cap-or-x-height.php
Copy link to clipboard
Copied
Hi Uwe! That works perfectly!
I had started to do write something simulare where I outlined and measured the character, but I wasn't close to doing it in an elegant way.
Thank you. It was a big help!
Copy link to clipboard
Copied
Hi Jongware,
very good suggestion.
Something like that:
( function()
{
if( app.selection.length != 1 ){ return };
if( !app.selection[0].hasOwnProperty( "baselineShift" ) ){ return };
if( app.selection[0].insertionPoints.length < 2 ){ return };
var doc = app.documents[0];
var character = app.selection[0].characters[0];
var tempFrame = doc.textFrames.add({ geometricBounds : doc.pages[0].bounds });
character.duplicate( LocationOptions.AT_BEGINNING , tempFrame.parentStory );
tempFrame.textFramePreferences.properties =
{
firstBaselineOffset : FirstBaseline.X_HEIGHT ,
minimumFirstBaselineOffset : 0 ,
insetSpacing : 0 ,
ignoreWrap : true ,
verticalJustification : VerticalJustification.TOP_ALIGN
};
tempFrame.texts[0].alignToBaseline = false ;
var height = tempFrame.insertionPoints[0].baseline - tempFrame.geometricBounds[0] ;
alert( "x-Height of selected character: "+ height );
tempFrame.remove();
}() )
Regards,
Uwe Laubender
( ACP )