• 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 50



          set name of active sheet to sTryName

     end try

end tell

I've given this script a script property declaration at the top, more nearly equivalent to the CONST declaration that many of these VBA macros have, although it's not strictly necessary. It does draw attention to the definition of sBASENAME, however, more than a simple

set sBASENAME to "MySheetName"

at the top would, so if you want to change the name to something else later it's very clear where to do it, as with CONST declarations. I say it's "not strictly necessary" since you're not taking advantage of the main virtue of script properties – persistence between runs (which you would do in VBA by a very different method) – and thus is no more effective for the purpose of having a single place to make the change than a regular set assignment, but certainly makes the definition clear. Note that the property declaration is placed above and outside the Excel tell block.

Error trapping, especially in connection with repeat loops, are a little different in AppleScript since the try/on error/end try structure has to be carefully positioned, unlike the freer On Error Resume Next manner of VBA. You have to work out whether your try/end try block needs to be around the repeat block, or within it, or whether you might need two of them. You might also need an exit repeat within an if/end if block inside the repeat block; we don't need that this time.

Another way to do it in AppleScript without needing a try/error block uses the exists command found in the Standard Suite and implemented by most applications, including Excel:

property sBASENAME : "MySheetName"

 

tell application "Microsoft Excel"

     tell active workbook

          set i to 1

          repeat until (worksheet (sBASENAME & " (" & i & ")") ¬

              exists) = false

              set i to i + 1

          end repeat

          make new worksheet at end with properties ¬

              {name:sBASENAME & " (" & i & ")"}

     end tell

end tell

This is actually simpler and clearer to read, with the added advantages of not needing extra variables nor those extra lines required to force errors. It does involve "thinking in AppleScript" rather than merely "translating" VBA literally, and is the sort of thing you should start doing as you become more familiar with the language.

One unusual thing about some newer scriptable applications like Excel, Word, PPT and the Apple iApps – less so with "traditional" AppleScriptable applications like Entourage – is that setting a variable to something non-existent often does not in itself necessarily cause an error. The error occurs when you next use the variable. That is why you have to immediately follow both statements setting ws to a worksheet that may not exist with an immediate call to the variable – it's an implicit get – to force the error.

You must check when writing your scripts if such a call is necessary, or else just do it as a matter of course. Do not assume an error will occur otherwise. If not, an error may not appear until later in the script (the first time you need to access ws) at a point where you have not trapped specifically for this one: it could be something else.

Next – in AppleScript there is no default value 0 for an increment variable, as in VBA: the variable is undefined. If you write

   set i to i + 1

without first setting i to 0, the script will error. We need to initialize i to 0 before the repeat loop begins, of course.

There is no equivalent to VBA's Format function in basic AppleScript. Many third-party scripting additions provide equivalent functions, and AppleScript has full access, via the built-in do shell script command in Standard Additions, to all the very powerful Unix tools that can probably out-perform Format if needed. On this occasion, it's a very simple matter just to concatenate the incrementing variable i into a string expression between two parentheses. A string to the left of the concatenation operator & coerces a number on its right to a string, so there is no need for an explicit coercion as string.

Because an error will be forced when you arrive at a sheet name that does not yet exist, taking you right out of the repeat loop into the on error block outside it, there is no need for any sort of while or until condition in the repeat statement nor for an exit repeat in an if block anywhere. You are guaranteed to exit straight into the on error block, and thence to the end of the script every time. It makes life a lot simpler sometimes having structured try/on error blocks!



< 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