Fractions Script-works but would like to simplify

This works how I want it to pretty much perfectly, but it has so many essentially repetitive statements in it. I am wondering if someone can help me simplify it some what. One idea I had was to set selection ranges, but I could not get it to work well. Thanks for any suggestions.


tell application "Adobe InDesign CS2"
	set foundText to {}
	set replaceText to {}
	tell document 1
		set Numerator to make character style with properties {name:"numerator", position:superscript, fill color:"Black"}
		set Denominator to make character style with properties {name:"denominator", horizontal scale:58.3, vertical scale:58.3, fill color:"Black"}
		set TENSfr to search for "-^9/^9"
		set HUNSfr to search for "-^9^9/^9^9"
		set THOUfr to search for "-^9^9^9/^9^9^9"
		set TENSoverHuns to search for "-^9/^9^9"
		set TENSoverTHOU to search for "-^9/^9^9^9"
		set HUNSoverTHOU to search for "-^9^9/^9^9^9"
	end tell
	try
		repeat with i from 1 to count of TENSfr
			set applied character style of character 2 of item i of TENSfr to "numerator"
			set applied character style of character 4 of item i of TENSfr to "denominator"
		end repeat
	end try
	try
		repeat with i from 1 to count of HUNSfr
			set applied character style of character 2 of item i of HUNSfr to "numerator"
			set applied character style of character 3 of item i of HUNSfr to "numerator"
			set applied character style of character 5 of item i of HUNSfr to "denominator"
			set applied character style of character 6 of item i of HUNSfr to "denominator"
		end repeat
	end try
	try
		repeat with i from 1 to count of THOUfr
			set applied character style of character 2 of item i of THOUfr to "numerator"
			set applied character style of character 3 of item i of THOUfr to "numerator"
			set applied character style of character 4 of item i of THOUfr to "numerator"
			set applied character style of character 6 of item i of THOUfr to "denominator"
			set applied character style of character 7 of item i of THOUfr to "denominator"
			set applied character style of character 8 of item i of THOUfr to "denominator"
		end repeat
	end try
	try
		repeat with i from 1 to count of THOUfr
			set applied character style of character 2 of item i of THOUfr to "numerator"
			set applied character style of character 3 of item i of THOUfr to "numerator"
			set applied character style of character 4 of item i of THOUfr to "numerator"
			set applied character style of character 6 of item i of THOUfr to "denominator"
			set applied character style of character 7 of item i of THOUfr to "denominator"
			set applied character style of character 8 of item i of THOUfr to "denominator"
		end repeat
	end try
	try
		repeat with i from 1 to count of TENSoverHuns
			set applied character style of character 2 of item i of TENSoverHuns to "numerator"
			set applied character style of character 4 of item i of TENSoverHuns to "denominator"
			set applied character style of character 5 of item i of TENSoverHuns to "denominator"
		end repeat
	end try
	try
		repeat with i from 1 to count of TENSoverTHOU
			set applied character style of character 2 of item i of TENSoverTHOU to "numerator"
			set applied character style of character 4 of item i of TENSoverTHOU to "denominator"
			set applied character style of character 5 of item i of TENSoverTHOU to "denominator"
			set applied character style of character 6 of item i of TENSoverTHOU to "denominator"
		end repeat
	end try
	try
		repeat with i from 1 to count of HUNSoverTHOU
			set applied character style of character 2 of item i of HUNSoverTHOU to "numerator"
			set applied character style of character 3 of item i of HUNSoverTHOU to "numerator"
			set applied character style of character 5 of item i of HUNSoverTHOU to "denominator"
			set applied character style of character 6 of item i of HUNSoverTHOU to "denominator"
			set applied character style of character 7 of item i of HUNSoverTHOU to "denominator"
		end repeat
	end try
end tell

this is the first reduction which I am sure I can reduce at a more global level. The thru statement works when applied properly. I have also added some character mods.


try
			try
		repeat with i from 1 to count of HUNSfr
			set applied character style of characters 2 thru 3 of item i of HUNSfr to "numerator"
			set applied character style of characters 5 thru 6 of item i of HUNSfr to "denominator"
		end repeat
	end try

	end try

Hi,

here is an attempt to shorten the script a bit.
The THOUfr-part appears twice in your script? consciously ? I skipped one
I haven’t tested it so I hope I got all parameters right :wink:

tell application "Adobe InDesign CS2"
	set foundText to {}
	set replaceText to {}
	tell document 1
		set Numerator to make character style with properties {name:"numerator", position:superscript, fill color:"Black"}
		set Denominator to make character style with properties {name:"denominator", horizontal scale:58.3, vertical scale:58.3, fill color:"Black"}
		set TENSfr to search for "-^9/^9"
		set HUNSfr to search for "-^9^9/^9^9"
		set THOUfr to search for "-^9^9^9/^9^9^9"
		set TENSoverHuns to search for "-^9/^9^9"
		set TENSoverTHOU to search for "-^9/^9^9^9"
		set HUNSoverTHOU to search for "-^9^9/^9^9^9"
	end tell
end tell

apply_char_style(2, 2, 4, 4, TENSfr)
apply_char_style(2, 3, 5, 6, HUNSfr)
apply_char_style(2, 4, 6, 8, THOUfr)
apply_char_style(2, 2, 4, 5, TENSoverHuns)
apply_char_style(2, 2, 4, 6, TENSoverTHOU)
apply_char_style(2, 3, 5, 7, HUNSoverTHOU)

on apply_char_style(numFrom, numTo, denomFrom, denomTo, what)
	try
		repeat with i in what
			tell application "Adobe InDesign CS2"
				set applied character style of characters numFrom thru numTo of contents of i to "numerator"
				set applied character style of characters denomFrom thru denomTo of contents of i to "denominator"
			end tell
		end repeat
	end try
end apply_char_style

OK so this is taking an approach that I am not yet familiar with. I basically understand the concept, however it is not quite working as is. I am going to play with it a bit to see this would be way more efficient.

Thanks

There’s a fractions script that works quite well in Code Exchange by Qwerty Denzel

The other scripts are a bit more robust than what I was doing, and I really am not sure how I would implement them in InDesign. I probably could somehow apply my search variables directly into the mix. I like the reduction to LCD, something that would be a welcome addition.