open file access error

Ok, so here is the issue.

I have a text document that contains one line of text - it is a date string just like you would get if you copied the string given by: get (current date)

When the script first launches, I have to read the file and get that information. The script then proceeds to do a bunch of other things, and then has to open that same file and now change the date the current date returned when the script first began.

The script will always open the file the first time, and then when it gets to the second time it needs to open the file, I get an error that reads… “File file path:To:File.txt is already open.” (The double “file file” in the error message is not a typo by me - that’s what the error reads. Can anyone see why I would be getting that error? Is there a way to determine if my close statements are working?


--Now update files that have been updated
set modFile to choose file
try
	set modReport to open for access file modFile
	set startDate to read modReport
	close access modReport
on error
	close access modReport
end try

--do a bunch of stuff

		set modReport to open for access file modFile with write permission
		try
			write startTime to modReport
			close access modReport
			exit repeat
		on error
			display dialog "can't edit the file"
			close access file modReport
			exit repeat
		end try




Hi,

two suggestions:
Open for access has two forms

open for access [alias]

or

open for access file [string path]

in your case choose file returns an alias, so omit file

if an error occurs, you have to close the file (modFile), not the identifier (modReport),
try this

set modFile to choose file
try
    set modReport to open for access modFile
    set startDate to read modReport
    close access modReport
on error
    try
        close access modFile
    end try
end try

the extra try block avoids an error message, if the file is already closed

Unfortunately, that hasn’t worked either…

it’s strange…

Here’s the event log for the first portion of the script dealing with that text file.

Why does AppleScript return 248 one time and then another number on a different iteration of the script??

Later, when the script attempts to access the file again - this is what event log reports.

248 is just in internal ID to identify the file.
It changes every time you use open for access

I assume, there is something wrong in your repeat loop.
I tried the code successfully just as it is

Hi, Scott.

The ‘248’ is the value returned by ‘open for access’ on that particular occasion. It’s the value given to your variable ‘modReport’ and is an ID for the access that’s just been opened between your script application (or whatever it was ‘tell’-ing at the time) and the file. The system hands out the numbers serially, presumably to ensure that the numbers are unique.

When using the other File Read/Write commands after opening for access, you can use either a file specifier, an alias, or this reference number. It’s usually best to use the number.

As Stefan’s already said, since ‘modFile’ is an alias, you shouldn’t put ‘file’ in front of it in the ‘open for access’ lines.

Similarly, you shouldn’t put ‘file’ in front of 'modReport in the second part of your script!

Only one access to a file can have write permission at any one time, so if you’re unable to open the file again with write permission, it means the write-permission access wasn’t closed on a previous run. Possible causes for that would be the ‘file modReport’ I’ve just mentioned, or clicking the “Cancel” button in the display dialog, which would stop the script before the access was closed.

The quick way to get back your write permission is to quit the application running the script (eg. Script Editor) or the application the script told to open the access. This will close all the application’s still-open accesses, allowing a fresh start when you relaunch the application.

By the way, I’d recommend inserting set eof modReport to 0 before the ‘write’ line, in case the previous date string was longer than the one you’re writing.

Thanks for the eof tip, Nigel.

I’m still uncertain why it would not close and reopen within the same script - but, I just left it open throughout the script and closed it at the end instead.

All of that seems to be working properly now.

Thanks for all the help!