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 5
Microsoft Office AppleScript Resources
As you can see, some of the definitions are pretty sparse. The format for AppleScript dictionaries and their definitions is determined by Apple. The new "sdef" type of Dictionary (you‘ll see that Word‘s is titled "Microsoft Word.sdef") as used by Script Editor 2 in Panther OS 10.3 and later, in Smile 3 and in Script Debugger 4, gives a lot more scope than the previous type of dictionary seen in OS 10.2 and earlier, but still nowhere near the scope of Visual Basic Editor‘s Help.
If you‘re a real VBA expert, you may not need the longer descriptions and examples for terms you‘re translating to AppleScript. But they are available. Microsoft has provided voluminous AppleScript Reference Guides for Word, Excel and PowerPoint, free, at the Developer Center on their MacTopia website: <http://www.microsoft.com/mac/resources/resources.aspx?pid=asforoffice>.
These are truly invaluable resources: 500-page PDF manuals in the case of Word and Excel, a little smaller for PowerPoint, that not only give fuller descriptions and examples of usage for every term in the dictionaries, but essential introductory essays explaining how to script important aspects. For example in the Word AppleScript Reference, there is a full discussion, with examples, of using ranges. The main body of each Reference consists of the application's full VBA Help adapted to AppleScript, sometimes with unique AppleScript-only information in addition. These Reference guides will be your most important resources for scripting Office.
For further help, when you get stuck, write to the appropriate Microsoft Mac office newsgroup:
microsoft.public.mac.office
microsoft.public.mac.office.entourage
microsoft.public.mac.office.excel
microsoft.public.mac.office.powerpoint
microsoft.public.mac.office.word
These can all be accessed either by subscribing via the Microsoft News Server (msnews.microsoft.com) in Entourage (you‘ll find the Microsoft News Server near the bottom of your Folders List: click on it, enter ".mac." In the Search Box in the main window, and cmd-click the newsgroup(s) above, then click the Subscribe button) or any other newsreader; or via the "Browser" links to Google groups in Safari or another browser at <http://www.microsoft.com/mac/community/community.aspx?pid=community>. Using a newsreader is best as your question will appear there a few seconds after posting, while Google groups in a browser can take several hours before they appear.
Another place to request AppleScript help (particularly help not specific to the Microsoft Office apps) is by joining the AppleScript-Users Mailing List run by Apple at <http://lists.apple.com/mailman/listinfo/applescript-users>.
Compiling, Saving and Running Scripts
Compiling Scripts
AppleScript is pre-compiled before saving, and formats the different "parts of speech" (uncompiled text, operators, language keywords, application keywords, comments, values, variables and references) with distinct fonts and colors when compiling. In Script Editor, if you go to Script Editor menu/Preferences/Formatting, you can see the colors and fonts. You can even change them, if you wish. When you type new code, or if you are reading this on-screen and can copy and paste the examples in these articles, newly entered code is always seen in the uncompiled formatting, e.g.,
tell app "Microsoft Word"
save as active document file name myDocName file format format text
end tell
If your script is written with proper syntax, clicking the Compile button (or select Script/Compile or press cmd-K, or simply Enter), it will compile like this:
tell application "Microsoft Word"
save as active document file name myDocName file format format text
end tell
Compiling sets the formatting. (AppleScript has quickly looked up terms in its own language dictionary and parser, in the application dictionary of the 'tell' block, and in installed scripting addition dictionaries, worked out which terms are AppleScript keywords, application keywords, quoted text, etc. and made anything that is "none of the above" into variables, as long as your syntax has no errors.)
Compiling also sets the indentation within "blocks", which is very nice of it, if there is no error. If there is a syntax error, the script will not compile, and you will get an error message. AppleScript error messages are notorious for being incomprehensible, but most of the time, it will be one or the other of two problems: either
1. You have made a typographical error, so what should be a language or application keyword instead is considered a variable, with the words following (or even preceding, if you mistyped the last or middle word of a multi-word keyword) also variables or keywords in a statement that now makes no sense; or
2. You have pasted some code from a document or email message where some long lines hard-wrapped to a new line, making both syntactically wrong. In the latter case you need to remove the infiltrating carriage return or else add a continuation character (¬ , made by typing option-L) to the end of the first, broken line.
If you‘re writing your own script, you may simply have got the syntax wrong, of course. Even if you‘ve pressed the "Compile" button without problem, with all keywords correctly typed, it is still possible that the syntax is meaningless and you will get an error in execution when running the script.
Running Scripts
Finally, when you‘ve got a script working, how do you save it and run it? Of course it can be test-run from Script Editor by clicking the Run button, but you will want to save it and run it more conveniently when you‘re in Word. Unlike VBA macros, you cannot save a script in the document itself (more on this in the next section). You have the choice of saving a script as a self-standing Application (or Application Bundle, the way ahead) or as a Script (or Script Bundle), by flipping the File Format popup in Script Editor‘s Save dialog. (You can also save as Text, useful when you have to interrupt your work with a script that won‘t yet compile, but no such text will execute: it must be saved as Application or Script to run.)
A script application might be useful when creating a workflow involving several applications, or when what you want to save is a "droplet" on which you can drop files. (you effect this by including an 'on open' subroutine and saving as an Application. Discussed in more depth in any of the AppleScript books.) But script applications have the same liability as any application: there is a delay of a few seconds after the file is double-clicked while the application launches, bouncing up and down in the Dock until it is ready to go. By contrast, saving as a Script (which adds the .scpt extension) carries no such overhead when run from a suitable script runner such as a script menu: it just runs straight off, just like macros.
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine