It is simple to open an existing document in AppleScript, and the code is similar to that used in Visual Basic for Applications (VBA). Compare the following examples:
VBA |
|---|
Documents.Open FileName:="Mac HD:Folder:Filename.doc" |
AppleScript |
|---|
tell application "Microsoft Word" open alias "Mac HD:Folder:Filename.doc" end tell |
The VBA code uses the Open method, and AppleScript uses the open command (in the Standard Suite). The proper AppleScript uses the alias reference form, but the Microsoft Word developers enabled a coercion that allows you to use just the path text as an equivalent, as you can in VBA. In AppleScript, you can also open a list of documents all at once if you want.
Opening and modifying a document
There are times when you also want to modify a document, or get information from it. For example, below is the VBA code for accomplishing these tasks:
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
In VBA, you set a variable (reference) to the result of the
Open
statement, which returns a Document object. Then you set some properties of Document or apply some methods to it.
In AppleScript, you can also set a variable to the result of a command. However, you cannot set a variable to the open command from the Standard Suite because it does not return a result.
Workaround for the open command
As much as possible, application developers use the commands in the Standard Suite to maintain consistency in the language, and in doing so, they follow the model supplied by Apple. However, it is possible for application developers to add their own parameters to commands in the Standard Suite, and Word developers added 10 of them. They are all derived from VBA's Open method. That said, the application developers cannot alter the fact that open does not return a result
The following AppleScript example provides a workaround for modifying a document in light of the limitations in the open command:
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
This code opens the document and then sets the variable to
active document, which is the document in the front.
Workaround for the open command and an open document
If a document is not already open, the open command opens the file in the front of the application. However, if a document is already open, the file does not open in front, and you end up acting on the wrong document.
The following workaround shows code that you can use if your document is already open, and accommodates the limitations of open:
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


