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



              end tell

The second thing is that the text frame class (in the Drawing Suite) has a text range – not text object – property to represent its Range. Generally speaking, the developers were very careful not to give properties the same name as classes, since this can cause AppleScript to become confused. (See more about this in the Entourage Chapter 6.) So various classes have a find object property of class find; a font object property of type font; and a text object property of type text range. Here somebody forgot about that: so just in case you ever wanted to run a whose filter on the text range property of text frame, or refer to it in a tell block targeted at text frame, you would need to include the keyword its. (See the Entourage chapter for details and examples.)

Lo and behold – there is one more such example of a property with the same name as a class, right here in this script. In the ReplaceInRange handler's tell block directed at the findObject, we had to say 'its replacement' because replacement is also a class name and the script would indeed become confused and would error here if we did not include its, which specifies that we really do mean the findObject's replacement property and not the application's replacement element (class) that would otherwise take precedence.

You should keep this pair of handlers handy, as in a script "library". (That's a saved script that has no top-level commands, only handlers [subroutines] and perhaps some script properties if needed.) You then need only copy and paste these two handlers into any script that needs it. Many will. (Or you need not do even that. Read up in an AppleScript book how to load script: you can then just load the script library when needed and tell it to do the ReplaceEverywhere subroutine.) For a particular text find/replace, you then just call the ReplaceEverywhere handler.

For example, to replace "hot" with "cold" everywhere in a document:

my ReplaceEverywhere("hot", "cold")

Traditionally, you put this "top level" command at the top of the script and the handlers at the bottom, but you need not: you can do the opposite. When the script compiles it "learns" where everything is. Strictly speaking, if the call to ReplaceEverywhere("hot", "cold") is not itself inside an application tell block, you do not need the my. But since most of the time you will be calling it from within another Word tell block, you absolutely need the my (which means that the script, not the application, is the "parent", so the script will not assume that the term ReplaceEverywhere is a keyword of the application, which would fail and error.) Just get used to always using my when calling subroutines and you can't go wrong.

Many Kinds of Deletions

Deleting items in AppleScript is trickier than in VBA because all the references to objects are by index and get re-evaluated every time you delete an item, so items will get skipped and the script eventually error when it gets to the final items whose indices are greater than any existing item. Therefore you must iterate backwards through items, which imposes just the one type of repeat loop that can go backwards "by -1". This aspect is mentioned in all the scripts of this section, and in greatest detail in the Delete all empty rows of a table script.

Delete all rows of a table that contain a particular text string in the first column

(by Word MVP Bill Coan)

Sub DeleteRows()

 

Dim TargetText As String

Dim oRow As Row

 

If Selection.Information(wdWithInTable) = False Then Exit Sub

 

TargetText = InputBox$("Enter target text:", "Delete Rows")

 

For Each oRow In Selection.Tables(1).Rows

If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete

Next oRow

 

End Sub

This translates to:

set cellEndChar to ASCII character 7

tell application "Microsoft Word"

     if (get selection information selection information type ¬

          (with in table)) = "false" then return

     display dialog ¬

          "Delete rows with this text in cell 1:" default answer ¬

          "Enter target text" with icon 1

     set targetText to text returned of result



< 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