Replace Text with each system Font

Hi, I am very new to AppleScript but determined to learn. I have an XML file which has the following line about 300 times:

American Typewriter

I am trying to create an AppleScript which can first create a list of all system fonts (names) using something like:

do list available fonts

Then go through the XML file and every time it finds the “American Typewriter” text it needs to replace it with the next font in the list. So by the end of the process, the XML should contain 300 (assuming there are 300 fonts on the system) different font names, where it used to always say “American Typewriter”.

The problem is I don’t even know where to start. I have used bits of other scripts to create a script which just replaces “American Typewriter” with “Helvetica”, but it needs me to copy and paste the text from the XML file into it to work (I did this to try and get a rough starting point):

[b]set the message_text to ¬
“XML TEXT GOES HERE”
set the message_text to replace_chars(message_text, “American Typewriter”, “Helvetica”)

on replace_chars(this_text, search_string, replacement_string)
set AppleScript’s text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript’s text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript’s text item delimiters to “”
return this_text
end replace_chars[/b]

As you can see I am miles away from what I actually need. Can anyone give a few pointers to help add the part where it first does the list fonts part, then looks for an external XML file and then does the parsing, text replacement and saves the new XML file.

Thanks for any help,
Mark Burton

This is a quick example using text item delimiters (assumes the number of “American Typewriter” lines in the xml is the same than number of fonts):

set theXML to "<something>
	<value>American Typewriter</value>
	<value>American Typewriter</value>
	<value>American Typewriter</value>
</something>"

set itemsToInsert to {"a", "b", "c"}

set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "American Typewriter"
set theXML to theXML's text items
set AppleScript's text item delimiters to prevTIDs

(*
 theXML is now:

{"<something>
	<value>", "</value>
	<value>", "</value>
	<value>", "</value>
</something>"}

*)

set finalList to {theXML's item 1}
repeat with i from 1 to count itemsToInsert
	set end of finalList to itemsToInsert's item i
	set end of finalList to theXML's item (i + 1)
end repeat
--> now, add rest of theXML's items

try --> perhaps there are no more "American Typewriter"!
	set finalList's end to items (i + i) thru -1 of theXML
end try

--> convert it back to text
set theXML to finalList as text

(*
theXML is now:

"<something>
	<value>a</value>
	<value>b</value>
	<value>c</value>
</something>"

*)

About obtaining a list of fonts… I only know Extra Suites’ command “ES list fonts”. Look here: http://www.kanzu.com/
Maybe it does exists a “call method” available from a AppleScript Studio script, but I don’t know it :wink:

Thanks JJ.

That is very, very helpful. Actually that king of sets the structure. Like you say the font name calling seems to be a bit difficult. Also I am hoping I can create it so that the XML file can be called and parsed, rather than having to actually paste the data into the Script. That way it could be saved as droplet or standalone app which could be used on many different XML files.

Thank you so much for the help and work on the script, I shall report back if I actually manage to get this thing up and running.

Kind regards
Mark