Moving from Microsoft Office VBA to AppleScript:
MacTech's Guide to Making the Transition
Introduction
|
Table of Contents
Page Prev and Page Next buttons at bottom of the page.
|
April, 2007
Page 23
Dim oDoc As Document
Set oDoc = Documents.Open FileName:="Mac HD:Folder:Filename"
With oDoc
.HyphenationZone = InchesToPoints(0.25)
.HyphenateCaps = False
.AutoHyphenation = True
End With
The process of setting a variable to the result of a command is usually straightforward in AppleScript too. However, the command open from the Standard Suite does not return a result, so you cannot set a variable to the command. The commands in the Standard Suite are derived from a model supplied by Apple, who strongly advise all applications to use the Standard Suite as far as possible to maintain a consistency to the language.
It is possible for applications to add their own parameters to commands of the Standard Suite, and Word adds a great many, in fact 10 of them, all derived from VBA's Open. But they cannot alter the behavior that open does not return a result. They would have done far better to do what Excel does – leave the plain open command in place without parameters, and also give Word its own 'open document' command with all the parameters, that would return a result.
Here is a workaround that seems likely to be successful:
tell application "Microsoft Word"
open " Mac HD:Folder:Filename.doc"
set theDoc to active document
tell theDoc
set hyphenation zone to (inches to points inches 0.25)
set hyphenate caps to true
set auto hyphenation to true
end tell
end tell
We open the document and then set our variable to active document – the document in the front. The open command always opens the file in the front of the application, so this works if the document was not already open. If the document already is open, however, it does not come to the front, and you will be acting on the wrong document. So here's the full workaround you need:
tell application "Microsoft Word"
try
set theDoc to document "Filename.doc"
on error
open "Mac HD:Folder:Filename.doc"
set theDoc to active document
end try
tell theDoc
set hyphenation zone to (inches to points inches 0.25)
set hyphenate caps to true
set auto hyphenation to true
end tell
end tell
It still makes me nervous though – theoretically there might some problem along the way. But it works, and is what we have for now. When Office 2008 arrives, do check in the Microsoft Word Suite to see if there is an 'open document' or similarly named command that returns a result, and also check open in the Standard Suite just in case, as well.
Working with Collections
As explained in Chapter 2, AppleScript does not have Collection Objects as such. You simply get the plural (a list) of any class. You will note that the dictionary always gives the plural if there is one: very, very rarely, there is no plural, and then you can‘t get a list. This is only the case for those rare classes where there is only one such object, such as application or selection. In all other cases, the class is an element of another class (and is so denoted by that "Contained by" reference in the dictionary), either of the application itself or of another class.
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine