Indesign CS4: find/change for font only

I want to automate swapping out “bad” fonts for “good” fonts. I know how I would do it in Quark, but reading the boards, it appears that I have to do the find change text options.

I’ve successfully done it for text changes, but not attributes. Others here seem to think I should look in story 1. But that assumes that I want to change the character or paragraph styles. These documents do not use either of these styles, and the font can be mixed in with others.

Looking at the find/change text options in the dictionary, I see properties, but I see no example. Doing it manually, I would click on Basic character formats in the F/C dialog, but I see no analog in the dictionary.

Would someone provide a simple model that I could follow? thanx, sam

I rarely go to the Find/Change options unless I have to. In this case you don’t really need to and it might be faster not to do it.

The first thing that you need is the property of the character to search for, and that is applied font. If you look at the properties of a character though you will not see that listed, they just use the label font for some reason.

The next thing that is tricky is you need to reference the fonts as application fonts, not document fonts when constructing them and have the correct name. This is one case where using a straight object reference may not be the best thing to do because that reference may contain “document 1” in it and if it does then it will not work correctly.

InDesign uses a tab to separate the family name from the style so “Verdana Regular” is “Verdana[tabRegular”. So to reference the font correctly you need to construct it as:

font "Verdana	Regular" of application "Adobe InDesign CS4"

So you could have something like this:

tell application "Adobe InDesign CS4"
	tell document 1
		
		set FontList to name of every font --use this to get the names of the fonts used in the document the way ID uses them
		
		set searchFont to item 1 of FontList
		set replaceFont to item 3 of FontList
		
		set applied font of every character of every story whose applied font is (font searchFont of application "Adobe InDesign CS4") to (font replaceFont of application "Adobe InDesign CS4")
	end tell
end tell

It is fast, takes care of all the characters in every story (even in embedded text frames) and there is only 1 undo to get back to the original state.

Wow, Jerome, you and I think the same. I had attempted the applied font and saw the tab but could not make it work, which pushed me in the Find/Change direction.

This is another one for the archives, thanx, sam

No problem Sam, I find that dealing with fonts can be a bit tricky in scripting ID mostly because I don’t do it much. Once you figure out how to address them though it is not hard, but I usually have to play with it a bit to remember (or relearn) how to achieve the results I am going after.

Hello!

First post here. Perhaps I should start a new thread for InDesign CS5.5?

My objective is to “map” several undesirable fonts to replacement fonts. I will have to do this repetitively throughout the project since I’ll keep getting an updated IDML (InDesign Markup Language) file that has many fonts that need to be replaced.

I have never used AppleScript before (that I recall). (I have used pre-made Javascripts in InDesign, but I don’t know how to write them.) I do not understand if when using this script I will be given the opportunity to select the “bad” font and its “replacement” or if the example script was just set up to replace font #1 with font #3 in the list of used fonts (that’s what it looks like to me).

I just tried the above AppleScript after changing all the “CS4” references to “CS5.5”. I doesn’t work with CS5.5 and gives this error: “Adobe InDesign CS5.5 got an error: AppleEvent timed out.” Below it says “number - 1712”.

Thanks! Eric (using Snow Leopard 10.6.8)

AppleScript: AppleScript Editor v. 2.3 (118) / AppleScript 2.1.2
Browser: Safari 534.52.7
Operating System: Mac OS X (10.6)

Hi Eric

I tried writing script to replace fonts. It worked when I wrote it but occasionally doesn’t. The gist of it is that the documents I was working on had some style sheets and some custom formatted text. Also, not all fonts were available to me, hence the missing fonts bit. Anyway, it seems very complicated and there is probably a bettter way of doing it.

Let me know if any of this is any good

-- as the title suggests this script is to change one family of Helvetica to another
-- if you want something different some tinkering will be required

-- the swap list works like this
-- item 1 - the old family
-- item 2 - the old style
-- item 3 - the new family
-- item 4 - the new style

on run
	
	tell application "Finder"
		set filelist to choose file with prompt "Choose your InDesign files for changing Helvetica family." with multiple selections allowed
	end tell
	
	repeat with anitem in filelist
		tell application "Adobe InDesign CS5"
			set user interaction level of script preferences to never interact
			open anitem without showing window
			changeHelvetica() of me -- runs the handler, of me so that the script runs it, not InDesign
			
			save document 1 -- to anitem
			close document 1
			set user interaction level of script preferences to interact with all
			
		end tell
	end repeat
	beep 3
	activate me
	display alert "Finished!"
end run

on open filelist
	
	repeat with anitem in filelist
		tell application "Adobe InDesign CS5"
			set user interaction level of script preferences to never interact
			open anitem without showing window
			
			changeHelvetica() of me -- runs the handler, of me so that the script runs it, not InDesign
			
			save document 1 to anitem
			close document 1
			set user interaction level of script preferences to interact with all
			
		end tell
	end repeat
	
	beep 3
	activate me
	display alert "Finished!"
	
	
end open


on changeHelvetica()
	
	set swapFontList to {¬
		{"Helvetica Neue (T1)", "35 Thin", "Helvetica Neue LT Std", "35 Thin"}, ¬
		{"Helvetica Neue (T1)", "45 Light", "Helvetica Neue LT Std", "45 Light"}, ¬
		{"Helvetica Neue (T1)", "46 Light Italic", "Helvetica Neue LT Std", "46 Light Italic"}, ¬
		{"Helvetica Neue (T1)", "55 Roman", "Helvetica Neue LT Std", "55 Roman"}, ¬
		{"Helvetica Neue (T1)", "57 Condensed", "Helvetica Neue LT Std", "57 Condensed"}, ¬
		{"Helvetica Neue (T1)", "66 Medium Italic", "Helvetica Neue LT Std", "66 Medium Italic"}, ¬
		{"Helvetica Neue (T1)", "75 Bold", "Helvetica Neue LT Std", "75 Bold"}, ¬
		{"Helvetica Neue (T1)", "85 Heavy", "Helvetica Neue LT Std", "85 Heavy"}, ¬
		{"Helvetica Neue (T1)", "87 Heavy Condensed", "Helvetica Neue LT Std", "87 Heavy Condensed"} ¬
			}
	-- don't ask me why - it doesn't make any sense
	set swapMissingFontList to {¬
		{"[Helvetica Neue (T1)]", "35 Thin", "Helvetica Neue LT Std", "35 Thin"}, ¬
		{"[Helvetica Neue (T1)]", "45 Light", "Helvetica Neue LT Std", "45 Light"}, ¬
		{"[Helvetica Neue (T1)]", "46 Light Italic", "Helvetica Neue LT Std", "46 Light Italic"}, ¬
		{"[Helvetica Neue (T1)]", "55 Roman", "Helvetica Neue LT Std", "55 Roman"}, ¬
		{"[Helvetica Neue (T1)]", "57 Condensed", "Helvetica Neue LT Std", "57 Condensed"}, ¬
		{"[Helvetica Neue (T1)]", "66 Medium Italic", "Helvetica Neue LT Std", "66 Medium Italic"}, ¬
		{"[Helvetica Neue (T1)]", "75 Bold", "Helvetica Neue LT Std", "75 Bold"}, ¬
		{"[Helvetica Neue (T1)]", "85 Heavy", "Helvetica Neue LT Std", "85 Heavy"}, ¬
		{"[Helvetica Neue (T1)]", "87 Heavy Condensed", "Helvetica Neue LT Std", "87 Heavy Condensed"} ¬
			}
	
	tell application "Adobe InDesign CS5"
		set myDocument to document 1
		tell myDocument
			set myParaStyles to name of paragraph styles of myDocument
			repeat with i from 1 to count of items of myParaStyles
				set myFontApplied to name of applied font of paragraph style i
				repeat with x from 1 to count of swapFontList
					set myFontToChange to ((item 1 of item x of swapFontList) & "\t" & (item 2 of item x of swapFontList))
					set myNewFamily to (item 3 of item x of swapFontList)
					set myNewStyle to (item 4 of item x of swapFontList)
					try
						if myFontApplied is equal to myFontToChange then
							set properties of paragraph style i to {applied font:myNewFamily, font style:myNewStyle}
						end if
					end try
				end repeat
			end repeat
		end tell
		tell myDocument
			set myCharStyles to name of character styles of myDocument
			repeat with i from 2 to count of items of myCharStyles
				set myFontApplied to applied font of character style i
				repeat with x from 1 to count of swapFontList
					set myNewFamily to (item 3 of item x of swapFontList)
					set myNewStyle to (item 4 of item x of swapFontList)
					set myFontToChange to (item 1 of item x of swapFontList)
					if myFontApplied is equal to myFontToChange then
						set properties of character style i to {applied font:myNewFamily, font style:myNewStyle}
					end if
				end repeat
			end repeat
		end tell
		
		
		-- seems like you have to set styles and fonts separately. and styles first
		repeat with x from 1 to count of swapFontList
			set myFontToChange to (item 1 of item 1 of swapFontList) -- no need to loop, therefore the first list item is used
			set myNewFamily to (item 3 of item 1 of swapFontList) -- ditto
			set myOldStyle to (item 2 of item x of swapFontList)
			set myNewStyle to (item 4 of item x of swapFontList)
			--Clear the find/change preferences.
			set find text preferences to nothing
			set change text preferences to nothing
			--Set the find options.
			set case sensitive of find change text options to false
			set include footnotes of find change text options to true
			set include hidden layers of find change text options to true
			set include locked layers for find of find change text options to true
			set include locked stories for find of find change text options to true
			set include master pages of find change text options to true
			set applied font of find text preferences to myFontToChange
			set applied font of change text preferences to myNewFamily
			set whole word of find change text options to false
			set font style of find text preferences to myOldStyle
			set font style of change text preferences to myNewStyle
			
			tell myDocument
				set myFoundItems to change text
			end tell
		end repeat
		
		-- and then the font family.
		set myFontToChange to (item 1 of item 1 of swapFontList) -- no need to loop, therefore the first list item is used
		set myNewFamily to (item 3 of item 1 of swapFontList) -- ditto
		set find text preferences to nothing
		set change text preferences to nothing
		
		set case sensitive of find change text options to false
		set include footnotes of find change text options to true
		set include hidden layers of find change text options to true
		set include locked layers for find of find change text options to true
		set include locked stories for find of find change text options to true
		set include master pages of find change text options to true
		set whole word of find change text options to false
		set applied font of find text preferences to myFontToChange
		set applied font of change text preferences to myNewFamily
		tell myDocument
			set myFoundItems to change text
		end tell
		
		--Clear the find/change preferences after the search.
		set find text preferences to nothing
		set change text preferences to nothing
		
		-- and then do it all again just in case the fonts are missing
		repeat with x from 1 to count of swapMissingFontList
			set myOldStyle to (item 2 of item x of swapMissingFontList)
			set myNewStyle to (item 4 of item x of swapMissingFontList)
			--Clear the find/change preferences.
			set find text preferences to nothing
			set change text preferences to nothing
			--Set the find options.
			set case sensitive of find change text options to false
			set include footnotes of find change text options to true
			set include hidden layers of find change text options to true
			set include locked layers for find of find change text options to true
			set include locked stories for find of find change text options to true
			set include master pages of find change text options to true
			set whole word of find change text options to false
			set font style of find text preferences to myOldStyle
			set font style of change text preferences to myNewStyle
			
			tell myDocument
				set myFoundItems to change text
			end tell
		end repeat
		
		-- and then the font family.
		set myFontToChange to (item 1 of item 1 of swapMissingFontList) -- no need to loop, therefore the first list item is used
		set myNewFamily to (item 3 of item 1 of swapMissingFontList) -- ditto
		set find text preferences to nothing
		set change text preferences to nothing
		
		set case sensitive of find change text options to false
		set include footnotes of find change text options to true
		set include hidden layers of find change text options to true
		set include locked layers for find of find change text options to true
		set include locked stories for find of find change text options to true
		set include master pages of find change text options to true
		set whole word of find change text options to false
		set applied font of find text preferences to myFontToChange
		set applied font of change text preferences to myNewFamily
		tell myDocument
			set myFoundItems to change text
		end tell
		
		--Clear the find/change preferences after the search.
		set find text preferences to nothing
		set change text preferences to nothing
		
		
		-- mop up the remaining characters
		tell myDocument
			repeat with i from 1 to count of swapFontList
				set findChangeStyle to (item 4 of item i of swapFontList)
				
				set searchFont to "Helvetica Neue (T1)\t" & findChangeStyle
				set replaceFont to "Helvetica Neue LT\t" & findChangeStyle
				try
					set (applied font of every character of every story whose applied font is (font searchFont of application "Adobe InDesign CS5")) to (font replaceFont of application "Adobe InDesign CS5")
				end try
			end repeat
		end tell
		
		
		
	end tell
end changeHelvetica


Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)