Alternative notification system. Quickest way to read/write to a file?

As I am curious, I wrote two scripts to test.

# script 1

set p2d to path to desktop
set targetFile to (p2d as text) & "sharedReport.txt"

#Init the report
my writeto(targetFile, "", «class utf8», false)

set oldTime to 62000
repeat
	set curTime to time of (current date)
	if curTime > oldTime then
		repeat 5 times
			set maybe to my writeto(targetFile, "Script 1, current time : " & curTime & linefeed, «class utf8», true)
			if maybe then exit repeat # The write task behaved well
			# We are here if the write task failed,
			# guess that script 2 was writing, wait 1 second
			delay 1
		end repeat
		set oldTime to oldTime + 120
	end if
	delay 5
end repeat

#=====
(*
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
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====
# script 2

set p2d to path to desktop
set targetFile to (p2d as text) & "sharedReport.txt"

# Don't init the report which is shared with script 1

set oldTime to 62005
repeat
	set curTime to time of (current date)
	if curTime > oldTime then
		repeat 5 times
			set maybe to my writeto(targetFile, "-- Script 2, current time : " & curTime & linefeed, «class utf8», true)
			if maybe then exit repeat # The write task behaved well
			# We are here if the write task failed,
			# guess that script 1 was writing, wait 1 second
			delay 1
		end repeat
		set oldTime to oldTime + 140
	end if
	delay 5
end repeat

#=====
(*
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
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

I started script 1 then script 2

From time to time I opened the sharedReport.txt file and soon see data written it it.
As guessed I got:

Script 1, current time : 62004
– Script 2, current time : 62009
Script 1, current time : 62124
– Script 2, current time : 62149

As you may see I took care that the two scripts don’t try to write at the same time.

They clearly behave as assumed.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 24 décembre 2019 17:23:26

Added some instructions supposed to get rid of possible simultaneous write attempts.