Re: search and count
Re: search and count
- Subject: Re: search and count
- From: Emmanuel <email@hidden>
- Date: Mon, 18 Nov 2002 18:09:44 +0100
At 9:21 AM -0500 18/11/02, Ruby Madraswala wrote:
I need help with a script. I want to:
1. open a text file. (Is simple text scriptable?)
2. search for a word "Data" always the first word of a line.
3. if found count number of pipes "|" in that line.
4. pass the count to the script.
Here is a sketch of how I would handle your question using only
"vanilla" AppleScript.
Disclaimer: I've tested none of the lines I write below. All of them
may be bogus.
1. use the "read" command of the Standard Additions:
set theText to (read thePath)
2. check first whether "Data" begins the text, otherwise use the
"offset" command to find a RC followed by "Data" (assuming your file
is RC-line separated):
set theOffset to offset of (return & "Data") in theText.
3. first, get the location of the end of the "Data" line:
set theEndOfText to text theOffset thru -1 of theText
set theOtherOffset to offset of return in theEndOfText
(here you've got to test whether theOtherOffset is 0, meaning the
"Data" line is the last line of the file)
then, extract the whole line
set theLine to text 1 thru (theOtherOffset-1) of theEndOfText
Finally, use the text item delimiters to count the pipes:
set text item delimiters to "|"
set theCount to (count (text items of theLine)) - 1
(n pipes will break theLine into n+1 pieces)
(If you use Smile, thus the Regular Expressions, you can make your
script a bit shorter and you will test it more easily.)
Emmanuel
_______________________________________________
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.