Why can’t I select a range of paragraphs in MS Word by using “thru”, like so:
tell application "Microsoft Word"
tell document 1
set the_paragraphs to paragraphs 1 thru 3
end tell
end tell
Must I use a repeat loop to do what I want with the paragraphs ?
More generally, is the usage of “thru” reserved to Applescript objects, such as text strings and lists?
How do I select a range of objects in an application?
Paragraphs is an object in Word that can’t be filter. So when you get all the paragraphs (word object), convert it to a list and then use the standard AppleScript thru object specifier you can get the right paragraphs:
tell application "Microsoft Word"
tell document 1
set the_paragraphs to items 1 thru 3 of (paragraphs as list)
end tell
end tell
I don’t have Word, but . [Edit: Guess here deleted, and the following modified, in the light of Shane’s comments two posts down.]
No. With application objects too, ‘thru’ is commonly used when specifying ranges of elements. But it’s up to the application’s developers to implement this and apparently (and unbelievably!) ranges aren’t implemented in Word, even with elements of text.
tell application "Microsoft Word"
tell document 1
paragraphs --> {paragraph 1 of document 1, paragraph 2 of document 1}
-- looks like a list, but is it?
item 1 of paragraphs --> current application
-- apparently not...
-- try something else:
item 1 of result --> paragraph 1 of document 1
end tell
end tell
tell application "Microsoft Word"
tell document 1
set x to words 1 thru 3
class of x
--> Error: Can't get word 1.
return paragraph 1
-->paragraph 1 of document 1 of application "Microsoft Word"
count of paragraphs
--> 21
set y to paragraphs 1 thru 3
--> Error: Microsoft Word got an error: The object you are trying to access does not exist
end tell
end tell