Need suggestions for code/speed improvement

Hi,
I have some quick code I tossed together for doing transliteration in Word. And, yes, I don’t know what I’m doing. The original document has mixed fonts. One font was created before unicode and assigns IPA glyphs to the lower 128 ascii range within the font. The goal of this script is to replace all of the ‘hijacked’ ascii characters with the appropriate Unicode characters…but only for that particular font. So, I need to find characters with that font assigned and then transliterate the original ascii code to the correct Unicode codepoint. This script works, but is slow as molasses. I reviewed the various examples of search/replace on the web, but haven’t found any where I can combine the id with the font name for searching…and then replace the character and font name. Any ideas are welcome!

tell application “Microsoft Word”
activate

tell active document
	set myChars to the characters of the text object of the active document
	
	set orig_acute_lc to 98 as integer
	set orig_Acute_uc to 66
	set orig_Xdot_uc to 68
	set orig_xdot_lc to 100
	set orig_Lamdaglottalized_uc to 70
	set orig_lamdaglottalized_lc to 102
	set orig_Glottalstop_uc to 71
	set orig_glottalstop_lc to 103
	set orig_Glottalized_uc to 74
	set orig_glottalized_lc to 106
	set orig_Wsuperior_uc to 82
	set orig_wsuperior_lc to 114
	set orig_Caroncmb_uc to 86
	set orig_caroncmb_lc to 118
	set orig_Lbarred_uc to 90
	set orig_lbarred_lc to 122
	set orig_grave_lc to 96
	
	set charCount to (the count of characters of myChars)
	
	repeat with pos from 1 to charCount
		set myChar to character pos of myChars
		set myVal to the content of myChar
		set myCode to my getCode(myVal)
		if the name of the font object of myChar is "BadFont" then
			set the name of the font object of myChar to "Arial Unicode"
			
			if myCode = orig_acute_lc then
				set the content of myChar to my getUnicode("acute")
			else if myCode = orig_Acute_uc then
				set the content of myChar to my getUnicode("Acute")
			else if myCode = orig_Xdot_uc then
				set the content of myChar to "X" & my getUnicode("dotbelow")
			else if myCode = orig_xdot_lc then
				set the content of myChar to "x" & my getUnicode("dotbelow")
			else if myCode = orig_Lamdaglottalized_uc then
				set the content of myChar to my getUnicode("Lamdabarred") & my getUnicode("glottal")
			else if myCode = orig_lamdaglottalized_lc then
				set the content of myChar to my getUnicode("lamdabarred") & my getUnicode("glottal")
			else if myCode = orig_Glottalstop_uc then
				set the content of myChar to my getUnicode("glottalstop")
			else if myCode = orig_glottalstop_lc then
				set the content of myChar to my getUnicode("glottalstop")
			else if myCode = orig_Glottalized_uc then
				set the content of myChar to my getUnicode("glottal")
			else if myCode = orig_glottalized_lc then
				set the content of myChar to my getUnicode("glottal")
			else if myCode = orig_Wsuperior_uc then
				set the content of myChar to my getUnicode("Wsuperior")
			else if myCode = orig_wsuperior_lc then
				set the content of myChar to my getUnicode("wsuperior")
			else if myCode = orig_Caroncmb_uc then
				set the content of myChar to my getUnicode("caroncmb")
			else if myCode = orig_caroncmb_lc then
				set the content of myChar to my getUnicode("caroncmb")
			else if myCode = orig_Lbarred_uc then
				set the content of myChar to my getUnicode("Lbarred")
			else if myCode = orig_lbarred_lc then
				set the content of myChar to my getUnicode("lbarred")
			else if myCode = orig_grave_lc then
				set the content of myChar to my getUnicode("grave")
			end if
		end if
		
		
		
	end repeat
end tell

end tell

on getCode(myVal)
tell me
return id of myVal
end tell
end getCode

on getUnicode(charName)
considering case
if charName = “acute” then
return character id 769
else if charName is “caroncmb” then
return character id 780
else if charName is “grave” then
return character id 768
else if charName is “lbarred” then
return character id 619
else if charName is “Lbarred” then
return character id 11362
else if charName = “lamdabarred” then
return character id 411
else if charName = “Lamdabarred” then
return character id 63641
else if charName = “dotbelow” then
return character id 803
else if charName = “glottal” then
return character id 787
else if charName is “glottalstop” then
return character id 660
else if charName = “mark” then
return character id 847
else if charName = “wsuperior” then
return character id 695
else if charName = “Wsuperior” then
return character id 695
end if
end considering
end getUnicode

Hi
I found a small scirpt in the Word 2004 Applescript reference guide that alludes to being able to search/replace both text and formatting (styles). However, Applescript barks with the code. It doesn’t like ‘clear formatting replacement’ line. Any ideas? Anyone have any experience or code examples for find/replace with fonts/styles?

tell application “Microsoft Word”
activate

set selFind to find object of selection
tell selFind
	clear formatting
	set content to "hi"
	clear formatting replacement
	set content of replacement to "hello"
	execute find wrap find find continue ¬
		replace replace all with match forward
end tell

end tell

Thx

If you’re interested, here’s the solution I created after trial and error. A loop with 2 dim array would make for shorter code…but I’m short on time…

tell application “Microsoft Word”
activate

set orig_acute_lc to 98 as integer
set orig_Acute_uc to 66
set orig_Xdot_uc to 68
set orig_xdot_lc to 100
set orig_Lamdaglottalized_uc to 70
set orig_lamdaglottalized_lc to 102
set orig_Glottalstop_uc to 71
set orig_glottalstop_lc to 103
set orig_Glottalized_uc to 74
set orig_glottalized_lc to 106
set orig_Wsuperior_uc to 82
set orig_wsuperior_lc to 114
set orig_Caroncmb_uc to 86
set orig_caroncmb_lc to 118
set orig_Lbarred_uc to 90
set orig_lbarred_lc to 122
set orig_grave_lc to 96

set findFont to "sourceFont"
set replaceFont to "destFont"

set myFind to find object of text object of active document
clear formatting myFind
clear formatting replacement of myFind
set name of font object of myFind to findFont
set name of font object of replacement of myFind to replaceFont
set content of myFind to my getId(orig_acute_lc)
set content of replacement of myFind to my getUnicode("acute")
execute find myFind replace replace all
set content of myFind to my getId(orig_Acute_uc)
set content of replacement of myFind to my getUnicode("acute")
execute find myFind replace replace all
set content of myFind to my getId(orig_Xdot_uc)
set content of replacement of myFind to "X" & my getUnicode("dotbelow")
execute find myFind replace replace all
set content of myFind to my getId(orig_xdot_lc)
set content of replacement of myFind to "x" & my getUnicode("dotbelow")
execute find myFind replace replace all
set content of myFind to my getId(orig_Lamdaglottalized_uc)
set content of replacement of myFind to my getUnicode("Lamdabarred") & my getUnicode("glottal")
execute find myFind replace replace all
set content of myFind to my getId(orig_lamdaglottalized_lc)
set content of replacement of myFind to my getUnicode("Lamdabarred") & my getUnicode("glottal")
execute find myFind replace replace all
set content of myFind to my getId(orig_Glottalstop_uc)
set content of replacement of myFind to my getUnicode("glottalstop")
execute find myFind replace replace all
set content of myFind to my getId(orig_glottalstop_lc)
set content of replacement of myFind to my getUnicode("glottalstop")
execute find myFind replace replace all
set content of myFind to my getId(orig_Glottalized_uc)
set content of replacement of myFind to my getUnicode("glottal")
execute find myFind replace replace all
set content of myFind to my getId(orig_glottalized_lc)
set content of replacement of myFind to my getUnicode("glottal")
execute find myFind replace replace all
set content of myFind to my getId(orig_Wsuperior_uc)
set content of replacement of myFind to my getUnicode("Wsuperior")
execute find myFind replace replace all
set content of myFind to my getId(orig_wsuperior_lc)
set content of replacement of myFind to my getUnicode("wsuperior")
execute find myFind replace replace all
set content of myFind to my getId(orig_Caroncmb_uc)
set content of replacement of myFind to my getUnicode("caroncmb")
execute find myFind replace replace all
set content of myFind to my getId(orig_caroncmb_lc)
set content of replacement of myFind to my getUnicode("caroncmb")
execute find myFind replace replace all
set content of myFind to my getId(orig_Lbarred_uc)
set content of replacement of myFind to my getUnicode("Lbarred")
execute find myFind replace replace all
set content of myFind to my getId(orig_lbarred_lc)
set content of replacement of myFind to my getUnicode("Lbarred")
execute find myFind replace replace all
set content of myFind to my getId(orig_grave_lc)
set content of replacement of myFind to my getUnicode("grave")
execute find myFind replace replace all
set content of myFind to ""
set content of replacement of myFind to ""
execute find myFind replace replace all

display dialog "Transliterate completed!"

end tell

on getUnicode(charName)
considering case
if charName = “acute” then
return character id 769
else if charName is “caroncmb” then
return character id 780
else if charName is “grave” then
return character id 768
else if charName is “lbarred” then
return character id 619
else if charName is “Lbarred” then
return character id 11362
else if charName = “lamdabarred” then
return character id 411
else if charName = “Lamdabarred” then
return character id 63641
else if charName = “dotbelow” then
return character id 803
else if charName = “glottal” then
return character id 787
else if charName is “glottalstop” then
return character id 660
else if charName = “mark” then
return character id 847
else if charName = “wsuperior” then
return character id 695
else if charName = “Wsuperior” then
return character id 695
end if
end considering
end getUnicode

on getId(cp)
return character id cp
end getId