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 16
-- check to see if Word is already running, for later
tell application "System Events"
set wordWasRunning to exists process "Microsoft Word"
end tell
try
tell application "Microsoft Word"
-- no need to make Word visible, nor the option*
activate
set oDoc to make new document
-- code here to manipulate the document
close oDoc saving no
if not wordWasRunning then quit
end tell
on error errMsg number errNum
-- bring current application (e.g., Excel) to the front to view dialog
activate
display dialog "Word caused a problem. " & errMsg & ¬
return & "Error: " & errNum with icon 0
if not wordWasRunning then
tell application "Microsoft Word" to quit
end if
end try
-- no need to release object references
end ControlWord
Of course without all my explanatory comments the script would be even shorter. Notice that we ask System Events (a faceless background application with a very interesting dictionary – you can find it in /System/Library/CoreServices, if you want to browse it) to tell us if Word, as a process, exists, since there's no way to mimic the VBA method of trying to create a new Word instance (which errors if one exists already). Also note that Word (i.e., the application class in Word's dictionary) does not have a visible property, although window does, so you can certainly hide and show individual windows.
If you want to hide (or show) the whole application, you would do that in System Events, where you can set the visible property of any process, including Word. (The Finder can still do the same thing, at the moment, with its "legacy" commands, but these process features are now actually done by System Events: the Finder would just pass on the command, and some day may no longer do so.) This is a basic difference between the Mac and Windows: the system, rather than the application itself, controls things like visibility of applications.
Also note that in AppleScript you need to put a 'try' somewhere in order to catch an error. Usually, if one wanted to put the try block around the whole content of this ControlWord() handler, you'd put it around the call to the handler rather than inside it.
Also, note that in OS 10.4 (Tiger) that call to Word to quit inside the on error block will be well-behaved, but in OS 10.3 and earlier, if by any chance Word had already quit, this new tell statement would launch Word only to quit it!
What is Missing? What Are Your Options?
There are four big features of Office VBA that are missing from the AppleScript implementation. We've touched on one of them in discussing the script menu: you cannot run scripts from toolbar buttons. This will no doubt distress some people who like to run their macros this way, but at least you can run the scripts from a menu.
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine