• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

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!



< Previous Page Next Page>
 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, 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.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live