writing to text file

Hi,

I’m trying to put data into a text file. I keep getting an error: “subject_number is undefined.” Does anyone have any ideas why this is happening?

Thanks!

on run
try
set full_name to prompt_for_full_name()
set birthday to prompt_for_birthday()
set subject_number to prompt_for_subject_number()
set name to write_file()
on error msg
display_error_message(msg)
end try
end run

on display_error_message(message)
display dialog message buttons {“OK”, “Cancel”} default button 1
end display_error_message

on prompt_for_full_name()
set full_name to 0
try
display dialog “Full Name:” buttons {“OK”, “Cancel”} default answer 0 default button 1
set dialog_result to result
set full_name to (text returned of dialog_result) as string
on error msg
display_error_message(msg)
set full_name to 0
end try
return full_name
end prompt_for_full_name

on prompt_for_birthday()
set birthday to 0
try
display dialog “Birthday” buttons {“OK”, “Back”} default answer 0 default button 1
set dialog_result to result
set subject_number to (text returned of dialog_result) as integer
on error msg
display_error_message(msg)
set subject_number to 0
end try
return birthday
end prompt_for_birthday

on prompt_for_subject_number()
set subject_number to 0
try
display dialog “Subject Number:” buttons {“OK”} default answer 0 default button 1
set dialog_result to result
set subject_number to (text returned of dialog_result) as integer
on error msg
display_error_message(msg)
set subject_number to 0
end try
return subject_number
end prompt_for_subject_number

on write_file()
tell application “TextEdit”
activate
make new document
set theDate to current date
set text of document 1 to {theDate, subject_number, birthday as text}
save document 1 in “/Users/cdlcoder/Desktop/lastsubject.txt”
end tell
end write_file

Hi,

subject_number is defined as a local variable in the on run handler as well as in the prompt_for_subject_number() handler. Both variables are different objects and are accessible only within the respective handlers.

Define subject_number as global or pass the value as parameter in the handler call

PS: There is also a variable name clash in the prompt_for_birthday() handler
and a class clash (string vs. integer) in the prompt_for_full_name() handler


.
set name to write_file(birthday, subject_number)
.

.

on write_file(theBirthday, theSubjectNumber)
	tell application "TextEdit"
		activate
		make new document
		set theDate to current date
		set text of document 1 to {theDate, theSubjectNumber, theBirthday as text}
		save document 1 in "/Users/cdlcoder/Desktop/lastsubject.txt"
	end tell
end write_file


Notes: Probably you have to take the string representation of current date and
use an HFS path (colon separated) in the save line

Also don’t set the ‘text’ of a document to a list, don’t use a reserved word (‘name’) as a variable, and don’t set the variable to the result of a handler which doesn’t return a result.

I know, I gave up at a certain time :wink:

:slight_smile:

More helpfully, putting everything together and replacing the three prompt handlers with one, this is probably how the script should look:

on run
	try
		set full_name to prompt_for("Full Name:")
		set birthday to prompt_for("Birthday:")
		set subject_number to prompt_for("Subject Number:")
		write_file(full_name, birthday, subject_number)
	on error msg
		display_error_message(msg)
	end try
end run

on display_error_message(message)
	display dialog message buttons {"OK", "Cancel"} default button 1
end display_error_message

on prompt_for(the_prompt)
	try
		display dialog the_prompt buttons {"OK", "Cancel"} default answer "" default button 1
		set dialog_result to result
		set the_input to (text returned of dialog_result)
	on error msg
		display_error_message(msg)
		set the_input to ""
	end try
	return the_input
end prompt_for

on write_file(full_name, birthday, subject_number)
	set theDate to date string of (current date)
	tell application "TextEdit"
		activate
		make new document
		set text of document 1 to theDate & tab & subject_number & tab & full_name & tab & birthday
		save document 1 in file ((path to desktop as text) & "lastsubject.txt")
	end tell
end write_file

or still more convenient without TextEdit


.

on write_file(full_name, birthday, subject_number)
	set theDate to date string of (current date)
	set destinationFile to ((path to desktop as text) & "lastsubject.txt")
	try
		set fileRef to open for access file destinationFile with write permission
		write theDate & tab & subject_number & tab & full_name & tab & birthday to fileRef
		close access fileRef
	on error
		try
			close access file destinationFile
		end try
	end try
end write_file


Wow! Thank you so much! I am so amazed by how helpful everyone on this forum has been. I am very new to all of this and just trying to teach myself.

Thanks!

Hi,

I have taken this code and am trying to combine it with previous code, so I can record players’ performance. I am not sure if I’ve done it correctly, but now am having input variables coming up as undefined when I run it. Any suggestions? Also, Is there a reason that AppleScripter put {} after save?

Thanks!

on run
try
set full_name to prompt_for(“Full Name:”)
set birthday to prompt_for(“Birthday:”)
set sn to prompt_for(“Subject Number:”)
game()
save {}

on error msg
	display_error_message(msg)
end try

end run

on display_error_message(message)
display dialog message buttons {“OK”, “Cancel”} default button 1
end display_error_message

on prompt_for(the_prompt)
try
display dialog the_prompt buttons {“OK”, “Cancel”} default answer “” default button 1
set dialog_result to result
set the_input to (text returned of dialog_result)
on error msg
display_error_message(msg)
set the_input to “”
end try
return the_input
end prompt_for

on game()
tell application “TextEdit”
activate
make new document
set theDate to date string of (current date)
set text of document 1 to theDate & tab & sn & tab & full_name & tab & birthday
save document 1 in file ((path to desktop as text) & “lastsubject.txt”)

end tell
repeat 6 times
	set x to 0
	feedback(play(randNum()))
	set x to x + 1
	write x & tab to document 1 starting at eof
end repeat

end game

on randNum()
set t to random number from 1 to 5
write t & tab to document 1 starting at eof
end randNum

on play(aNum)
display dialog (aNum) giving up after 3
{(display dialog (“”) default answer “”)'s text returned} & aNum
write text returned & retun to document 1 starting at eof
end play

on feedback({this, |that|})
repeat
if not this = |that| as string then
display dialog “Try again” buttons {“OK”} default button 1
feedback(play(randNum()))
exit repeat
else
display dialog “Well done!” buttons {“Cancel”, “Continue”} cancel button 1 default button 2
exit repeat
end if
end repeat
end feedback

on save
save document 1 in file ((path to desktop as text) & “lastsubject.txt”)
end end

Alternatively, I’m now trying it out with StefanK’s script and now am only running into the destinationFile not being defined…

on run
try
set full_name to prompt_for(“Full Name:”)
set birthday to prompt_for(“Birthday:”)
set subject_number to prompt_for(“Subject Number:”)
write_file(full_name, birthday, subject_number)
game()
on error msg
display_error_message(msg)
end try
end run

on display_error_message(message)
display dialog message buttons {“OK”, “Cancel”} default button 1
end display_error_message

on prompt_for(the_prompt)
try
display dialog the_prompt buttons {“OK”, “Cancel”} default answer “” default button 1
set dialog_result to result
set the_input to (text returned of dialog_result)
on error msg
display_error_message(msg)
set the_input to “”
end try
return the_input
end prompt_for
on write_file(full_name, birthday, subject_number)
set theDate to date string of (current date)
set destinationFile to ((path to desktop as text) & “lastsubject.txt”)
try
set fileRef to open for access file destinationFile with write permission
write theDate & tab & subject_number & tab & full_name & tab & birthday to fileRef
close access fileRef
on error
try
close access file destinationFile
end try
end try
end write_file

on game()

repeat 6 times
	set x to 0
	feedback(play(randNum()))
	set x to x + 1
	set fileRef to open for access file destinationFile with write permission
	write x & tab to file starting at eof
end repeat

end game

on randNum()
set t to random number from 1 to 5
set fileRef to open for access file destinationFile with write permission
write t & tab to file starting at eof
end randNum

on play(aNum)
display dialog (aNum) giving up after 3
{(display dialog (“”) default answer “”)'s text returned} & aNum
set fileRef to open for access file destinationFile with write permission
write text returned & retun to file starting at eof
end play

on feedback({this, |that|})
repeat
if not this = |that| as string then
display dialog “Try again” buttons {“OK”} default button 1
feedback(play(randNum()))
exit repeat
else
display dialog “Well done!” buttons {“Cancel”, “Continue”} cancel button 1 default button 2
exit repeat
end if
end repeat
close access fileRef

end feedback

in the handler randNum() the variable destinationFile is not defined.
This handler cannot “see” the local variable destinationFile in write_file()

You have also to close always the file reference opened with open for access