Applescript select text range between 2 markers

M

mikeyt

Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel Try as I may I just can't figure this one out. In my word document I have sections of text which are delimited by a markers. I want to be able to italicize the text between the markers. To do this, I presume I need to set the text range, or possibly selection, as the text between these two markers. And this has got me stumped.

I'm able to find and select the first marker, and then find and select the second marker, but how do I then select the text between these two markers.

Example text is:

"More then three centuries after the $Flying Scotsman% was brought into service, it looks like the famous name $may be back% in service."

I want to be able to italicize the text between the markers $ and %, so in the example above it would be "Flying Scotsman" and "may be back".

Any help greatly appreciated.
 
J

John McGhie

I can't do this in AppleScript, but it's very similar to the way you do it
in VBA.

You can use either the range or the selection object, then use the Extend
method:

Selection.Extend Character:="%"

That will extend the selection right until the first % sign. You need to
somehow protect against a "runaway" condition if there is any possibility
that the % sign may not be there.

However, you can do this with the Replace tool without needing script:

In Find/Replace, set $*% as your Find string.

Specify "Use wildcards" so the * matches "any number of characters" so you
are not searching for $<any characters>%

In the Replace box, specify Format>Font>Italic. Make sure the Replace box
contains no characters, and ... voila!

Hope this helps

Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel Try
as I may I just can't figure this one out. In my word document I have sections
of text which are delimited by a markers. I want to be able to italicize the
text between the markers. To do this, I presume I need to set the text range,
or possibly selection, as the text between these two markers. And this has got
me stumped.

I'm able to find and select the first marker, and then find and select the
second marker, but how do I then select the text between these two markers.

Example text is:

"More then three centuries after the $Flying Scotsman% was brought into
service, it looks like the famous name $may be back% in service."

I want to be able to italicize the text between the markers $ and %, so in the
example above it would be "Flying Scotsman" and "may be back".

Any help greatly appreciated.

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!

--

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 
M

mikeyt

That was definitely of some help, and i can find the items between markers using the wildcard. What I now want to know if it is possible to mimick the &quot;Highlight all items found&quot; feature in the applescript.

With this sample text: &quot;More then three centuries after the $Flying Scotsman% was brought into service, it looks like the famous name $may be back% in service.&quot;

I can now use this applescript to find and select the first occurrence - i would like to be able to highlight ALL occurrences though:

CODE:
tell application &quot;Microsoft Word&quot;
&nbsp;&nbsp;set selection to null
&nbsp;&nbsp;set selfind to find object of selection
&nbsp;&nbsp;set forward of selfind to true
&nbsp;&nbsp;set wrap of selfind to find continue
&nbsp;&nbsp;set match wildcards to true
&nbsp;&nbsp;set content of selfind to &quot;($)*(%)&quot;
&nbsp;&nbsp;execute find selfind
end tell

I'm looking in the dictionary for any clues as to the &quot;Highlight All Found&quot; but no luck. Anyone else know?
 
J

John McGhie

Use the Replace object to replace the text formatting. There are multiple
options on the Format tab, including bold, italic, highlighting, colours.
No marching red ants any more (thank god...) but almost everything else is
available.

Then Execute your Find with Replace All set to True :)

Cheers


That was definitely of some help, and i can find the items between markers
using the wildcard. What I now want to know if it is possible to mimick the
"Highlight all items found" feature in the applescript.

With this sample text: "More then three centuries after the $Flying Scotsman%
was brought into service, it looks like the famous name $may be back% in
service."

I can now use this applescript to find and select the first occurrence - i
would like to be able to highlight ALL occurrences though:

CODE:
tell application "Microsoft Word"
set selection to null
set selfind to find object of selection
set forward of selfind to true
set wrap of selfind to find continue
set match wildcards to true
set content of selfind to "($)*(%)"
execute find selfind
end tell

I'm looking in the dictionary for any clues as to the "Highlight All Found"
but no luck. Anyone else know?

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!

--

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top