MacTech Network:   MacTech Forums  |  MacForge.net  |  Computer Memory  |  Register Domains  |  Cables  |  iPod Deals  |  Mac Deals  |  Mac Book Shelf


  MacTech Magazine

The journal of Macintosh technology

 
 

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  MacTech Forums  
  Get a copy of MacTech RISK FREE  
Google
Entire Web
mactech.com
Mac Community
More...
MacTech Central
  by Category  
  by Company  
  by Product  
MacTech News
  MacTech News  
  Previous News  
  MacTech RSS  
Article Archives
  Show Indices  
  by Volume  
  by Author  
  Source Code FTP  
Inside MacTech
  Writer's Kit  
  Editorial Staff  
  Editorial Calendar  
  Back Issues  
  Advertising  
Contact Us
  Customer Service  
  MacTech Store  
  Legal/Disclaimers  
  Webmaster Feedback  

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.

Would you like a hard copy
or PDF of this Guide?

You can get a hard copy sent to you
AND download a PDF now ($19.95)
, or

... just download a PDF ($9.95).

Either way, you get a complimentary
MacTech Magazine Subscription

courtesy of the
Microsoft Mac Business Unit


 

  Magazine Reg. Price:  $50.00 value  
  Guide Reg. Price:  $40.00 value  
  You Save:  over $80.00!  

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.



 


Click here to find out more about our best subscription bundle deal ever!
2 years of the magazine, and the all new MacTech DVD ... at 70% off!



Click on the cover to
see this month's issue!

TRIAL SUBSCRIPTION
Get a RISK-FREE subscription to the only technical Mac magazine!
 
Nokia Qt Beta
 


MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797

Register Low Cost (ok dirt cheap!) Domain Names in the MacTech Domain Store. As low as $1.99!
Save on brand compatible and name brank ink jet and laser supplies.
Save on long distance * Upgrade your Computer
Movies with No Late Fees!

See local info about Westlake Village
SJ * BRJ * BJ * OJ * NITS
Staff Site Links



All contents are Copyright 1984-2008 by Xplain Corporation. All rights reserved.

MacTech is a registered trademark of Xplain Corporation. Xplain, Video Depot, Movie Depot, Palm OS Depot, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, NetProLive, JavaTech, WebTech, BeTech, LinuxTech, Apple Expo, MacTech Central and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.