Optical Kerning InDesign (Based on Selected frames)

I know what I have is not correct. Surely there must be simple method to covert all paragraph text to optical kerning within a “selection” or the parent frame of selected text or insertion point.

The problem I am facing is I eventually want my script to work on any selected objects. This would include a selection that contains the combination of grouped objects, a few single text frames, polygons, etc. But I also would like it to work on a text frame that only contains an insertion point where it then applies optical kerning to the “entire” parent story?

Can anybody assist me with this?

tell application "Adobe InDesign CS4"
	activate
	set aDoc to active document
	tell aDoc
		set myText to object reference of every paragraph of every story of selection
		set myText to paragraph i of the selection
		set kerning method of myText to "Optical"
	end tell
end tell

Hi, Jeff. All InDesign text descends from stories, so there is no need to reference paragraphs. Text can also be changed document-wide, sans selection, via the story object. If you just really want a selection, then target the parent story.


tell application "Adobe InDesign CS3"'s document 1
	if selection is not {} then try
		set selection's parent story's kerning method to "Optical"
	on error
		if selection's class is group then set selection's text frames's parent story's kerning method to "Optical"
	end try
end tell

Thank you Marc,
I appreciate the help and explanation. But I still struggle when the selected objects become “complicated”. For example, assume I have two text frames that are grouped, and then another group of two text frames, and then one regular text frame. All three objects are then selected. My goal is to have all text of each text from to be converted to optical kerning.

Presently your code is only working if the selected object is a single group or a basic text frame.

Does this make sense?

I was hoping I didn’t have to set up a bunch of repeat loops to get around this dilemma? Do you know what I mean?

Thanks,
-Jeff

This is the best I could do. It only works on one group that contains sub groups. What I cannot figure out is how to have it work on a selection that might have a selection such as:

Selection = ( group & group ), whereas each group might have sub groups of text frames

Selection = (group & single text frame), whereas the group might have sub groups.

on myGroup(itemx)
	try
		tell application "Adobe InDesign CS4"'s document 1
			try
				repeat with i from 1 to count of text frames in itemx
					set itemx's text frame i's parent story's kerning method to "Optical"
				end repeat
			on error
				repeat with i from 1 to count of groups in itemx
					set itemx to group i of itemx
					my myGroup(itemx)
				end repeat
			end try
		end tell
	end try
end myGroup


tell application "Adobe InDesign CS4"'s document 1
	if selection is not {} then try
		set selection's parent story's kerning method to "Optical"
	on error
		try
			if selection's class is group then set selection's text frames's parent story's kerning method to "Optical"
		on error
			repeat with i from 1 to count of groups in selection
				set itemx to group i of selection
				my myGroup(itemx)
			end repeat
		end try
	end try
end tell

Obviously, I don’t know how to use subroutines correctly, because no matter what I attempt I cannot get it to convert all the text frames to optical kerning when there are multiple groups and text frames as part of the selection :frowning:

The easiest way to get the text frames in a group is like this:

every item of all page items of someGroup whose class is text frame

You probably need to wrap it in a try block.

I only anticipated one group and some loose items, rather than grouped groups.

Edit: The original code for this post had an errant if consideration. This version works for single items, groups with single items, and grouped groups.

tell application "Adobe InDesign CS3"'s document 1
	if selection is not {} then repeat with anItem in selection
		tell anItem to try
			set parent story's kerning method to "Optical" --loose text frames
		on error
			if its class is group then
				try
					set all page items's text frames's parent story's kerning method to "Optical" --grouped groups
				end try
				try
					set text frames's parent story's kerning method to "Optical" --singular groups
				end try
			end if
		end try
	end repeat
end tell

I am extremely appreciative of all your help as always. This simple script was more daunting for me than some of the seemingly more involved scripts. The attached script appears to work in all instances. It required the need to loop in each item of of each item of selection as well.

Thanks once again,
-Jeff

tell application "Adobe InDesign CS4"'s document 1
	if selection is not {} then repeat with anItem in selection
		tell anItem to try
			set parent story's kerning method to "Optical"
		on error
			try
				set all page items's text frames's parent story's kerning method to "Optical"
			end try
			repeat with i from 1 to count of items in anItem
				set newItem to item i of anItem
				try
					set newItem's text frames's parent story's kerning method to "Optical"
				end try
			end repeat
		end try
	end repeat
end tell