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 35
In VBA, you can do this, as in the macro we're converting:
Set myRange = ActiveDocument.Paragraphs(1).Range
AmountMoved = myRange.MoveEnd(unit:=wdParagraph, Count:=1)
That sets myRange to the Range of the first paragraph, and then uses MoveEnd to move the end another paragraph on. Again, it does so "in place" – there's no need to redefine myRange, it's still there as a dynamic reference. So the MoveEnd Method has the "luxury" of being able to return the integer representing the number of characters this move has advanced.
In AppleScript, we do not have to create our own range this time. We can use move end of range on a text range set to the text object of the first paragraph, without crashing. However we still cannot modify the range "in place". Exactly as in the previous example, we have to set myRange, or another variable, to get hold of the new range returned by created by move end of range:
tell application "Microsoft Word"
set myRange to text object of paragraph 1 of active document
set myRange to (move end of range myRange ¬
by a paragraph item count 1)
end tell
That's fine. But the necessity of the command to return the new range as the result means it cannot return the amount moved, as we have seen earlier. That is why so much of the script version is concerned with calculating this amount moved, which is not at all difficult (getting and redefining rangeEnd after every move) but a bit of a bother. As we shall see shortly, there is a simpler way to do it given the necessary difference in the AppleScript command from the VBA version..
Also, in the macro we're converting, at the end of the document you have to keep alert. In VBA when you try to do the final MoveEnd on the last paragraph mark, it doesn't error but simply returns 0. In AppleScript, move end of range also doesn't error but returns missing value (for a nonexistent new range that can't be made); that's a sort of null value. But the next line that attempts to get end of content of a non-existent range – that of course errors.
So we trap the error in a try/on error block and arbitrarily set the amountMoved variable to 0. (This is perhaps a somewhat silly way to do it, but I did so in order to keep the structure the same as for the VBA macro and to demonstrate that AppleScript has a repeat while loop too, although it's not used too often.)
You could re-do the script keeping all the move end and move start commands (and of course the delete) but simply omit all the get end of content and the amountMoved calculations: just leave all that out and depend on the trapped error at the end to close the script in a simple repeat loop with no while. New improved version, optimized for AppleScript:
tell application "Microsoft Word"
--start with first paragraph and extend range down to second
set myRange to text object of paragraph 1 of active document
set myRange to (move end of range myRange by a paragraph item count 1)
--loop until there are no more paragraphs to check
repeat
--if two paragraphs are identical, delete second one
--and add the one after that to myRange so it can be checked
if content of text object of paragraph 1 of myRange = ¬
content of text object of paragraph 2 of myRange then
delete text object of paragraph 2 of myRange
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine