Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: deleting text



on Wed, 26 Nov 2003 09:24:31 -0500, "Ruby Madraswala"
<email@hidden> wrote:

> Thanks Kai, I was able to take couple of lines out of my code. Once the
> deleting slash and pipe worked, I decided to delete some more special
> characters, and I had it searching and replacing one character at a time.
>
> Now that I have deleting working, do I use the same concept to add any special
> characters to a word?

You might, Ruby - since you can use text item delimiters to carry out the
equivalent of a search and replace routine. Here's a very general example:

------------------------------

to changeText of t from a to b
set d to text item delimiters
set text item delimiters to a
set t to t's text items
set text item delimiters to b
set t to t as string
set text item delimiters to d
t
end changeText

set txt to "This is the car I bought yesterday."
changeText of txt from "car" to "carriage clock"

--> "This is the carriage clock I bought yesterday."

------------------------------

However, this will also change the specified text within a word, too - which
might result in some unexpected nonsense:

------------------------------

set t to "I took my car to the escarpment."
changeText of t from "car" to "carriage clock"

--> "I took my carriage clock to the escarriage clockpment."

------------------------------

To a certain extent, the most appropriate method will depend on context. For
example, here's a rough-and-ready way of adding a string to only individual
words within some text:

------------------------------

to addString of b to a at t
set d to text item delimiters
set w to t's words
set c to count w
set l to {}
set s to 1
repeat with n from 1 to c
if w's item n is a then
set l's end to t's text from word s to word n & b
set s to n + 1
end if
end repeat
if c >= s then
set l's end to t's text from word s to -1
set e to ""
else
set text item delimiters to w's last item
set e to t's last text item
end if
set text item delimiters to space
set t to (l as string) & e
set text item delimiters to d
t
end addString

set txt to "What kind of hat is that? I hate that hat!"
addString of "chet" to "hat" at txt

--> "What kind of hatchet is that? I hate that hatchet!"

------------------------------

Note that, if the number of words exceeds 4060-4070 (it varies a bit), this
method might run into stack overflow problems - though there are
workarounds.

If you get stuck with whatever it is you're trying to do, feel free to throw
some specific examples back...

---
kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.