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



    

     set theRows to every row of table 1 of selection

     repeat with i from (count theRows) to 1 by -1

          set theRow to item i of theRows

          if content of text object of cell 1 of theRow = ¬

              (targetText & return & cellEndChar) then

              delete theRow

          end if

     end repeat

end tell

Notes: What VBA knows as Chr(7), AppleScript knows as ASCII character 7 (and old teletype operators knew as "Bell"). It's an invisible character that is used by Word as a table-cell-end character following a carriage return, known as vbCr or Chr(13) in VBA and return or ASCII character 13 in AppleScript. You can see a visual representation if you turn on Show Non-Printing Characters in Word's Preferences or standard tool bar. ASCII character is a command in the Standard Additions dictionary (Apple's built-in collection of scripting additions).

There is always a certain overhead that slows down a script when calling scripting additions, so you don't want to call it over and over in a repeat loop. That's why we set a variable to it at the top of the script: it's called just once this way. Similarly, you normally always want to use AppleScript's three constants return, space and tab rather than their equivalent ASCII character commands (13, 31, 9). (In the old Word AppleScript of Word X and earlier, you could not use tab in Word tell blocks: it was always interpreted as Word's tab class. In 2004, they were clever to do away with that conflict: the class is now called tab stop, and tab on its own is the usual text character.)

AppleScript is also clever in never confusing the return character with the return command. You can see the difference in the last few lines of the script above between the light blue regular-weight return (the character) and the dark blue bold weight for the return command (formatted as a Language keyword).

You will notice the difference between the AppleScript repeat loop and VBA's For Each loop. In AppleScript, if we use the 'repeat with theRow in the Rows' syntax (or more literally 'repeat with theRow in (every row of table 1 of selection)') AppleScript actually keeps track of the count by indexing the list exactly as in a 'repeat with i from 1 to (count theRows)'. It constantly checks, or refreshes, the list of items but not the index, on every iteration. So any time you use a delete loop to delete items, you must use the form above where you iterate backwards from (count theRows) to 1 by -1.

Otherwise, every time you delete an item the next iteration skips one (Say you delete item 1, then "item 2" is now the original item 3, and the original item 2, now "item 1", gets skipped. Every second item gets skipped this way, and pretty soon you hit an error when the index is larger than the last remaining item.) Always iterate backwards when deleting: that way all as-yet-uninspected items keep their original index to the end.

Finally, note that the with in table (note the separate words "with in") information type does not return the booleans true and false but the strings "true" and "false".

Delete duplicate paragraphs using a Text Range object

Dim AmountMoved As Long

Dim myRange As Range

 

'start with first paragraph and extend range down to second

Set myRange = ActiveDocument.Paragraphs(1).Range

AmountMoved = myRange.MoveEnd(unit:=wdParagraph, Count:=1)

 

'loop until there are no more paragraphs to check

 

Do While AmountMoved > 0

 

'if two paragraphs are identical, delete second one

'and add the one after that to myRange so it can be checked

 

If myRange.Paragraphs(1).Range.Text = _



< 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