-- Set the size of a document window (or of all files in a book)
-- to "fit to page".
--
-- For FrameMaker 5.5.6.
--
-- Uses fcodes, which simulate user input (key presses).
-- Note that the "fit to page" command will set the window to a 
-- max size limited by the current screen, so for e.g. a A4 page
-- on a screen with 800 * 600, wthe indow can never be high
-- enough to show whole page. Furthermore, the window title bar will
-- never be set to overlap with FM's toolbar (if it's visible).
-- And the window will not be moved to the top left screen position
-- (maybe it would if window were zoomed first to its full size?).

-- Markus Frischknecht Aug/Nov 1999.

-- Watch for line breaks/continuation character in the lines
-- with the display dialog command!


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
    -- some parameters to control this script:
    set saveChanges to false -- individual documents will not be saved
    set closeDoc to true -- individual documents will be closed after processing
    -- closeDoc will only be checked and used if saveChanges is true
    
    -- 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
    
    try
      -- if we get here, all files of a book to process
      set dlog to display dialog "All Windows set to fit to page?" ¬
         buttons {"Abort","OK with Save", "OK"} default button "OK" -- one long line
      
      if button returned of dlog = "Abort" then
        return
      else if button returned of dlog = "OK with Save" then
        set saveChanges to true
      end if
      
      
      -- now loop over all files of a book
      
      repeat with currFile in allFiles
        open file currFile -- is ignored if file is already open
        set currComponent to document named currFile -- get file as document
        tell currComponent
          activate
          -- Activate doesn't always work! Dangerous if macro below will replace in frontmost window!
          with timeout of 120 seconds
            Frame commands {3891} -- zoom: fit window to page
          end timeout
        end tell
        
        -- Save document here if desired:
        
        if saveChanges then
          save document named currFile
          -- Close doc here if desired:
          if closeDoc then
            close document named currFile
          end if
        end if
      end repeat
      display dialog "All document windows set to fit to page." ¬
        buttons {"OK"} default button "OK" -- one long line
    on error
      beep
    end try
    
    return -- and exit script
    
  on error
    -- no book in front, assume that it is a standard document;
    -- no options for saving or closing considered here
    try
      tell active document
        -- activate
        Frame commands {3891} -- zoom: fit window to page
      end tell
    on error
      display dialog "Window in front is not a Document or a Book." buttons {"OK"} default button "OK"
    end try
  end try
end tell -- FrameMaker