-- A FrameMaker AppleScript for changing the language of all paragraph formats.
-- Note that FrameMaker will remove all overrides in paragraph styles when that 
-- underlying style is changed!

-- This version runs with FrameMaker 5.5.6.
-- Needs the Dialog Director Scripting Addition by Christopher E. Hyde.
-- Save this script as a Stay-Open Application!
-- Markus Frischknecht 1999
-- mailto:mfrischknecht@access.ch


-- if property: can save settings and position between runs
property dReplaceLanguage : {bounds:[32, 54, 494, 486], name:"Replace Language", style:movable dialog, contents:[¬
	{class:push button, bounds:[386, 405, 444, 425], name:"OK"}, ¬
	{class:push button, bounds:[260, 405, 318, 425], name:"Cancel"}, ¬
	{class:static text, bounds:[10, 5, 319, 21], contents:"Replace Language in FrameMaker Book/File:"}, ¬
	{class:radio group, bounds:[9, 27, 227, 45], button offset:[0, 16], contents:["All languages", "All but None", "Only the following lanuage(s):"], value:2}, ¬
	{class:check box, bounds:[51, 77, 182, 95], name:"Brazilian", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 93, 182, 111], name:"British", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 109, 182, 127], name:"Canadian-French", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 125, 182, 143], name:"Catalan", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 141, 182, 159], name:"Danish", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 157, 182, 175], name:"English (US)", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 173, 182, 191], name:"Finnish", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 189, 182, 207], name:"French", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 205, 182, 223], name:"German", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 221, 182, 239], name:"Italian", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 237, 182, 255], name:"Norwegian", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 253, 182, 271], name:"Nynorsk", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 269, 182, 287], name:"Portuguese", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 285, 182, 303], name:"Spanish", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 301, 182, 319], name:"Swedish", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 317, 182, 335], name:"Swiss-German", value:false, enabled:772}, ¬
	{class:check box, bounds:[51, 333, 182, 351], name:"None", value:false, enabled:772}, ¬
	{class:static text, bounds:[240, 60, 446, 76], contents:"With the following language:"}, ¬
	{class:radio group, bounds:[249, 77, 381, 95], button offset:[0, 16], contents:["Brazilian", "British", "Canadian-French", "Catalan", "Danish", ¬
		"English (US)", "Finnish", "French", "German", "Italian", "Norwegian", ¬
		"Nynorsk", "Portuguese", "Spanish", "Swedish", "Swiss-German", "None"], value:1} ¬
		, ¬
	{class:static text, bounds:[345, 5, 450, 21], contents:"MF © 1998"}, ¬
	{class:check box, bounds:[10, 360, 244, 378], name:"Save documents after changes?", value:false}, ¬
	{class:check box, bounds:[10, 378, 285, 396], name:"Unlock and change locked documents?", value:false} ¬
		] ¬
	}




tell application "Finder" to set FrameMaker to ¬
	(process 1 whose creator type is "Fm55") as «class psn »


tell application "FrameMaker 5.5 PowerPC" to tell FrameMaker
	try
		tell active book
			-- get name of every book components
			set allFiles to name of every book component
		end tell
		set IsBook to true
	on error
		set IsBook to false -- it's certainly no book
		set docList to documents
		if docList is {} then
			beep
			display dialog "No document is open that can be changed." buttons "OK" default button 1
			return -- quit this script
		else
			-- it's not a book, but a single doc, just get path, put into allFiles
			set allFiles to name of active document as list
		end if
	end try
	
	-- activate -- necessary with DD0.7, otherwise script app remains in front
	tell me to set DlgResult to dd auto dialog dReplaceLanguage -- run dialog
	
	set bounds of dReplaceLanguage to item 27 of DlgResult
	
	
	if (item 1 of DlgResult) then
		-- build a set of all languages
		
		set LanguageSet to {¬
			Brazilian, British, Canadian_French, Catalan, Danish, ¬
			English, Finnish, French, German, Italian, Norwegian, ¬
			Nynorsk, Portuguese, Spanish, Swedish, Swiss_German, none ¬
			}
		
		set SaveAtEnd to item 25 of DlgResult
		set value of item 25 of contents of dReplaceLanguage to SaveAtEnd
		
		set UnlockAndChange to item 26 of DlgResult
		set value of item 26 of contents of dReplaceLanguage to UnlockAndChange
		
		
		-- All, All but none, The following languages to replace: radio button 1, 2 or 3
		set ChangeWhichLanguages to item 4 of DlgResult
		set value of item 4 of contents of dReplaceLanguage to ChangeWhichLanguages
		
		-- new language
		set value of item 23 of contents of dReplaceLanguage to item 23 of DlgResult -- save for next dialog run

		set newLanguage to item (item 23 of DlgResult) of LanguageSet
		
		
		if ChangeWhichLanguages is 3 then
			set ChangeSet to {} -- the languages to replace
			
			repeat with CheckLang from 5 to 21
				set value of item CheckLang of contents of dReplaceLanguage to item CheckLang of DlgResult
				if item CheckLang of DlgResult then
					-- convert from check box index to index in LanguageSet
					set ChangeSet to ChangeSet & (item (CheckLang - 4) of LanguageSet) as list
				end if
			end repeat
			
		end if
		
		
		set numberOfChanges to 0 -- counter to see how many we changed
		
		
		repeat with currFile in allFiles
			set ContinueLoop to true
			
			try
				open currFile
			on error
				display dialog ¬
					"The current document has never been saved. Please save it first and try again." buttons "Cancel" default button 1
				
				exit repeat
			end try
			
			set displaying to false -- avoid update after each format is changed
			
			tell active document
				set LockAgain to false
				if is view only then
					if UnlockAndChange then
						set (is view only) to false
						set LockAgain to true
					else -- change is not allowed
						beep
						try
							set dlgReply to display dialog "Document \"" & currFile & ¬
								"\" is locked. No changes are made." buttons {"Cancel", "OK"} default button "OK"
							set ContinueLoop to false -- we get here if OK pressed
						on error
							exit repeat -- if Cancel was pressed
						end try
					end if
				end if
				
				
				if ContinueLoop then
					
					set parafmtname to get name of every paragraph format
					repeat with i from 1 to the number of items of parafmtname
						set ChangeThisLanguage to false -- default: don't change it
						
						if ChangeWhichLanguages is 1 then -- change All
							set ChangeThisLanguage to true
						else
							set parafmt to (get properties of paragraph format (item i of parafmtname))
							
							if ChangeWhichLanguages is 2 then
								-- change all except None
								if (language of parafmt) is not none then
									set ChangeThisLanguage to true
								end if
							else -- must be ChangeWhichLanguages = 3
								-- test whether current language is in the list of languages to change
								if (language of parafmt) is in ChangeSet then
									set ChangeThisLanguage to true
								end if
							end if -- ChangeWhichLanguages is 2
						end if -- ChangeWhichLanguages is 1
						
						if ChangeThisLanguage then -- actual change
							set language of paragraph format (item i of parafmtname) to newLanguage
							set numberOfChanges to numberOfChanges + 1
						end if
					end repeat
					
					-- finally "close" or "save" if changed; or just leave open, save manually
					
					if SaveAtEnd then
						if LockAgain then
							set is view only to true -- maybe: if view only change back to this
							save
						else
							save
						end if
					end if
					
				end if -- ContinueLoop
				
			end tell -- active document
			
			set displaying to true
			-- to redraw display, after all tags have been updated
		end repeat -- with allFiles
		display dialog ((("Replace Language:" & return & numberOfChanges as string) & " changes made ") & ¬
			"in " & (length of allFiles) as string) & " document(s)." buttons "OK" default button 1
		
		
	end if
	
end tell -- FrameMaker