counting lines
counting lines
- Subject: counting lines
- From: "Arthur Cormack" <email@hidden>
- Date: Thu, 12 Jul 2001 15:07:04 -0400
Greetings and Salutations appleScript wizards,
a newbie question:
I am writing a script that uses bbEdit to open up a log file, and
change the date formats of every entry in that log file. I am having
a problem counting the number of lines in the logfile. The line that
I use to calculate the number of lines goes like this:
"set lineCount to the number of lines in text window 1"
And it always seems to return a number that is 1 less that the number
of lines.
-I have noticed, however, that If I manually add a hard return after
the last line (there wasn't one there before) then it works.
Is there another perhaps another term that I should be using to
calculate the number of lines in a text file?
Thanks,
arthur cormack
_________________________
producer, interactive
the online group, TVOntario
W:416.484.2600.2010 C: 416.453.4369
E: email@hidden
Here are the handlers that I use to do the manipulation:
on change_word_in_all_entries_of_entire_log_file(whichLogFile,
wordNum, newWord)
--set thisLog to suckTextFromFile(which_file)
--we are given a bunch of text ... we cycle through it ian return a
whiole big chunk
--we might run into memory problems this way i fear, especailly when
the size of the files that we are parsing becomes huge
--but we will try it for now, later we could have BBEDit do it a
line at a time
if whichLogFile = "" then return 0
if wordNum = "" then return 0
if newWord = "" then return 0
--start BBEDit, open log file to work on
tell application "BBEdit 6.1"
activate
open {file whichLogFile} with LF translation
--cycle through each log entry in the log file
set lineCount to the number of lines in text window 1 --semaintic
issue: paragraphs / lines??!!
end tell
repeat with i from 1 to lineCount
tell application "BBEdit 6.1"
set thisPar to line i of text window 1 as string
--display dialog (lineCount as string) & (wordNum as string) &
newWord
end tell
--display dialog thisPar
set newPar to changeLogEntryWord(thisPar, wordNum, newWord)
tell application "BBEdit 6.1"
set line i of text window 1 to newPar
end tell
end repeat
end change_word_in_all_entries_of_entire_log_file
on changeLogEntryWord(lineToChange, wordNum, newWord)
--display dialog (lineToChange as string) & (wordNum as string) &
newWord
--lineToChange is a line entry from a log file ... it is a string
that consists of multiple items
--this handler will change an item in this string and return a new
modified line
--wordNum is the position of the item in the log entry that is to be
changed
-- newItem is the new string that is going to be substituted into
this position
--the old item will be nuked
--it is important that newItem be a string with no spaces ... it
must also be 1 item
--do an error check ... make sure that there is an item at itemNum
and that newItem is a continuous string
if lineToChange = "" then return 0 --need this
if wordNum = "" then return 0 --and this
if newWord = "" then return 0 --and this
if ((wordNum as number) > the number of items in lineToChange) then
return 0 -- can't change item that isn't there
set oldLine to lineToChange
set wordCount to the number of words in oldLine
if wordNum = 1 then
set beforeSnipSpacer to ""
set beforeSnip to ""
else
set beforeSnipSpacer to " "
set beforeSnip to words 1 thru (wordNum - 1) of oldLine
end if
if wordNum = wordCount then
set afterSnipSpacer to ""
set afterSnip to ""
else
set afterSnipSpacer to " "
set afterSnip to words (wordNum + 1) thru wordCount of oldLine
end if
set newString to ((beforeSnip & beforeSnipSpacer & newWord &
afterSnipSpacer & afterSnip) as string)
return newString
end changeLogEntryWord