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 10
People working on large-scale VBA projects, particularly projects involving several collaborators, may be accustomed to using multiple VBA Modules controlled from a "central" module with global variables passed down to subsidiary modules written by different collaborators.
A similar modular structure is available in AppleScript using script objects. Script objects are often considered to be at the advanced end of AppleScript since they are not needed much in basic scripting, but they are fundamental to how AppleScript works. All of the AppleScript books discuss script objects: the Neuburg book is particularly good on script objects and discusses them early on.
The script files – .scpt and .app – in which you save your scripts are one form of script object. As such, they may be loaded by other scripts using the load script command in the built-in Standard Additions collection of scripting additions. All their script properties, variables, and handlers become available to the calling script, which may also set its own script properties and variables to these for convenience. So one script can act as a master script, loading the collaborators' script files as script objects to be used in just the same way as with VBA Modules.
Read up on script objects in the Neuburg and/or Rosenthal books (see Chapter 7: Resources for URLs) if you need to learn more about them.
What About Dim?
In AppleScript, variables are not declared by data type. In fact, they do not have to be declared at all unless you wish to make them global variables – whose scope applies in every handler as well as in the top-level script – or script properties, usually called just properties, which are similar to globals but also have their initial value defined and are persistent between script runs. You declare variables global simply, like this, and preferably at the top of the script:
global var1, var2
You can run several globals together on the same line, separated by commas. Properties are declared and defined like this, at the top of the script, each on its own line:
property prop1 : "whatever"
property prop2 : 3
You may choose to declare "ordinary" variables (i.e. not specifically declared as global or properties) as local, but are not required to: they are local by default whether you declare them as such or not. They are defined only for the handler or the top-level script in which they occur, and remain unknown to other handlers. However, such variables defined at the top level of a script without a declaration, although local to all intents and purposes, have certain quirks (namely retention) that are discussed in detail in the Neuburg book, which is especially good on scope (i.e. the realms within a script where terms and keywords of various types hold sway) .
You can happily change the value of a variable to one of another data type without problem:
set a to 3
set a to "camel"
tell application "Microsoft Word" to set a to paragraph 1 of active document
So you can forget all about Dim now… You also do not need to reset variables whose value is an application reference to Nothing or similar at the end of a script: i.e., there is no need to release object references. Both of these easy practices are ultimately due to the fact that AppleScript has automatic garbage collection and cleans up after itself – you do not need to worry about memory issues or disposal.
if/else/then, tell, repeat, try and other control blocks
If…Then…End If : if…else…then…end if
Most indented "blocks" in VBA have AppleScript equivalents. if…then…end if is identical (once you get used to the lower-case characters) to VBA's If…Then. One feature of AppleScript's "if" syntax is that if the consequent code is just one line you can optionally put the whole thing on a single line rather than in a block, omitting the end if. E.g.,
if x = 3 then
set y to 4
end if
can be written (if you wish) instead as
if x = 3 then set y to 4
if…else if…else if…else…end if is identical to VBA's (note the separate words "else" "if"). There is no Select Case control statement as such: use if…else if instead.
With…End With : tell…end tell
The equivalent of VBA's With…End is tell…end tell targeted at an application object:
tell application "Microsoft Word"
set textObject to the text object of active document
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine