InDesign position of a text character relative to its text frame edge

I am not optimistic about this request and I do not know how to search for it, but here it goes.

Is there a way to get a text character’s relative position to it’s parent text frame’s left edge?

You may ask why? Reason being, we use XML tags and it’s not uncommon for new text to flow into a text frame containing a text string with an xml markup tag. If there is not adequate space to allow for an additional character then it could result in an overset text frame.

So I am basically looking for a way to warn an operator in such an instance, “The word (“You.”) is too close to it’s parent left frame edge”.

Check out horizontal offset.

Hi. A character’s horizontal offset is its parent’s distance from the leftmost position, and its end horizontal offset is its own. This is not a good measure of adequate space for additional text, which flows to the right and overflows at the bottom.

If you have the horizontal offset, plus the bounds of the frame its in, calculating the space available to the right is trivial.

Shane, you are the best! Horizontal offset is what I was looking for. Yes, I did use the wrong edge in my post, but as you mentioned, calculating the difference will be simple.

Thanks again,
-Jeff

I agree that calculating the absolute space from the edges is trivial, but, because every character has unique dimensions, the available letter space can’t accurately be calculated, until the text is realized. You could test for overflows and correct that with minor tracking, leading, or frame adjustments.

Paragraph-level justification can also come into play. But it can be used to provide a reasonable guide.

Always a good idea.

I currently test for overflows, and it’s a lifesaver. But unfortunately the overflows in this particular instance appear after the layout leaves the operator.

Yes, text size would typically be a great deal of concern. But luckily the text size that yields overflows in my instance is typically no greater than 10 pts. And the overflow culprit is the result of adding either one and no more than two characters. Therefore I can use some simple logic based on the current font size. such as if font size is 5, 6, 7, 8, 9, 10, then use applicable values as the acceptable distance required to allow for two more characters using a wide font as a guide, say “Black Oak”. (I may even test for various fonts in a list that are wider than most and apply unique logic.) I will also apply logic to further fine tune the required threshold distance, checking the font’s horizontal scaling. If 120 - 200% then subtract from the necessary required space required before generating a warning. (Tracking is not a concern and next to never applied.)

I probably should have specified that it’s a date range I am working with. 7/7/16 may later become 12/30/16 as the result of the XML feed. (But never 12/30/2016).

Presently I am displaying a warning whenever a short date range is detected, i.e., 7/7/16. For example, “The date range is missing digits and has a potential to overset.”
Unfortunately I can’t have operators add preceding zeros due to aesthetics. In any event, as you can see these short digit date ranges can be common and operators bypass the warning because the frame usually has room and they “assume” the best. My goal is to avoid a warning message when ample character space is available for these short date ranges. If the warning is less common it should alert the operator because it will catch them off guard, “Hmm, I haven’t seen this warning in awhile, I better check it out.”

Justification is a good point. It’s rare in my instances to have right justification applied. But to be safe I will display a warning regardless of any offset math, since right justification of a date range is not allowed.

Even if my code doesn’t work, I will not consider it all for nothing. Every time I dabble in ApplesScript something I learn something new that may be applicable in the future. So it will be fun nonetheless.

Many thanks as always. Best forum on the Internet!

Actually there was no need for me to look at font sizes and scaling of text because I could simply divide the width of the selection by the amount of characters within it (with a slight buffer). I’m fortunate that I can limit this script to only execute on selections that contain either 7 or 8 characters: “7/7/16” or “7/30/16”

This script is working nicely:

tell application "Adobe InDesign CS4"
	--get the character count in the selected text
	set the characterCount to count of characters in selection
	
	if characterCount is less than 8 then
		set lastInsertionPoint to object reference of (insertion point -1) of selection
		set theFirstInsertionPoint to object reference of (insertion point 1) of selection
		set thePos to horizontal offset of lastInsertionPoint
		set theFirstPos to horizontal offset of theFirstInsertionPoint
		
		set {a, b, c, d} to geometric bounds of item 1 of parent text frames of selection
		
		-- Space from right frame's edge
		set mylastLoc to d - thePos
		
		
		set myFirstLoc to d - theFirstPos
		
		
		set theSelectionWidth to (myFirstLoc - mylastLoc)
		
		--Subtracting 2 because "/" in dates ranges such as 7/7/16 don't account for much character width
		set theNewDigits to characterCount - 2
		
		--Divide theSelectionWidth by theNew Digit number to get the width of a character
		
		set myCharacterWidth to theSelectionWidth / theNewDigits
		
		if characterCount is 6 then
			--multiply charcter width by 2 to get the necessary width to allow for 2 additional characters if the date range is 7 digits
			set necessarySpace to myCharacterWidth * 2
		else if characterCount is 7 then
			--use the width of just 1 character to determine the required with to the right of the selection
			set necessarySpace to myCharacterWidth
		end if
		
		
		display dialog necessarySpace & " I am the necessary character width of two characters required to the right of the selection" as string
		
		if necessarySpace is greater than mylastLoc then
			display dialog "Not enough space to the right of the selection"
		end if
	end if
	
	
end tell