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 3
Another example:
temp = Options.DefaultFilePath(wdUserTemplatesPath)
in VBA becomes in AppleScript:
set temp to get default file path file path type user templates path
(Note in the latter example a departure from the VBA model where DefaultFilePath is a Property of the Options object while in AppleScript get default file path is a command with a file path type parameter, and user templates path one of the enumerated values it can have.)
Regular AppleScripters can be put off by these long run-on constructions found in Office 2004 scripting: there‘s nothing very English-like about it, without the usual prepositions at the beginning of parameter names (or else "–ing" participles on verbs) that would improve readability. At times, the enumerated values repeat words found in the parameter names, which in turn may repeat words found in the command name, and they seem to run together. Regular VBA scripters will be pleased to see familiar terms appearing in the AppleScript version, and will soon adapt to the lack of dots and other punctuation. Most writers of Office VBA macros should become accustomed to Office AppleScript quite easily.
Dictionaries
So, where are these Word-specific AppleScript terms to be found? Don‘t go looking in the Help menu, as you might in the VB Editor. Script Editor‘s Help knows nothing about Word or Excel or any of the hundreds of other scriptable applications. Look in the File menu of the Script Editor, at the "Open Dictionary…" menu item. Select "Microsoft Word" in the list that comes up (or click the Browse button if you can‘t find it, and browse to it in /Applications/Microsoft Office 2004/ via a normal Mac OS "Open" navigation box).
You will now see the Word AppleScript Dictionary, with the hundreds of classes and commands available. They are divided into Suites of associated terms: don‘t forget to check more than one suite (you‘ll usually start with the Microsoft Word Suite) if you don‘t find what you‘re looking for.
In particular, always remember that text range – corresponding to Range Object in VBA – is a class in the Text Suite. (But in Excel, the range class is in the Table Suite. Go figure.) Or if you have a good idea at what the term you‘re looking for, or part of the term, might be called in AppleScriptese, just enter it in the Search box and look through the results.
You‘ll soon figure out the color-coded "C", "P" and other icons for "Command", "Class", "Property", etc. also spelled out in the Kind column. (If you‘re treating yourself well and have Script Debugger, there are more results including "F" for Functions, i.e., Commands returning results, "#" for enumerations, and so on, and clearer descriptors.)
The Dictionary entries show you all the properties and elements of classes, e.g.,
footnote n [inh. base object] : Every footnote
elements
contained by documents, selection objects, ranges.
properties
entry index (integer, r/o) : Returns the index for the position of the object in its container element list.
note reference (text range, r/o) : Returns a text range object that represents a footnote mark.
text object (text range, r/o) : Returns a text range object that represents the portion of a document that's contained in the footnote object.
The current version of Script Editor shows you which classes the class in question is an element of – i.e., is contained by – as well as any elements it itself may contain – here none. For those who can afford to splurge (or check the Demo), Script Debugger makes the hierarchy clearer. Note that the description of each property begins with the data type of the property, e.g., integer (a basic AppleScript type), text range (another Word class), and so on. Other classes, such as text range here are clickable links to their own entries in the Dictionary, which is useful.
If the properties are read only, they are marked as r/o. (Tip: some r/o properties can actually be set at the time of creation only, when using the 'make new [object] with properties {…}' command. In other words, it‘s always worth a try to see if it‘s possible.) When making a new object, there are default values for every property, so they do not have to mention it if default. The dictionary will usually, but not always, tell you what the defaults are, especially for boolean properties (true or false values).
For commands, the Dictionary shows you any parameters and whether a result is returned or not:
undo v : Undoes the last action or a sequence of actions, which are displayed in the undo list. Returns true if the actions were successfully undone.
undo document
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine