

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.
|
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>

- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine