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



    -- code here

end repeat

for “For Each” loops. This should work fine without problems when eachItem is an application object reference, as it usually will be. But you can often go awry if you forget that eachItem in this construction is a reference, not yet evaluated. The = equality operator will behave unexpectedly rigorously in this block:

repeat with eachItem in {1, 2, 3, 4, 5}

    if eachItem = 2 then

        beep

        display dialog (eachItem as string)

        exit repeat

    end if

end repeat

display dialog (eachItem as string)

No beep is heard, and the dialog displays 5. That's because eachItem never equals 2: it equals 'item 2 of {1,2,3,4,5}'. To avoid dealing with this issue, which you can solve by replacing the second line by

if contents of eachItem = 2 then

(or by other ways of forcing an evaluation of eachItem such as coercion), many people find it simpler just to use

repeat with i from 1 to (count {1, 2, 3, 4, 5})

    set eachItem to item i of {1, 2, 3, 4, 5}

    if eachItem = 2 then

i.e., a For…Next loop, in all cases.

Note: In VBA if you have "For i = 1 to 5 … Next i", the equivalent of this type of repeat loop, when you drop out of the loop having been round 5 times, the value of i is now 6. In AppleScript, i is 5 on exit from the loop 'repeat with i from 1 to 5'. This will surprise VBA coders who often rely on reading the value of n to see whether a loop was exited normally or by means of an Exit For statement.

exit repeat is the equivalent of Exit For, and gets you out of the block.

Check out the last subsection here – Special AppleScript Features – for two very powerful methods that can replace some repeat loops altogether.

On Error Resume Next/Go To : try…on error…end try

While not identical to VBA's On Error, AppleScript has something similar, but you must frame the section of code in which you want to trap the error with a try…end try block. A simple

try

    --code here

end try

is like an On Error Go To line after 'end try' applied to any runtime error that occurs within the block. This code:

try

    --code here

on error

    --alternate code here  

end try

provides an alternative in the event of an error, like On Error Resume Next but which can be located anywhere in your script. A more elaborate

try

    --code here

on error errMsg number errNum

    if errNum = -1708 then
        --
alternate code here quoting or parsing the message



< 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