The other day I wanted to find a way to accelerate the process of zooming a Word window so that it fits the width of the document. It's something I've done manually thousands of times over the years. Just go to Display, then Zoom, select the option and hit Enter.
I know that Word is scriptable but everything looked so complex that I never really tried. When I script in uncharted territory I usually go the top-bottom way: I get a document, check it's properties and go deeper and deeper until I find what I need. The issue with this approach is that there are so many different classes and sub-classes and properties all over that you easily get lost and frustrated.
I just tried the bottom-up approach with this Word feature and it worked pretty well: start from what you thing is the end property and go up the ladder to the highest level object.
In the case at hand, I suppose the element I'm looking for has "fit" in its description. That's my only assumption.
So, here we go. Open Script Editor, open the Microsoft Word dictionary and search for "fit".
In the "Microsoft Word Suite" we find 2 properties and 1 command:
fit text width is a property of "selection object"
fit text width (real) : Returns or sets the width in the current measurement units in which Microsoft Word fits the text in the current selection.
page fit is a property of "zoom"
page fit (page fit none/page fit full page/page fit best fit) : Returns or sets the view magnification of a window so that either the entire page is visible or the entire width of the page is visible.
fit to pages v : Decreases the font size of text just enough so that the document will fit on one fewer pages. An error occurs if Word is unable to reduce the page count by one.
A cursory reading tells us that we'd like to try "page fit" first. We know it is a property of "zoom", which is defined as:
zoom n [inh. base object] : Contains magnification options, for example, the zoom percentage for a window or pane.
Now we're getting closer to bridging our "bottom" (fit) to some "up" (which would be a document, or a window). Since we do have a reference to a window zoom percentage in the definition let's check "window" and see what we come up with.
The first thing we get is "active window", and the first item is:
active window (window, r/o) : Returns the currently active window object.
which is a property of application. active window returns a window object, so let's check what window objects are made of.
A glance at the list of properties of a window does not give us any hints at how to link a window to a zoom... There are no zoom properties so we have to go back to zoom and find hints there.
There is a second item for zoom, it is a property of a view:
zoom (zoom, r/o) : Returns the zoom object associated with this view object.
We're one step higher now, and since the definition of view is:
view n [inh. base object] : Contains the view attributes, show all, field shading, table gridlines, and so on, for a window or pane.
we have a connection to the window that we found earlier.
If we check window again, we see that it indeeds has a view property:
view (view, r/o) : Returns a view object that represents the view for the window.
So let's put this together:
active window is a window and has a view property that has a zoom property that has a page fit property which can hold one of the following 3 values: page fit none/page fit full page/page fit best fit
To make sure that we're not missing anything let's check the code item by item, I assume that you have a Word document open, otherwise the result will be "missing value".
tell application "Microsoft Word"
get active window
end tell
--> active window
so far, so good.
tell application "Microsoft Word"
get view of active window
end tell
--> view of active window
no issue with that
tell application "Microsoft Word"
get zoom of view of active window
end tell
--> zoom of view of active window
we're progressing
tell application "Microsoft Word"
get page fit of zoom of view of active window
end tell
--> page fit full page
Boom ! We made it. Our document is in full page view.
So, let's set it to "page fit best fit":
tell application "Microsoft Word"
set page fit of zoom of view of active window to page fit best fit
end tell
Et voilà! The window is now in page width fit.
I'm not sure which is the fastest: checking the web for a "zoom my document to page width in AppleScript" answer or finding your way into the dictionary to find the answer yourself, but I know which will help you be proficient with AppleScript faster...