• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

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



< Previous Page Next Page>
 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, 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.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live