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.
|
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>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine