Re: position of text
Re: position of text
- Subject: Re: position of text
- From: "Marc K. Myers" <email@hidden>
- Date: Thu, 25 Apr 2002 03:31:48 -0400
- Organization: [very little]
>
Date: Wed, 24 Apr 2002 22:23:16 -0400
>
Subject: dd: position of text
>
From: dave dowling <email@hidden>
>
To: email@hidden
>
>
a text function question for you AS gurus out there:
>
>
how can i get the position of a string in a text block? for instance:
>
>
set myTextBlock to "blah blah blah Lucy blah blah blah want a
>
treat? blah blah blah Lucy."
>
set myString to "Lucy"
>
>
i can get the string "Lucy" from myTextBlock as a list by using
>
(word 3 of myTextBlock as list) or something like that. but how do
>
i say to AS, "Give me the index of the word myString in the list
>
myTextBlock", where the result is "3", since the string "Lucy" is
>
the 3rd word in myTextBlock. furthermore, how do i distinguish
>
between the first iteration of the string "Lucy" and the 2nd one,
>
assuming i need to manipulate one but not the other?
>
>
also, consider this similar scenario:
>
>
set myURL to "http://www.apple.com/macosx/"
>
set myString to "macos"
>
>
supposing i want to change the string "macosx" to the string
>
"macos9" in myURL. i'd like the position (index) of the letter to
>
be replaced (the letter 'x' in this case). how do i isolate a
>
string or character's index?
>
>
i know i could write a looping script that will locate the
>
string/char, but i'd like to know if there's an existing function
>
in AppleScript's stock library before i take this route. i didn't
>
see anything in the Language Guide, but maybe i missed something?
Isn't "Lucy" the fourth word in the text?
You could say "set x to offset of myString in myTextBlock", which would
give you the index of the first character. Akua Sweets provides you
with "collect the items of" which returns the indices of matches in a
list of text strings. The use of a scriptable text editor (I use
Tex-Edit Plus) makes this kind of stuff very easy. For search and
replace the easiest thing to use is AppleScript's text item delimiters.
You set the delimiters to the search string, get the text items of the
text (a list), set AppleScript's text item delimiters to the replace
string, and convert the list back to text. This replaces all
occurrences of the search string and is extremely fast.
I hope this helps.
Marc K. Myers <email@hidden>
http://AppleScriptsToGo.com
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074
[4/25/02 3:30:41 AM]
_______________________________________________
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.