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


  MacTech Magazine

The journal of Macintosh technology

 
 
FileGenius: File Transfer & Management

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  Submit a Tip!  
  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 18



In case you skipped these when learning AppleScript, you should know about two very powerful features of AppleScript that can really speed up your scripts, and simplify them too. Most scriptable applications, fortunately including the Microsoft Office applications, implement both of them.

The first is the whose (or where) filter or, as the old Script Editor used to refer to it, specifying an element "by satisfying a condition".

tell application "Microsoft Word"

    every paragraph of active document whose content of text object contains "AppleScript"

end tell

gets me a list of 66 paragraphs in this document containing the string "AppleScript" in 2 seconds flat, without having to run a laborious repeat loop through every paragraph in turn. You can also ask for first paragraph (or last paragraph) too, but be sure to put that version in a try/on error block since it errors if there are none.

This is really incredibly powerful, works on virtually every element of every class in all the applications, and can save you great time and effort (if you're sure you've got the syntax correct!). I strongly recommend scrapping tedious repeat loops for your For, For Each, Do While VBA loops and substituting whose filters whenever possible when converting your macros. (The repeat loops seem to run a lot slower in AppleScript than in VBA, too – another very good reason.)

One gotcha to look out for: if the name of the property you're filtering on also happens to be the name of a class (e.g., category in Entourage, or replacement in Word) then use the synonym where its instead of whose: the important word its focuses AppleScript on the element being considered and its property of the equivocal name – otherwise AppleScript gets confused since the class name has priority when in doubt and you get an incorrect result of {} (no results).

An equally powerful feature for putting AppleScript to work and avoiding tedious repeat loops is asking for the (singular) property of every element:

 

tell application "Microsoft Word"

    name of every document

end tell

-->{"Document3", "MacPowerPointVBA_PunchList.doc", "Converting VBA To AppleScript in Microsoft Office.doc", "Document2"}

   

    name local of every Word style of active document
    --> {"1 / 1.1 / 1.1.1", "1 / a / i", "Article / Section", "Balloon Text", "Block Text", "Body Text", "Body Text 2", … etc

You get a list of the requested property for every element. Once again, there is no need to go through a tedious repeat loop through each element. (In the case of Word styles, that would be upwards of 165 elements. There could be hundreds or thousands of elements in other contexts.)

Now very occasionally you'll hit a bug with this method (e.g., I just hit one trying to get content of every word of text object of paragraph 1 of active document, where the bug is actually with every word, not with the content of property retrieval), but it's very rare: as long as you can get 'every element of some object' as a list you can get any of its properties in a list too.

Note that you cannot apply a whose filter to the resulting list, or any other AppleScript list (a failing of AppleScript many have waited long to see disappear, and may still yet someday). It‘s only applicable to application elements. What is odd is that most applications, including Entourage, allow you to combine the two features discussed here to get a property list on a whose filter, e.g.:

tell application "Microsoft Entourage"

    name of every contact whose default email address contains "microsoft.com"

end tell
--> {"Bill Gates", "Steve Ballmer", "Roz Ho"}

but you can't do the equivalent for Word, Excel or PowerPoint, e.g.:

tell application "Microsoft Word"

    name of every document whose content of text object contains "AppleScript"

end tell
--> missing value

That's a shame, but the two special features still give you a lot of added power on their own.

There's lots more to explore in AppleScript, but this should cover most of the essential points of convergence, and divergence, between the VBA and AppleScript ways of doing the same things in general terms, across the applications – especially if you read one of the AppleScript books before attempting your own scripts. Now it's time to look at some examples from the individual Microsoft Office applications.

Note: In the following chapters, the version of Office used is 2004, updated to 11.3.3, current at the time of writing. It is quite likely that many of the gaps and bugs mentioned in Office 2004's AppleScript, usually with workarounds provided, will be fixed in Office 2008. If you happen to be reading this after Office 2008 has been released, you may be pleasantly surprised to discover that some of the problems discussed no longer apply. Here's hoping!



 


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!
 
 


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.