read and write to files

I have the following piece of code:



my write_to_file(" error", "Shared G4 HD:Users:karrenmay:Desktop:imageNumberFile.txt", true)

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file --starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

it was previously an append file, but i think that i have almost got it so that i can over write the file.

But i cant seem to read the value from before, i.e. there is a number and i wish to import that into the script, us it then +1 to it then save it again.

I can’t figure out how to incorporate it into the example given but does this read the file?

set previous_ to read file "Shared G4 HD:Users:karrenmay:Desktop:imageNumberFile.txt"

– Rob

yes it does, so i am guessing that i could just +1 to it???

have solved it, what i did was do the add bit first, then write to the file. But not append.



set previous_ to read file "Shared G4 HD:Users:karrenmay:Desktop:imageNumberFile.txt"
set numberIm to previous_
set numberIm to ((numberIm) + 1)

my write_to_file("" & numberIm & "", "Shared G4 HD:Users:karrenmay:Desktop:imageNumberFile.txt", true)

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file --starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file


Cheers Rob!!