Another option is with text item delimiters
set instring to "This is some _boring_ text"
set repstring to "EXCELLENT"
set tid to applescript's text item delimiters
set applescript's text item delimiters to "_"
set outtext to text item 1 of instring & repstring & text item 3 of
instring
set applescript's text item delimiters to TID
outtext
Not exactly what you were looking for but it works.
It can even be expanded.
set instring to "This is some _boring_ text. With _multiple_ replaces."
set reptext to {"EXCELLENT","no"}
set tid to applescript's text item delimiters
set applescript's text item delimiters to "_"
set itemlist to every text item of instring
set outtext to ""
set applescript's text item delimiters to TID
repeat with i from 1 to count itemlist
if i mod 2 = 1 then
set outtext to outtext & item i of itemlist
else
set outtext to outtext & item (i div 2) of reptext
end
end
furthermore....
set instring to "This is some _boring_ text. With _multiple_ _out of
order_ replaces."
set reparray to {"out of order", "ordered or unordered", "boring",
"EXCELLENT", "multiple", "no"}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
set itemlist to every text item of instring
set outtext to ""
set AppleScript's text item delimiters to tid
repeat with i from 1 to count itemlist
if i mod 2 = 1 then
set outtext to outtext & item i of itemlist
else
repeat with j from 1 to ((count reparray) div 2)
if item (2 * j - 1) of reparray = item i of itemlist then
set outtext to outtext & item (2 * j) of reparray
exit repeat
end if
end repeat
end if
end repeat
-Mark
Feeling pedantic and liking it.
On Sep 18, 2008, at 4:56 PM, Mark J. Reed wrote:
> Well, there's always the brute force approach:
>
> set theText to (text begin through (beginItal-1) of theText) &
> newString & (text (endItal+1) through end of theText)
>
> On Thu, Sep 18, 2008 at 4:19 PM, RJay Hansen
> <[log in to unmask]> wrote:
>> Thanks for the suggestion Mark. Unfortunately, that generates the
>> same
>> error--"Can't set text 23 thru 32 of theText...etc."
>>
>> RJay
>>
>>
>> On Sep 18, 2008, at 2:54 PM, Mark J. Reed wrote:
>>
>>> If theText is
>>>
>>> Here's some text with underscores (_like this_).
>>>
>>> Then the first underscore is at text position 36, but is contained
>>> within word 6. I'm guessing that beginItal holds the former value,
>>> not the latter, so you want "set text beginItal thru endItal of
>>> theText", not "set words ...". (Setting words would also replace
>>> the
>>> entire words "(_like" and "this_)" rather than leaving the
>>> parentheses
>>> alone.)
>>>
>>> On Thu, Sep 18, 2008 at 3:16 PM, RJay Hansen
>>> <[log in to unmask]>
>>> wrote:
>>>>
>>>> I'm writing a script that's intended to parse out text that is
>>>> enclosed
>>>> within underscores (_like this_). I'm using Tex-Edit Plus and
>>>> Smile.
>>>>
>>>> I'm putting text selected in Tex-Edit Plus into a variable then
>>>> getting
>>>> what
>>>> I need working with the text in the variable. Once I have it, I
>>>> want to
>>>> replace the string surrounded by the underscores (and including the
>>>> underscores) with the new string using this:
>>>>
>>>> set words beginItal thru endItal of theText to newString
>>>>
>>>> beginItal and endItal are the indexes of the underscores. theText
>>>> is the
>>>> variable that the selection from the document is read into and
>>>> newString
>>>> is
>>>> of course, the replacement string.
>>>>
>>>> Smile comes back with "Can't set words 23 thru 32 of theText (it
>>>> puts the
>>>> actual string in the error message/not the variable name as shown
>>>> here)
>>>> to
>>>> newString (and again, it puts the new string in the error
>>>> message, not
>>>> the
>>>> variable name)".
>>>>
>>>> I haven't been able to figure out why I can't do this. Any help
>>>> would be
>>>> appreciated.
>>>>
>>>> Thanks,
>>>>
>>>> RJay
>>>>
>>>
>>>
>>>
>>> --
>>> Mark J. Reed <[log in to unmask]>
>>
>
>
>
> --
> Mark J. Reed <[log in to unmask]>
>
|