• 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 13



          -- and/or checking for the error number

    else

        -- do something else

    end if

end try

lets you branch depending on what the specific error number and/or message was.

Exit Sub : return/error number -128

To quit the top level of a script, you can use return at any time. In a subroutine, that will just get you back to the main script after the line that called the subroutine. To quit the whole script, even from a subroutine, use

      error number -128

which is equivalent to clicking a Cancel button. Just check that you're not in a try block at the time. If you are in a try block, you will have to test for the error number as in the previous example, and if it is -128 invoke error number -128 a second time in the on error section.

No Collection Objects nor Add Method

The examples to this point have been examples of how to "translate" basic VBA language to AppleScript. Of course, there are also some differences in the Object Model of the Office applications due to differences and requirements of AppleScript.

One of the most important is that there are no Collection Objects in AppleScript: the concept does not exist. Quite likely, it once took you some time to get your head around the idea of single Collection Objects that somehow incorporate all existing instances of their singular "client" objects, even before there are any!

You can forget all that now. There's nothing like that in AppleScript: only singular object Classes in the Dictionary. If there are more objects – several instances, either as elements of the application or as elements of some other class – they can be considered together in a list, which is like a VBA Array. There is no special treatment for lists of application objects as opposed to, say, a list of integers {1, 2, 3, 4, 7, 97, 1456863}. It's just a list. every text range, the canonical plural listed in the dictionary for most classes, has a synonym for the plural form – e.g., text ranges. You can frequently (usually) send commands to act on the whole list at once, if the developers have implemented this feature.

Although there are no Collection Objects as such, the plural form of each element, i.e., the list of all instances of that element of any class (or of the application), does of course constitute a collection. So the way of referring to a particular element can be quite similar in AppleScript to the implicit Item method of VBA Collection Objects, but without the parentheses.

Most elements can be referred to by index: document 2, or paragraph 1 of active document, etc. (document 2 is an element of the application, which does not have to be specified explicitly.) The index may change under certain conditions, of course (elements with a lower index being deleted, for example).

So there are often alternative methods. If a class has a name property, then you can generally specify it (as an element) by name, simply following the element's class with the name (a string): document "Converting VBA to AppleScript.doc", or document property "Author" of active document. Sometimes, with built-in elements like Word styles or for applications that maintain databases like Entourage, you can specify elements by unique ID.

And there is a very powerful method – whose filters – available to almost all elements in the Office applications, and is discussed in the next section Special AppleScript Features. The old Script Editor used to inform you of every method of specification for each element of a class, but the new (OS 10.3 and on) Script Editor 2 no longer does, which is a shame. Script Debugger, however, continues to do so.

So how do you add a new instance of a class without a Collection Object to add it to? The answer is: you don't. You create a new object, almost always via the make new command from the Standard Suite:

make v : Make a new element

 

make

new type class : the class of the new element.

at location reference : the location at which to insert the element

[with data anything] : the initial data for the element

[with properties record] : the initial values for the properties of the element

—>reference : to the new object(s)

The virtually universal way is to make the new element at a location (an existing element of the application or of some other class) with properties {…record…}. (An AppleScript record is an unordered collection of key-value pairs.) If you are making a new object that is listed as an element of the application itself (check the application class entry in the Microsoft Word Suite right now, if you have not done so yet), for example if making a new document, omit the at parameter.



< 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