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 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>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine