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

Indesign Script: Getting the x-height

Explorer ,
Apr 01, 2020 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?

TOPICS
Scripting

Views

1.7K

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 , Apr 01, 2020 Apr 01, 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.pare
...

Votes

Translate

Translate
Community Expert ,
Apr 01, 2020 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 )

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
Guide ,
Apr 01, 2020 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

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 ,
Apr 01, 2020 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 )

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

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 ,
Apr 02, 2020 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.

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

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

David Creamer: Community Expert (ACI and ACE 1995-2023)

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
Explorer ,
Apr 01, 2020 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!

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 ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

LATEST

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 )

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