Using the Word Dictionary
18
Referring to the active document
Instead of referring to a document by name or index number -- for example
document
"Sales.doc"
-- the active document property of the application class returns a document object
which refers to the active document (the document in the forefront). The following example
displays the name of the active document, or if no documents are open, displays a message.
if (count of documents) 1 then
display dialog (get name of active document)
else
display dialog "No documents are open"
end if
Working with the selection object
When you work on a document in Word, you usually select text and then perform an action, such as
formatting the text or typing text. In a script, it is usually not necessary to select text before
modifying the text. Instead, you create a text range object that refers to a specific portion of the
document. For information about defining text range objects, see
Working with text range objects
.
However, when you want your code to respond to or change the selection, you can do so with the
selection object.
The select command selects or highlights an object. For example, the following instruction selects
the first word in the active document.
select word 1 of active document
For more select command examples, see
Selecting text in a document
.
The selection property of the application class returns a selection object that represents the selection
in the active document window pane. The selection property of the window class represents the
selection of the window. Word maintains the selection object for a window even if the window is
not active. There can be only one selection object per document window pane and only one
selection object can be active. If there is no highlighted selection, the selection object represents the
insertion point. For example, the following example changes the paragraph formatting of the
paragraphs in the selection.
set left indent of paragraph format of selection to ¬
inches to points inches 0.5
The following example applies bold formatting to the selected text.
set bold of font object of selection to true
Working with text range objects
A common use of AppleScript is to specify an area in a document and then do something with it,
such as insert text or apply formatting. For example, you may want to write a script that locates a
word or phrase within a portion of a document. The portion of the document can be represented by
a text range object. After the text range object is identified, commands and properties of the text
range object can be applied in order to modify the contents of the text range.