number of lines of text in a file

morning,

I`m not at my mini at the moment (sadly at work instead).
however I have found a code snippet that looks interesting

give me the count of the lines of file 'foo.txt'
get the count of lines of file foo.txt
get the number of lines of file foo.txt
get the count of the lines of file 'foo.txt'
get the number of lines of document foo.txt
get the number of lines of foo.txt
get the number of foo.txt's lines
get how many lines of file foo.txt
get the number of lines in file foo.txt

are all of these statements the same and valid?
i.e.
set stev_count to get the number of lines of foo.txt

this is a problem I've been puzzling over for a while now.

any comments for a newbie at applescript would be welcomed.

cheers
Steve

mini, Mac OS X (10.4.7), ipod mini, several firewire disks, bluetooth keyboard and mouse, eyetv

Posted on Sep 4, 2006 11:39 PM

Reply
8 replies

Sep 5, 2006 12:40 AM in response to Steven Prigg

Hi,

The concept of "lines" has no meaning in plain AppleScript (although it may be implemented in some application's dictionaries).

However, you can count paragraphs:

set the_text to read(choose file)
set par_count to (count paragraphs in the_text)
return par_count


All empty paragraphs will be included in the count - including one at end if the last paragraph in the file is followed by a return.

HTH

H

Sep 5, 2006 1:11 AM in response to HD

HD
thanks for the reply.

basically i have a text file with an unknown number of lines.
each line is terminated with a LF (ascii char 10).

I was getting into a bit of a pickle last night trying to count the number of lines using the "get eof" as a marker for the end of the file.

I think where I was falling down was that eof returns the byte number where the file ends, i think.

could I do something like:

set fileRef to open for access myFile with write permission
set myData to read fileRef to (get eof)
close access fileRef
set AppleScript's text item delimiters to ASCII character 10 -- (a line feed)
set newTxt to text items of myData
set number oflines to count myData


am I on the right lines here?

again
thanks
Steve



mini Mac OS X (10.4.7) ipod mini, several firewire disks, bluetooth keyboard and mouse, eyetv

Sep 5, 2006 2:00 AM in response to Steven Prigg

You shouldn't need to go to this level of complexity. The script I gave above works for me when reading a file whose paragraphs are separated with Unix LFs.

A couple of points:

You don't need to open the file for access unless you want to modify its content. "Read" is sufficient. And opening with write permission introduces the risk of leaving the file locked open if your script fails for any reason.

The "read" command automatically reads to the end of the file. So there's no need to use "eof".

Your script can be as simple as this:

set file_content to read myFile
set numberoflines to count paragraphs in file_content


Hope this helps.

H

Sep 5, 2006 2:30 AM in response to HD

I completely understand now!!

many many thanks

I`ll give it a try when I get home.

on another note I was also having problems returning a value form a sub routine.

e.g.

on test_routine (var1, var2, var3)
--various lines of code using var1, var2, var3
set fred to 10
set bill to 15
set job to 20

return {fred, bill, job}
end test_routine

when I call the routine how do I refrence the returned data from it i.e. if I want to use "bill"

set myvar to item 2 of my test_routine (1,2,3)

is this right?

sorry to pester you again

BR
Steve

Sep 5, 2006 3:47 AM in response to Steven Prigg

Hi

Your handler works for me, but I'm not sure that it does what you want it to.

The test_routine handler returns {10,15,20}, the values you assign to your three variables.

"item 2 of my test_routine" returns 15, which is the value you've assigned to the variable bill.

If you want the handler to return the string "bill", then we'd probably need to know what those "various lines of code" are and what you want to do with the whole script.

Perhaps:

if var2 = 15 then return "bill"?

Cheers,

H

Sep 5, 2006 6:56 AM in response to dev_sleidy

tell application "TextEdit"
activate
--Count the characters:
set allCharacters to every character of the front document
set numberOfCharacters to (count allCharacters)
set characterText to "Characters: " & numberOfCharacters
--Count the words:
set allWords to every word of the front document
set numberOfWords to (count allWords)
set wordText to "Words: " & numberOfWords
--Count the paragraphs:
set allParagraphs to every paragraph of the front document
set numberOfParagraphs to (count allParagraphs)
set paragraphText to "Paragraphs: " & numberOfParagraphs
--Assemble the text for the dialog box:
set dialogText to characterText & return & wordText & return & paragraphText
display dialog dialogText
end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

number of lines of text in a file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.