-- Search and replace in a book.
-- For FrameMaker 5.5.6.
-- Uses fcodes, which simulate user input (key presses).
-- Problems: can't get return code from set search term dialog, so Cancel command is not
-- returned to script. Also dialog that asks if user is sure to proceed (no undo)...

-- Double tell method: how to avoid having to select the application
-- the first time the compiled script is run on a new machine.
-- Note that this works only on Sys 7.5 and later (which includes 8.x).
-- Save this script as a compiled script.

-- Copyright Markus Frischknecht July 1999.
-- mailto:mfrischknecht@access.ch
-- Version 1.0: loop over book, allow saving and closing book documents.


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

tell application "FrameMaker 5.5 PowerPC" to tell FrameMaker
	
	try
		
		-- only proceed if book window is in front
		tell active book
			set allFiles to name of every book component
			-- names are paths, which can be used in open command
		end tell -- book
		
		set firstbook to true -- used as flag: when to display set search dialog
		try
			repeat with currFile in allFiles
				
				open file currFile -- is ignored if file is already open
				set currComponent to document named currFile -- get this as document

				tell currComponent
					activate
					-- Doesn't always work!!! Dangerous since macro below will replace in frontmost window!
				end tell
				
				
				if firstbook then -- is only displayed if doc win in front
					Frame commands {565}
					-- !fis -> Display Set Search dialog; KBD_SETSEARCH 0x235, dec 565
					-- this command is only accepted with a document window in front
					set saveChanges to false
					set dlog to display dialog "Continue Replace Process?" buttons {"Abort", "OK with Save", "OK"} default button "OK"
					
					-- allow user to abort: display dialog Cancel, OK;
					-- necessary since Frame commands above can't detect "Cancel" button in dialog
					if button returned of dlog = "Abort" then
						return
					else if button returned of dlog = "OK with Save" then
						set saveChanges to true
					end if
					
				end if
				
				set firstbook to false
				
				with timeout of 300 seconds  -- should be enough even for large books
					
					Frame commands {563}
					--	!rg -> Change all; KBD_RGLOBAL 0x233, dec 563
					-- NB: always displays warning UNDO impossible...!
					-- And if not found in a book document, dialog displayed "Not found".
				end timeout
				
				-- Save document here if selected in earlier dialog:
				if saveChanges then
					save document named currFile
					-- Close doc here if you want.
					close document named currFile
				end if
			end repeat
			display dialog "Replacing done." buttons {"OK"} default button "OK"
		on error
			beep
		end try
		

		return  -- and quit
		
	on error
		-- no book in front, abort
		display dialog "Window in front is not a Book." & return & "Replace command can't proceed." buttons {"OK"} default button "OK"
	end try
end tell -- FrameMaker