Why dosn't a simple script work more than once?

I admit, I’m not good at this. I have read every tutorial on variables I can find and nothing I try works. I’m just trying to pass the contents of a text file to a variable to use in a “set cell” Applescript in Filemaker “do script”.

It works one time then it returns error “The variable fileContents is not defined.” number -2753 from “fileContents”

set theFile to POSIX file ("/Users/Chris/Desktop/SHS PDF Files/copies/FMP_Text.txt")
open for access theFile
	set fileContents to (read theFile)
on error
	close access theFile
end try
return fileContents

If I declare fileContents as a global, it still dosen’t work.


Just think about what will happen logically

  • define variable theFile
  • open file descriptor
  • in exception, read the file and close it on catch ← fault is made here
  • return file contents

in your exception you read the file succesfully and no catch will be triggered and therefore the file is never close. Then second run you want to open the file but it’s already open and your code will throw an error and then close the file while it should have in the previous run.

Solution: To read a file you don’t need to open or close it but can open, read the entire file and close it on one go:

set theFile to POSIX file ("/Users/Chris/Desktop/SHS PDF Files/copies/FMP_Text.txt")
set fileContents to read theFile
return fileContents

You even don’t need to specify an HFS path, read/write understands POSIX paths as well.

To be pedantic, the script fails on subsequent runs not so much because the file’s already open, but because the ‘read’ command happens to reuse an access reference set up and used in an earlier run of the script.

It’s possible to have several accesses open to the same file at the same time, each with its own access reference, although only one can have write permission. If the ‘read’ command’s given a file specifier instead of the reference returned by ‘open for access’ (as it is in dishusa’s script), it uses the first existing reference which comes to hand. If you’re lucky (as I have just been, several times), the reference used will be the one just opened and the read will naturally start at the beginning of the file. Otherwise (as in dishusa’s case), the reference will be one previously used to read right to the end of the file, in which case, the current read attemps to continue beyond that point and causes an error.

So if you use ‘open for access’, it’s a good idea to use the reference number it returns and to ensure that the access is closed when you’re finished with it.

DJ’s advice that you don’t need to use ‘open for access’ here is correct, although it won’t by itself cure the problem if you’re you’re already having it. You need to close all the existing unclosed accesses first so that ‘read’ will then open and close its own. The easiest way to do this is to quit the application which opened the accesses ” usually the application running the script (unless the ‘open for access’ command was in a ‘tell’ statement). When the application quits, all file accesses registered to it are automatically closed.

Thank you Nigel (and others whom replied). That makes sense as the new error I get now is:

error “End of file error.” number -39 from file “Macintosh HD:Users:Chris:Desktop:SHS PDF Files:copies:FMP_Text.txt”

Can someone point me to the proper syntax to close the read properly or do something to the file to make it run consistently?

Now the next commands give unexpected results. The next thing it does is set cell in Filemaker to the contents of the read file variable but it only sets the first several characters of a 1500+ character text document. Probably something to do with text encoding but that is now WAY over my head.

I’m just trying to do the equivalent of a “copy” command, taking the text from this text document and “pasting” it into a filemaker cell. Simple enough but all I get are errors stacked on errors.

I’m 57 years old. I don’t have enough time left in this lifetime to figure out an applescript to do this automatically. I have already spent 3 days on just getting it to read the variable and now it has the next error. Once that is fixed, I’m sure there will be another error generated…(do you see hints of frustration?)

If the problem is really that you left the file in “open for access” state, you may try to run :

set theFile to "/Users/Chris/Desktop/SHS PDF Files/copies/FMP_Text.txt"
	close access file theFile
end try

Below is a sample of code taking care of the problem when we write to a file.

Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
			close access targetFile
		end try
		return false
	end try
end writeto


I don’t post a sample reading a file because most of the time, as others already wrote, we don’t need to open for access a file which we just want to read.
I just do that when I know that the file contents is huge. In such case I write a loop reading chunks of reasonable size.

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) mercredi 2 décembre 2015 17:56:30