AppleScript Reads Only the First Two Characters from a File

Hello,

I've encountered an issue with an AppleScript that's supposed to read the entire content of a text file, but for some reason, it only reads the first two characters. Here's the scenario:

I have a text file located at <PATH>/numbers.txt. The file contains the string "1123". However, when I run my AppleScript, it seems to only process the first two characters of the file's content.

I tried Apple script background and blocking.

Here is the AppleScript I'm using:

set file_key_Path to "/numbers.txt"
set expectedContent to "1123" -- Expected content
set file_key_Content to ""

try
-- Attempt to open the file
set file_key_Reference to open for access file_key_Path
-- Read the file content
set file_key_Content to read file_key_Reference as «class utf8»
-- Close the file
close access file_key_Reference
on error errorMessage
-- Handle errors
set file_key_Content to "Could not read file: " & errorMessage
return file_key_Content
end try

-- Check the read content
if file_key_Content is expectedContent then
-- If content matches, do something
else
-- If content does not match, do something else
end if

I think you need to actively tell Apple Script how much to read. Try this:

set file_key_Content to read file_key_Reference from 0 to eof

Thank you for your suggestion. I tried reading the file from start to end using the from 0 to eof specification as you recommended, but the script still only returns the first character ("1") instead of the full content "1123".

When running the script, the dialog only shows "1". It's perplexing because when I check the file content manually using cat in the terminal, it shows "1123" as expected. The file's permissions seem fine, and it is UTF-8 encoded without any special characters or BOM markers. I confirmed this by examining the file in a hex editor.

Do you have any further ideas on why the script would not read the entire content of the file? Is there possibly an issue with how AppleScript handles the file reading on some systems or under certain conditions?

Hello,

I've been encountering a peculiar issue with an AppleScript that reads content from a text file. The file in question, numbers.txt, is expected to contain the string "1123". Initially, the script reads the file correctly, but after modifying the file's content (even with a simple text editor), the script starts to read only the first character.

Here's the behavior breakdown:

  • When I create numbers.txt with the echo command (echo "1123" > numbers.txt), the script reads the full content perfectly.
  • If I open numbers.txt in a text editor, modify the content to "1123", and save it, the script then reads only the first character "1".
  • I've verified that the file encoding remains UTF-8 and that no special characters are introduced. Permissions are also correctly set, allowing for read and write operations.
  • Using cat in the Terminal displays the content correctly as "1123".
  • Even after deleting and recreating the file, the issue persists with the script only reading the first character after any file modification.

The expected output is the full string "1123", but after any file modification, only "1" or "11" is returned.

Has anyone experienced similar issues with AppleScript reading files after modifications? Any suggestions or insights would be greatly appreciated.

Thank you for your help!

have you checked whether it works as expected when you run the script in apple’s script editor app?