eof error

Hi, I have an eof error on reading a text file. The code loads this subroutine on startup and then when needed:

on Sreloadgenres()
	log "a"
	log VSGprimefileloc
	log (get eof VSGprimefileloc)
	set VSLgenres to read VSGprimefileloc
	log "b"
	VGWpnVTgcTgVTlrTrOXres's setString_(VSLgenres) --text view
	log "c"
end Sreloadgenres

As you will see in the log below. It clearly gets the error after ‘a’ on the log but not when it is first used. This is all after a clean all and build, the text file originally contains ‘2 3’ (3 bytes).

The above log is after a clean and build but if stopped and rebuilt after the error with new data in the text file (56125 bytes) this happens (probably sooner because of my code):

Here also it is successful at first.

Try addressing all your open/close/read/write/eof commands to current application.

Problem remains. Entire Script (sorry about the funny variables, it’s a hierarchy):

script Prime_NumbersAppDelegate
	property parent : class "NSObject"
	
	property VGWpn : missing value -- window
	property VGWpnVTgc : missing value-- tab group
	property VGWpnVTgcTgVTlrTlOXlog : missing value --general tab text views
	property VGWpnVTgcTgVTlrTrOXres : missing value
	property VGWpnVTgcTgOFLrun : missing value --label
	property VGWpnVTgcTgOFTrun : missing value -- text field
	property VGWpnVTgcTgOSrun : missing value --stepper
	property VGWpnVTgcTgOFLend : missing value --label
	property VGWpnVTgcTgOFTend : missing value --field
	property VGWpnVTgcTgOBsttstp : missing value --button
	property VGWpnVTgcTcOBsttstp : missing value--the rest are similar for an irrelevant tab (VGWpnVTgcTc...)
	property VGWpnVTgcTcOFTstt : missing value
	property VGWpnVTgcTcOSstt : missing value
	property VGWpnVTgcTcOFTend : missing value
	property VGWpnVTgcTcOSend : missing value
	property VGWpnVTgcTcVTlrTlOXlog : missing value
	property VGWpnVTgcTcVTlrTrOXres : missing value
	
	property VSGprimefileloc : ""--important global variables
	--property VSGpreffileloc : ""
	property VSGgenprog : 0
	property VSGgencount : 0
	
	on Sgensttstp_(sender)-- click this, use Smain with general progress variable obtained in startup and a text field value
		Smain(VSGgenprog, VGWpnVTgcTgOFTrun's intValue() as integer)
	end Sgensttstp_
	
	on Scustsave_(sender)
	end Scustsave_
	
	on Scustsaveas_(sender)
	end Scustsaveas_
	
	on Scustopen_(sender)
	end Scustopen_
	
	(*on controlTextDidChange_(aNotification)
		log VGWpnVTgcTgOFTrun's intValue()
		Sgentextchange(VGWpnVTgcTgOFTrun's intValue())
	end controlTextDidChange_*)
	
	on Smain(VSLa, VSLb)--prime calculation (works)
		open for access VSGprimefileloc with write permission
		set VSLprimenumbers to {}
		repeat with VSLn from VSLa to VSLb by 2
			set VSLy to 0
			repeat with VSLx from 3 to (VSLn ^ 0.5 as integer) + 1 by 2
				if VSLn mod VSLx = 0 then
					set VSLy to 1
					exit repeat
				end if
			end repeat
			if VSLy = 0 then set the end of VSLprimenumbers to (" " & VSLn as string)
			if (VSLn + 1) mod 10000 = 0 then
				log 100 * ((VSLn - VSLa) / (VSLb - VSLa)) & "% -" & VSLn as string
			end if
			if (VSLn + 1) mod 100000 = 0 then
				if VSLprimenumbers is not {} then
					tell current application to write (VSLprimenumbers as string) to VSGprimefileloc starting at (get eof VSGprimefileloc) + 1-- write, important?
					set VSLprimenumbers to {}
					Sreloadgenres() --where error sub gets called
				end if
			end if
		end repeat
		close access VSGprimefileloc -- close, important?
	end Smain
	
	on Sgenstepperchange_(sender)
		set VSLgenrunstp to VGWpnVTgcTgOSrun's intValue() as integer
		set VSLgenruntext to VGWpnVTgcTgOFTrun's intValue() as integer
		log VSLgenrunstp
		if VSLgenrunstp is 1 then
			VGWpnVTgcTgOFTrun's setIntValue_(VSLgenruntext + 1000000)
		else
			if VSLgenruntext > 1000000 then
				VGWpnVTgcTgOFTrun's setIntValue_(VSLgenruntext - 1000000)
			else
				VGWpnVTgcTgOFTrun's setIntValue_(0)
			end if
		end if
	end Sgenstepperchange_
	
	on Scuststtstepperchange_(sender)
		set VSLcuststtstp to VGWpnVTgcTcOSstt's intValue() as integer
		set VSLcuststttext to VGWpnVTgcTcOFTstt's intValue() as integer
		log VSLcuststtstp
		if VSLcuststtstp is 1 then
			VGWpnVTgcTcOFTstt's setIntValue_(VSLcuststttext + 10000)
		else
			if VSLcuststttext > 10000 then
				VGWpnVTgcTcOFTstt's setIntValue_(VSLcuststttext - 10000)
			else
				VGWpnVTgcTcOFTstt's setIntValue_(0)
			end if
		end if
	end Scuststtstepperchange_
	
	on Scustendstepperchange_(sender)
		set VSLcustendstp to VGWpnVTgcTcOSend's intValue() as integer
		set VSLcustendtext to VGWpnVTgcTcOFTend's intValue() as integer
		log VSLcustendstp
		if VSLcustendstp is 1 then
			VGWpnVTgcTcOFTend's setIntValue_(VSLcustendtext + 10000)
		else
			if VSLcustendtext > 10000 then
				VGWpnVTgcTcOFTend's setIntValue_(VSLcustendtext - 10000)
			else
				VGWpnVTgcTcOFTend's setIntValue_(0)
			end if
		end if
	end Scustendstepperchange_
	
	on Sgetfilelocs()
		tell current application's class "NSBundle"
			tell its mainBundle()
				set VSGprimefileloc to (its pathForResource_ofType_("Prime Numbers", "txt")) as string
				--set VSGpreffileloc to (its pathForResource_ofType_("Prefs", "txt")) as string
			end tell
		end tell
	end Sgetfilelocs
	
	on Sreloadgenres()
		log "a"
		log VSGprimefileloc
		log (get eof VSGprimefileloc)
		tell current application to set VSLgenres to read VSGprimefileloc-- the error line
		log "b"
		VGWpnVTgcTgVTlrTrOXres's setString_(VSLgenres) --text view
		log "c"
	end Sreloadgenres
	
	on applicationWillFinishLaunching_(aNotification)
		VGWpnVTgcTgVTlrTlOXlog's setString_("Prime Numbers starting.")
		Sgetfilelocs()
		tell current application to set VSGgencount to count words of (read VSGprimefileloc)
		VGWpnVTgcTgVTlrTlOXlog's setString_((VGWpnVTgcTgVTlrTlOXlog's |string|() as string) & "
Prime count: " & VSGgencount)
		Sreloadgenres()
		--set VSGgenprog to (read VSGpreffileloc)
		tell current application to set VSGgenprog to item -1 of words of (read VSGprimefileloc)
		VGWpnVTgcTgVTlrTlOXlog's setString_((VGWpnVTgcTgVTlrTlOXlog's |string|() as string) & "
Current progress: " & VSGgenprog)
		
		VGWpnVTgcTgVTlrTlOXlog's setString_((VGWpnVTgcTgVTlrTlOXlog's |string|() as string) & "
Prime Numbers ready.")
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

You’re not addressing your open for access and get eof commands to current application. Also, I’ve found it more reliable to store the result of the open and use that for subsequent references, rather than file references.

No offense, I regard those variable names 15 years after c:\ongrtls.w95 and with the capability of (I guess) 255 characters as completely counterproductive, even for the author.
Try to read and understand the script in half a year.

:wink:

I put it on the open and close now, still not working. Not too sure about that second comment but bare in mind these files could be potentially MBs.

Ha! Too late :stuck_out_tongue: This is me coming back after a half year, and my system works perfectly…

These variables are named after their locations in and easy to remember way e.g.

VGWpnVTgcTgOFLrun

V variable
G GUI
Wpn contained by WINDOW called prime number
VTgc …VIEW TAB GROUP general custom
Tg …TAB general
O OBJECT
FLrun TEXT FIELD (LABEL-generally non editable) to do with or named ‘run’

you see they look after themselves :wink:

my Dialog Maker app inspired me to do this because it was 1400+ lines of millions of variables that were so linked there were often multiple needing an extremely similar name, this way I don’t need to comment or go look the IB what they are.

As in:

tell current application to set fileRef to (open for access file blahBlah)

tell current application to read fileRef

All of this and what you said, I’ve heard of refs but never used them.

OK I just changed line 1 of Smain to this:

tell current application to set VSGprimefileloc to open for access VSGprimefileloc with write permission

Same thing happened but the log VSGprimefileloc was 23.

I’m afraid that with those variable names and that much code, I get dizzy just looking at it. Can you make a simple example that shows the problem?

script Prime_NumbersAppDelegate
	property parent : class "NSObject"
	
	property window : missing value
	property windowTabGroup : missing value
	property windowTabGroupGeneralTabLogTextView : missing value
	property windowTabGroupGeneralTabResultTextView : missing value
	property windowTabGroupGeneralTabRunTextLabel : missing value
	property windowTabGroupGeneralTabRunTextField : missing value
	property windowTabGroupGeneralTabRunStepper : missing value
	property windowTabGroupGeneralTabEndTextLabel : missing value
	property windowTabGroupGeneralTabEndTextField : missing value
	property windowTabGroupGeneralTabOBsttstp : missing value
	property windowTabGroupTcOBsttstp : missing value --irrelevant...
	property windowTabGroupTcOFTstt : missing value
	property windowTabGroupTcOSstt : missing value
	property windowTabGroupTcEndTextField : missing value
	property windowTabGroupTcOSend : missing value
	property windowTabGroupTcLogTextView : missing value
	property windowTabGroupTcResultTextView : missing value
	
	property VSGprimefileloc : ""
	--property VSGpreffileloc : ""
	property VSGgenprog : 0
	property VSGgencount : 0
	
	on Sgensttstp_(sender)
		Smain(VSGgenprog, windowTabGroupGeneralTabRunTextField's intValue() as integer)
	end Sgensttstp_
	
	on Scustsave_(sender)
	end Scustsave_
	
	on Scustsaveas_(sender)
	end Scustsaveas_
	
	on Scustopen_(sender)
	end Scustopen_
	
	(*on controlTextDidChange_(aNotification)
		log windowTabGroupGeneralTabRunTextField's intValue()
		Sgentextchange(windowTabGroupGeneralTabRunTextField's intValue())
	end controlTextDidChange_*)
	
	on Smain(VSLa, VSLb)
		tell current application to open for access VSGprimefileloc with write permission
		set VSLprimenumbers to {}
		repeat with VSLn from VSLa to VSLb by 2
			set VSLy to 0
			repeat with VSLx from 3 to (VSLn ^ 0.5 as integer) + 1 by 2
				if VSLn mod VSLx = 0 then
					set VSLy to 1
					exit repeat
				end if
			end repeat
			if VSLy = 0 then set the end of VSLprimenumbers to (" " & VSLn as string)
			if (VSLn + 1) mod 10000 = 0 then
				log 100 * ((VSLn - VSLa) / (VSLb - VSLa)) & "% -" & VSLn as string
				if (VSLn + 1) mod 100000 = 0 then
					if VSLprimenumbers is not {} then
						tell current application to write (VSLprimenumbers as string) to VSGprimefileloc starting at (get eof VSGprimefileloc) + 1
						set VSLprimenumbers to {}
						Sreloadgenres()
					end if
				end if
			end if
		end repeat
		if VSLprimenumbers is not {} then
			tell current application to write (VSLprimenumbers as string) to VSGprimefileloc starting at (get eof VSGprimefileloc) + 1
			set VSLprimenumbers to {}
			Sreloadgenres()
		end if
		tell current application to close access VSGprimefileloc
	end Smain
	
	on Sgenstepperchange_(sender)
		set VSLgenrunstp to windowTabGroupGeneralTabRunStepper's intValue() as integer
		set VSLgenruntext to windowTabGroupGeneralTabRunTextField's intValue() as integer
		log VSLgenrunstp
		if VSLgenrunstp is 1 then
			windowTabGroupGeneralTabRunTextField's setIntValue_(VSLgenruntext + 1000000)
		else
			if VSLgenruntext > 1000000 then
				windowTabGroupGeneralTabRunTextField's setIntValue_(VSLgenruntext - 1000000)
			else
				windowTabGroupGeneralTabRunTextField's setIntValue_(0)
			end if
		end if
	end Sgenstepperchange_
	
	on Scuststtstepperchange_(sender)
		set VSLcuststtstp to windowTabGroupTcOSstt's intValue() as integer
		set VSLcuststttext to windowTabGroupTcOFTstt's intValue() as integer
		log VSLcuststtstp
		if VSLcuststtstp is 1 then
			windowTabGroupTcOFTstt's setIntValue_(VSLcuststttext + 10000)
		else
			if VSLcuststttext > 10000 then
				windowTabGroupTcOFTstt's setIntValue_(VSLcuststttext - 10000)
			else
				windowTabGroupTcOFTstt's setIntValue_(0)
			end if
		end if
	end Scuststtstepperchange_
	
	on Scustendstepperchange_(sender)
		set VSLcustendstp to windowTabGroupTcOSend's intValue() as integer
		set VSLcustendtext to windowTabGroupTcEndTextField's intValue() as integer
		log VSLcustendstp
		if VSLcustendstp is 1 then
			windowTabGroupTcEndTextField's setIntValue_(VSLcustendtext + 10000)
		else
			if VSLcustendtext > 10000 then
				windowTabGroupTcEndTextField's setIntValue_(VSLcustendtext - 10000)
			else
				windowTabGroupTcEndTextField's setIntValue_(0)
			end if
		end if
	end Scustendstepperchange_
	
	on Sgetfilelocs()
		tell current application's class "NSBundle"
			tell its mainBundle()
				set VSGprimefileloc to (its pathForResource_ofType_("Prime Numbers", "txt")) as string
				--set VSGpreffileloc to (its pathForResource_ofType_("Prefs", "txt")) as string
			end tell
		end tell
	end Sgetfilelocs
	
	on Sreloadgenres()
		log "a"
		log VSGprimefileloc
		tell current application to log (get eof VSGprimefileloc)
		tell current application to set VSLgenres to read VSGprimefileloc
		log "b"
		windowTabGroupGeneralTabResultTextView's setString_(VSLgenres) --text view
		log "c"
	end Sreloadgenres
	
	on applicationWillFinishLaunching_(aNotification)
		windowTabGroupGeneralTabLogTextView's setString_("Prime Numbers starting.")
		Sgetfilelocs()
		tell current application to set VSGgencount to count words of (read VSGprimefileloc)
		windowTabGroupGeneralTabLogTextView's setString_((windowTabGroupGeneralTabLogTextView's |string|() as string) & "
Prime count: " & VSGgencount)
		Sreloadgenres()
		--set VSGgenprog to (read VSGpreffileloc)
		tell current application to set VSGgenprog to item -1 of words of (read VSGprimefileloc)
		windowTabGroupGeneralTabLogTextView's setString_((windowTabGroupGeneralTabLogTextView's |string|() as string) & "
Current progress: " & VSGgenprog)
		
		windowTabGroupGeneralTabLogTextView's setString_((windowTabGroupGeneralTabLogTextView's |string|() as string) & "
Prime Numbers ready.")
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

I hope this helps, but the one below contains only the problem, which may not be enough though:

script Prime_NumbersAppDelegate
	property parent : class "NSObject"
	
	property window : missing value
	property windowTabGroup : missing value
	property windowTabGroupGeneralTabLogTextView : missing value
	property windowTabGroupGeneralTabResultTextView : missing value
	property windowTabGroupGeneralTabRunTextLabel : missing value
	property windowTabGroupGeneralTabRunTextField : missing value
	property windowTabGroupGeneralTabRunStepper : missing value
	property windowTabGroupGeneralTabEndTextLabel : missing value
	property windowTabGroupGeneralTabEndTextField : missing value
	property windowTabGroupGeneralTabOBsttstp : missing value

	
	property VSGprimefileloc : ""
	--property VSGpreffileloc : ""
	property VSGgenprog : 0
	property VSGgencount : 0
	
	on Sgensttstp_(sender)
		Smain(VSGgenprog, windowTabGroupGeneralTabRunTextField's intValue() as integer)
	end Sgensttstp_

	
	on Smain(VSLa, VSLb)
		tell current application to open for access VSGprimefileloc with write permission
		set VSLprimenumbers to {}
		repeat with VSLn from VSLa to VSLb by 2
			set VSLy to 0
			repeat with VSLx from 3 to (VSLn ^ 0.5 as integer) + 1 by 2
				if VSLn mod VSLx = 0 then
					set VSLy to 1
					exit repeat
				end if
			end repeat
			if VSLy = 0 then set the end of VSLprimenumbers to (" " & VSLn as string)
			if (VSLn + 1) mod 10000 = 0 then
				log 100 * ((VSLn - VSLa) / (VSLb - VSLa)) & "% -" & VSLn as string
				if (VSLn + 1) mod 100000 = 0 then
					if VSLprimenumbers is not {} then
						tell current application to write (VSLprimenumbers as string) to VSGprimefileloc starting at (get eof VSGprimefileloc) + 1
						set VSLprimenumbers to {}
						Sreloadgenres()
					end if
				end if
			end if
		end repeat
		if VSLprimenumbers is not {} then
			tell current application to write (VSLprimenumbers as string) to VSGprimefileloc starting at (get eof VSGprimefileloc) + 1
			set VSLprimenumbers to {}
			Sreloadgenres()
		end if
		tell current application to close access VSGprimefileloc
	end Smain

	on Sreloadgenres()
		log "a"
		log VSGprimefileloc
		tell current application to log (get eof VSGprimefileloc)
		tell current application to set VSLgenres to read VSGprimefileloc
		log "b"
		windowTabGroupGeneralTabResultTextView's setString_(VSLgenres) --text view
		log "c"
	end Sreloadgenres
	
end script

Presumably you’re getting the error because you’re trying to read straight after a write, so the file mark is at the end of the file. What happens if you use “read from…”?

Thank you, surprise surprise it works :lol: :slight_smile: