Find and replace a specific font in Illustrator applescript

Hi all

I am new to applescript and cannot figure out why this isnt working, I would love some help please.

I am trying to select a specific font and then only replace that specific font with another, whilst leaving all other fonts untouched.

This is my version which seems as though it should work. It doesnt error but still wont change the font.


tell application "Adobe Illustrator"
	set t to every text frame of current document
	set y to count of every text frame of current document
	repeat with i in t
		if exists text font "Helvetica-Narrow" of text of i then
			set the text font of text of i to text font "Times-Italic" 
		end if
	end repeat
end tell

I can replace all text items with a specific font using this script in the whole document using this script, but as I said I only want to replace certain fonts:


tell application "Adobe Illustrator"
	set t to every text frame of document 1
	set y to count of every text frame of document 1
	repeat with i in t
		set the text font of text of i to text font "Times-Italic" -- or a number like 22 this number is the index of the text font
	end repeat
end tell


Model: 2 x 2.4 GHz Quad-Core Intel Xeon Mac Pro
AppleScript: Version 2.3 (118)
Browser: Firefox 13.0.1
Operating System: Mac OS X (10.6)

Hi. When you loop one variable in another, such as repeat with i in t, you end up with a reference like “item 1 of {whatever list items}”, rather than an object reference. This can be problematic, especially in Illustrator; you’d be better off iterating with a standard range, i.e., repeat with i from 1 to y.

Your example actually doesn’t require iteration (or the if consideration), as it can be filtered:

tell application "Illustrator CS"
	set endstateFont to text font "Times-Italic"
	tell document 1 to set (stories whose its text's text font contains text font "Helvetica-Narrow")'s text's text font to endstateFont
end tell

Marc Anthony’s script will not work if individual characters in a string have been changed. Try this:

 tell application "Adobe Illustrator"
	set thisDoc to document 1
	set findFont to "HelveticaNeue"
	set replaceFont to "Myriad-Bold"
	tell thisDoc
		set text font of (every character of every story whose properties contains {text font:text font (findFont)}) to text font replaceFont of application "Adobe Illustrator"
	end tell
end tell

Model: MacBook Pro
AppleScript: 2.1.2
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

Thanks for your post divster!
Thats great it worked perfectly. It replaced individually set characters which was what was needed for the task.

Marc Anthony, your script would not compile until the applcation was named correctly. But even after that something must be incorrect with the script as it gave an applescript error when run “Adobe Illustrator got an error: Parameter error.”

Character-level replacement wasn’t indicated as a need in your question. :frowning: You also have a later version of Illustrator, which is why the application name change is needed. As written, there may be some incompatibility between versions or, more probably, the font ownership on your machine is different for the targeted font; this variant should address those possibilities:

tell application "Illustrator CS"
	set endstateFont to text font "Times-Italic"
		tell document 1 to set (stories's characters whose its text font's name is "Helvetica-Narrow")'s text font to endstateFont
end tell

This was kind of implied in not wanting to change any other font.

But I have to hand it to you Marc Anthony - your method is far faster. I tried it on a lot of different fonts using a swap list. My method 68 seconds, your method 4 seconds. I still had to add

of application "Adobe Illustrator"

to the end of the line to get it to work in CS5.1 though

set swaplist to {{"Knockout-HTF26-JuniorFlyweight", "Knockout-26JuniorFlywght"}, {"Knockout-HTF27-JuniorBantamwt", "Knockout-27JuniorBantamwt"}, {"Knockout-HTF28-JuniorFeatherwt", "Knockout-28JuniorFeathrwt"}, {"Knockout-HTF29-JuniorLiteweight", "Knockout-29JuniorLtweight"}, {"Knockout-HTF30-JuniorWelterwt", "Knockout-30JuniorWelterwt"}, {"Knockout-HTF31-JuniorMiddlewt", "Knockout-31JuniorMiddlewt"}, {"Knockout-HTF32-JuniorCruiserwt", "Knockout-32JuniorCruisewt"}, {"Knockout-HTF33-JuniorHeviwt", "Knockout-33JuniorHeviwt"}, {"Knockout-HTF34-JuniorSumo", "Knockout-34JuniorSumo"}, {"Knockout-HTF46-Flyweight", "Knockout-46Flyweight"}, {"Knockout-HTF47-Bantamweight", "Knockout-47Bantamweight"}, {"Knockout-HTF48-Featherweight", "Knockout-48Featherweight"}, {"Knockout-HTF49-Liteweight", "Knockout-49Liteweight"}, {"Knockout-HTF50-Welterweight", "Knockout-50Welterweight"}, {"Knockout-HTF51-Middleweight", "Knockout-51Middleweight"}, {"Knockout-HTF52-Cruiserweight", "Knockout-52Cruiserweight"}, {"Knockout-HTF53-Heviweight", "Knockout-53Heviweight"}, {"Knockout-HTF54-Sumo", "Knockout-54Sumo"}, {"Knockout-HTF66-FullFlyweight", "Knockout-66FullFlyweight"}, {"Knockout-HTF67-FullBantamwt", "Knockout-67FullBantamwt"}, {"Knockout-HTF68-FullFeatherwt", "Knockout-68FullFeatherwt"}, {"Knockout-HTF69-FullLiteweight", "Knockout-69FullLiteweight"}, {"Knockout-HTF70-FullWelterwt", "Knockout-70FullWelterwt"}, {"Knockout-HTF71-FullMiddlewt", "Knockout-71FullMiddlewt"}, {"Knockout-HTF72-FullCruiserwt", "Knockout-72FullCruiserwt"}, {"Knockout-HTF73-FullHeviweight", "Knockout-73FullHeviweight"}, {"Knockout-HTF74-FullSumo", "Knockout-74FullSumo"}, {"Knockout-HTF90-UltmtWelterwt", "Knockout-90UltmtWelterwt"}, {"Knockout-HTF91-UltmtMiddlewt", "Knockout-91UltmtMiddlewt"}, {"Knockout-HTF92-UltmtCruiserwt", "Knockout-92UltmtCruiserwt"}, {"Knockout-HTF93-UltmtHeviwt", "Knockout-93UltmtHeviwt"}, {"Knockout-HTF94-UltmtSumo", "Knockout-94UltmtSumo"}}


tell application "Adobe Illustrator"
	activate
	tell document 1
		-- gets rid of empty text frames
		delete (every text frame whose contents is "")
		
		repeat with i from 1 to count of swaplist
			try
				-- old method is first - it's much slower than new method below
				--set text font of (every character of every story whose properties contains {text font:text font (item 1 of item i of swaplist)}) to text font (item 2 of item i of swaplist) of application "Adobe Illustrator"
				set (stories's characters whose its text font's name is (item 1 of item i of swaplist))'s text font to text font (item 2 of item i of swaplist) of application "Adobe Illustrator"
			end try
		end repeat
		
	end tell
end tell

Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)