Re: Offset in paragraphs
Re: Offset in paragraphs
- Subject: Re: Offset in paragraphs
- From: Richard 23 <email@hidden>
- Date: Thu, 16 Nov 2000 19:48:01 -0800
Stephen Fitchett <email@hidden> wrote the following on
Fri, 17 Nov 2000 10:04:24 +1200 (although it was still Thursday as
I read the time coordinates at my location):
>
Is there any way of getting an offset of something in paragraphs, not
>
characters, preferably without any third party OSAX? I'm making a
>
conversation simulater in AppleScript and I need to get the offset in
>
paragraphs to generate a reply properly.
>
>
I'm using AppleScript 1.1.1 but will have 1.1.2 in a week or so.
This is straightforward using Tex-Edit Plus (probably Style as well).
You may want to use a scriptable text editor to save the output of
such a script if it gets to the point where it's a snappy
conversationalist!
tell application "Tex-Edit Plus"
tell document 1 to get offset of paragraphs
end tell
If you prefer not to use an application either, it's still possible.
I have my own redefinition of offset which will return multiple offsets
when calling offset and passing the substring to find as a list.
First the handler, then a sample of its use:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-- ----------------------------------------
-- offset performed with delimiters
-- pass subStr as list for list of offsets
-- (c) 2000 by Richard 23
-- ----------------------------------------
property |t/d| : a reference to AppleScript's text item delimiters
-- ----------------------------------------
on offset of subStr in theStr
set {isList, subStr, theList} to {subStr's class = list, subStr as
string, 0}
if theStr contains subStr then
set {|t/d|'s contents, subLen} to subStr's {it, length}
set {|t/d|'s contents, subList} to {"", theStr's text items}
set theList to {(length of subList's first item) + 1}
if isList then tell subList to repeat with theIdx from 2 to
length - 1
set end of theList to result + subLen + (item theIdx's length)
end repeat
end if
theList as item
end offset
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
NOTE: if you object to the |t/d| property as I know some people do,
simply replace each occurrence of
"|t/d|'s contents"
with
"AppleScript's text item delimiters"
Also note this is just a preliminary handler I copied from a recent
script. It isn't really what I consider to be my final word on the
matter!
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
set theStr to "this is a test
of offset
it's a redefined command handler
it might be faster since there's no osax calls
"
offset of return in theStr
--> 15
offset of {return} in theStr
--> {15, 26, 27, 60, 61, 108}
-- if you consider a blank line to indicate next paragraph:
offset of {return & return} in theStr
--> {26, 60}
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
If this helps, great! And please send me a working script if it
turns out well. Always interested in AI stuff.
You may want to use Apple's Text To Speech in such a project.
If so, you should take a look at my preliminary speech library
which helps correct pronunciation of known problematic words and
is easily extensible.
http://homepage.mac.com/richard23/
I think it's on page 1, and is called "Speech Cleanup"
I have developed some handlers for using predefined messages
which can contain "*" to indicate substrings to fill in at runtime.
These will be included in the next version of the speech library.
Contact me if you would like to get these before I get around to
uploading it.
Since I rarely get any feedback from people who download scripts
from my site I don't know how to prioritize what I update and how
soon I update the site. So if anyone uses any of my scripts on a
semi-regular basis please let me know so I at least have an idea of
what you like and what I should pay attention to. And please send
bug reports. I usually end up catching them all myself or they stay
in the uploaded scripts. That's no good!
If anyone finds the inevitable bug or incompatibility in one of my
scripts, please please please email me so I can fix it.
I really am trying to be helpful with the site y'know!
R23