Quick Read

Is it possible to do this task:

item -1 of words of (read alias "Macintosh HD:Users:Richard:Desktop:TextFile.txt")

a different way avoiding the long wait caused by ‘words of’ on large files?


word -1 of (read alias "Macintosh HD:Users:Richard:Desktop:TextFile.txt")

Or perhaps:


word -1 of (read alias "Macintosh HD:Users:Richard:Desktop:TextFile.txt" from -50) -- say.

Both of these assume that the file contains only single-byte characters.

So if I know the size of the last word I can just read that amount or more if I want. “single byte characters”?

In broad terms, yes. But .

If you don’t specify how the ‘read’ command is to interpret what it reads from the file, it assumes that the file contains text and that this is of the one-byte-per-character variety. If your file contains text in this form, and the last word contains, say, five characters, the word will occupy five bytes in the file. Whether or not these are the last five bytes will depend on whether the word’s followed by any non-word characters such as punctuation, spaces, or line endings.

In my second example above the ‘read’ command reads the last 50 bytes from the file and interprets them as a 50-character text. The core AppleScript language then extracts the last ‘word’ from that text. This may not work with ‘Unicode text’ or ‘«class utf8»’ because with them, you can’t absolutely guarantee that the 50th byte from the end of the file will be the first byte of a character. Starting the read from the middle of a character will either cause an error or result in the misinterpretation of the text.

Thank you :slight_smile: