Skip to main content
Known Participant
April 1, 2020
Answered

Indesign Script: Getting the x-height

  • April 1, 2020
  • 4 replies
  • 2481 views

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?

This topic has been closed for replies.
Correct answer Laubender

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 )

4 replies

Community Expert
April 2, 2020

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 )

Known Participant
April 2, 2020

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!

Dave Creamer of IDEAS
Community Expert
Community Expert
April 1, 2020

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

David Creamer: Community Expert (ACI and ACE 1995-2023)
LaubenderCommunity ExpertCorrect answer
Community Expert
April 1, 2020

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 )

FRIdNGE
April 1, 2020

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

Community Expert
April 1, 2020

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 )