Highlighted

Indesign Script: Getting the x-height

Explorer ,
Apr 01, 2020

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?

Adobe Community Professional
Correct answer by Laubender | Adobe Community Professional

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 )

Topics

Scripting

Views

646

Likes

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

Indesign Script: Getting the x-height

Explorer ,
Apr 01, 2020

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?

Adobe Community Professional
Correct answer by Laubender | Adobe Community Professional

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 )

Topics

Scripting

Views

647

Likes

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
Adobe Community Professional ,
Apr 01, 2020

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 )

Likes

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
Reply
Loading...
Advocate ,
Apr 01, 2020

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

Likes

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
Reply
Loading...
Adobe Community Professional ,
Apr 01, 2020

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 )

Likes

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
Reply
Loading...
Adobe Community Professional ,
Apr 01, 2020

Copy link to clipboard

Copied

Or any character that is not lower case—the OP wants the x-height.

 

Screen Shot 20.png

 

 

 

Screen Shot 21.png

 

The x-height

Screen Shot 22.png

Likes

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
Reply
Loading...
Most Valuable Participant ,
Apr 02, 2020

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.

Likes

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
Reply
Loading...
Explorer ,
Apr 01, 2020

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

Likes

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
Reply
Loading...
Adobe Community Professional ,
Apr 01, 2020

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

Likes

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
Reply
Loading...
Explorer ,
Apr 01, 2020

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!

Likes

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
Reply
Loading...
Adobe Community Professional ,
Apr 02, 2020

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 )

Likes

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
Reply
Loading...