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