hhas Hints on file size and word count

NOTE: these scriptlets were moved from an answer by hhas to a thread in AppleScript | OS X

  • To get a file’s size using Standard Additions’ ‘info for’ command:
set the_file to choose file
size of (info for the_file)
  • To get the length of the text in a plain text file, use Standard Additions’ ‘read’ command to read that text into AppleScript as a string or Unicode text value, then get the length of that:
set the_file to choose file
length of (read the_file as string) -- for a MacRoman-encoded [1] text file
set the_file to choose file
length of (read the_file as «class utf8») -- for a UTF8-encoded text file
set the_file to choose file
length of (read the_file as Unicode text) -- for a UTF16-encoded text file

(Note: in the above examples, the ‘as [class]’ bit is a parameter to the ‘read’ command that tells it how to read the file, not a coercion applied to the value of variable ‘the_file’.)

  • To get the length of the text in non-plain text files (.rtf, .doc, etc.) you’ll need to open them in a suitable application and ask the application to measure their length, e.g.:
set the_file to choose file -- e.g. an RTF file
tell application "TextEdit"
	open the_file
	count characters of text of document 1
end tell

HTH