4 Replies Latest reply: May 12, 2012 4:10 PM by BobHarris
personal_username Level 1 (0 points)

ok, i have this command:

 

set mywordcount to do shell script "wc -l < MyFile.txt"

 

which gives me the number of words of a given txt

with display alert it reports me number 1 when there is one word, number 4 if there are 4 words, etc...

 

the problem comes when i want to do one action if the file has 1 word, another if it has more than 1...

when i write:

 

if mywordcount = 1 then

myactions here

else

otheractions here

end if


it doesn't do anything at all...

any ideas?

thanks!

  • Mark Jalbert Level 5 (4,630 points)
    wc -l < MyFile.txt
    

    Will count the number of lines in the file MyFile.txt. If you are going to write a script with AppleScript then try to stay in AppleScript ( don't spawn another process). I don't script in AppleScript, so I'm not much help beyond that.

  • twtwtw Level 5 (4,925 points)

    the way to do this in applescript is as follows:

     

    set contentsText to read "/path/to/file.txt"

    set mywordcount to count of words of contentText

     

    that will return an integer value.  if you want to use the do shell script approach, keep in mind that dss always returns text, so you'll want to coerce the text value to an integer:

     

    set mywordcount to (do shell script "wc -l < MyFile.txt") as integer

  • personal_username Level 1 (0 points)

    thanks, again! ;-)

  • BobHarris Level 6 (17,689 points)

    NOTE:  wc -l is a line count and wc -w is a word count.  You post is mixing apples and oranges in that you ask for word count then post an example of using wc with a line count.