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



As a simple example, to make a blank new document, instead of Documents.Add as you do in VBA, you

tell application "Microsoft Word"

    make new document

end tell

A great feature of AppleScript implementations is that any property that you don't specify with an initial value in the with properties {…} parameter has been given a default value by the developers. You can even leave them all unspecified, as with 'new document' (not that you'd usually want to, for other objects). For example (to save space the tell block to "Microsoft Word" will be left out in some of the examples here, but you always need it when scripting, of course):

make new table at active document

Resulting Table After “make new table” (with Show non-printing characters On)

I just ran that script on this very document and, as you can see, it entered a default table at the cursor (insertion point) with 2 rows and 5 columns, just as in the UI. This doesn't happen in VBA's Tables.Add where you must specify some required arguments (Range, NumRows and NumColumns) but it is obligatory in AppleScript that make new have default values for all properties, without erroring, so the developers have included those. Most of the time, of course, you will be specifying properties at creation time using with properties:

tell application "Microsoft Word"

    set myDoc to make new document

    set myTable to make new table at myDoc with properties ¬

        {allow auto fit:true, allow page breaks:true, column options:¬

            {default width:1.0, preferred width:25, preferred width type:¬

               preferred width percent}, number of columns:¬

            4, number of rows:12, spacing:2.0}

end tell

This sets a certain number of table properties at inception – there are a lot more available, as a glance at the dictionary entry for table will show: in fact the very same properties you can set in VBA, with very similar nomenclature, so you should feel right at home. Any properties not specified will have default values: unfortunately neither the dictionary nor the Word AppleScript Reference tell you what the defaults actually are in this case, but then again, neither does the VBA Help for Table Object (which is what the Reference draws on for its information). So you'll have to test them out, just as you did in VBA.

Note that the number of columns and number of rows properties (which actually correspond to the NumColumns and NumRows parameters of the Tables.Add method rather than to any Property of Table Object in VBA) allow you to set these table dimensions at inception (i.e., only in a make new table statement, not later on) even though the dictionary discloses that they are [r/o] read-only properties. After the table exists, you can add new elements – rows and columns – at the table:

make new row at end of table 1 of active document

You can specify locations (insertion points) for adding elements at the beginning, at the end, or after or before an indexed element:

make new row at after row 5 of table 1 of active document

and you can delete indexed rows or columns too.

None of this is special to Word, by the way: make new [object] with properties applies to most classes in Excel and PowerPoint (and every other scriptable application on the Mac) as well. Occasionally, though, with the Office apps (almost uniquely) it cannot be done that way. This will be because of some peculiarity in the OLE Automation/VBA heritage that just couldn't be adapted to standard AppleScript usage by the developers.

In those cases, you will find "proprietary" commands – usually beginning with the word create (rather than make) – that do the job. (In Word, there are seven commands in the Microsoft Word Suite beginning create, of which the one you'll use the most often is create range.) Be on the lookout for these create and other proprietary commands if you run into any make new errors. Traditional AppleScripters can be unnerved by these, but veteran VBA macro writers should not be put out since they in fact correspond more closely to VBA Methods than does make new [object].

Note: The one important exception to the with properties parameter is when making a new document in Word: in this case you cannot specify the properties at inception (they will be ignored) and must set them immediately after making the document, just as with the Add Method in VBA. This is discussed in more detail in Chapter 4: Word (New Document). Excel does not have this problem when making a new workbook. (In PowerPoint, no properties as such of the presentation can be set, even after making one, not even properties that are meant to be settable, so the issue is not relevant.)

Another exception of sorts is that some Collection Objects in VBA are read-only, where you cannot make new members. So these have not been implemented as elements in AppleScript, since there are no "read-only elements" (as opposed to read-only properties) in AppleScript: you can always make new elements. (The important distinction in AppleScript between properties and elements is discussed in Chapter 1's More about Dictionaries section.)



< 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