Field text range problem

S

spaschke

Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Power PC In my applescript, I need to find out if the user has put the cursor in a field at the time the script is run.
There is no inField function and I can't seem to get a (start of / end of) text range of a field relative to the start of the document, for each field when looping through all the fields.
I realize that text ranges contain fields. This makes it confusing that I am trying to find a text range or selection object that may be in a field.

Here is a little script I was testing:
tell application "Microsoft Word"
        repeat with fieldnum from 1 to fieldcnt
                set show codes of field fieldnum to true
                set myrange to content of field code of field fieldnum of active document

if ((start of myrange) > thisRange) and (thisRange > end of myrange) then
                        display dialog "in field " & fieldnum
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (start of myrange > thisRange) and (thisRange < end of myrange) then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display dialog &quot;after field &quot; &amp; fieldnum
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (start of myrange < thisRange) then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display dialog &quot;before field &quot; &amp; fieldnum
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display dialog &quot;unknown&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end if
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--display dialog &quot;start &quot; &amp; (start of myrange) as text
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--display dialog &quot;end &quot; &amp; end of myrange as text
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end repeat
end tell

Also, has anyone gotten the command &quot;offset&quot; to work from within a tell application &quot;Microsoft Word&quot; section? It is in the dictionary, but always returns an access denied (or something similar) error.

Thanks,
Steve
 
J

John McGhie

Well, I only do VBA so I can't be much help, but check the Selection object.
It returns a Range object. The StartOf value can be matched with MyRange to
see if you are in it.

Just be aware that fields can also be in StoryRanges other than the MainText
story range (e.g. In a header or footer) and if they are, the character
position in the document will be meaningless because the field is not in the
document.

However, comparing Selection.Range.start with MyRange.start should still
operate correctly.

Hope this helps

Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Power PC In
my applescript, I need to find out if the user has put the cursor in a field
at the time the script is run.
There is no inField function and I can't seem to get a (start of / end of)
text range of a field relative to the start of the document, for each field
when looping through all the fields.
I realize that text ranges contain fields. This makes it confusing that I am
trying to find a text range or selection object that may be in a field.

Here is a little script I was testing:
tell application "Microsoft Word"
repeat with fieldnum from 1 to fieldcnt
set show codes of field fieldnum to true
set myrange to content of field code of field fieldnum of
active document

if ((start of myrange) > thisRange) and (thisRange > end of myrange) then
display dialog "in field " & fieldnum
else if (start of myrange > thisRange) and (thisRange
display dialog "after field " & fieldnum
else if (start of myrange
display dialog "before field " & fieldnum
else
display dialog "unknown"
end if

--display dialog "start " & (start of myrange) as text
--display dialog "end " & end of myrange as text
end repeat
end tell

Also, has anyone gotten the command "offset" to work from within a tell
application "Microsoft Word" section? It is in the dictionary, but always
returns an access denied (or something similar) error.

Thanks,
Steve

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

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