MacTech Network:   MacTech Forums  |  MacForge.net  |  Computer Memory  |  Register Domains  |  Cables  |  iPod Deals  |  Mac Deals  |  Mac Book Shelf


  MacTech Magazine

The journal of Macintosh technology

 
 

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  MacTech Forums  
  Get a copy of MacTech RISK FREE  
Google
Entire Web
mactech.com
Mac Community
More...
MacTech Central
  by Category  
  by Company  
  by Product  
MacTech News
  MacTech News  
  Previous News  
  MacTech RSS  
Article Archives
  Show Indices  
  by Volume  
  by Author  
  Source Code FTP  
Inside MacTech
  Writer's Kit  
  Editorial Staff  
  Editorial Calendar  
  Back Issues  
  Advertising  
Contact Us
  Customer Service  
  MacTech Store  
  Legal/Disclaimers  
  Webmaster Feedback  

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.

Would you like a hard copy
or PDF of this Guide?

You can get a hard copy sent to you
AND download a PDF now ($19.95)
, or

... just download a PDF ($9.95).

Either way, you get a complimentary
MacTech Magazine Subscription

courtesy of the
Microsoft Mac Business Unit


 

  Magazine Reg. Price:  $50.00 value  
  Guide Reg. Price:  $40.00 value  
  You Save:  over $80.00!  

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



 


Click here to find out more about our best subscription bundle deal ever!
2 years of the magazine, and the all new MacTech DVD ... at 70% off!



Click on the cover to
see this month's issue!

TRIAL SUBSCRIPTION
Get a RISK-FREE subscription to the only technical Mac magazine!
 
Nokia Qt Beta
 


MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797

Register Low Cost (ok dirt cheap!) Domain Names in the MacTech Domain Store. As low as $1.99!
Save on brand compatible and name brank ink jet and laser supplies.
Save on long distance * Upgrade your Computer
Movies with No Late Fees!

See local info about Westlake Village
SJ * BRJ * BJ * OJ * NITS
Staff Site Links



All contents are Copyright 1984-2008 by Xplain Corporation. All rights reserved.

MacTech is a registered trademark of Xplain Corporation. Xplain, Video Depot, Movie Depot, Palm OS Depot, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, NetProLive, JavaTech, WebTech, BeTech, LinuxTech, Apple Expo, MacTech Central and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.