Script will only run once

I am trying to write a script to gather Name, address, etc… in a text file.

But my handler is not working sometimes get a message “script doesn’t understand message”, sometimes it works.

Why?

set the iDay to day of the (current date) as integer
if iDay < 10 then set iDay to "0" & iDay as text

set the iMonth to month of the (current date) as text

set CaminhoArquivo to (path to desktop as text) & "Teste" & iDay & "_" & iMonth & ".txt"

-- Aqui voc� pergunta a raz�o social:


set razaopergunta to Texto_Novo("Qual � a raz�o social", 53, "Raz�oSocial:", "", " ")
get razaopergunta
set TextoFinal to ""
set TextoFinal to TextoFinal & razaopergunta

--Aqui voc� pergunta  o endere�o:


set enderecopergunta to Texto_Novo("Qual � o endere�o", 49, "Endere�o:", "", " ")
get enderecopergunta
--set TextoFinal to ""
set TextoFinal to TextoFinal & enderecopergunta

my write_to_file(TextoFinal, CaminhoArquivo)




on Texto_Novo(pergunta, MeuLimiteCaracteres, Limitador1, Limitador2, caracterespaco)
	
	set pergunta to display dialog pergunta default answer "Escreva aqui a resposta" buttons "Ok" default button "Ok"
	set meuTexto to Limitador1 & text returned of pergunta
	set Limitador1 to offset of Limitador1 in meuTexto
	set Limitador2 to offset of Limitador2 in meuTexto
	set Texto_Novo to (characters Limitador1 thru (Limitador2 - 1) of meuTexto) as string
	set myCount to the count of Texto_Novo
	set diferenca to MeuLimiteCaracteres - myCount
	if myCount is equal to MeuLimiteCaracteres then
		return Texto_Novo
	end if
	if myCount is greater than MeuLimiteCaracteres then
		set diferenca to myCount - MeuLimiteCaracteres
		repeat diferenca times
			set Texto_Novo to characters 1 thru -(Limitador1 + 1) of Texto_Novo as string
		end repeat
		return Texto_Novo
	end if
	if myCount is not greater than MeuLimiteCaracteres then
		repeat diferenca times
			set Texto_Novo to Texto_Novo & caracterespaco
		end repeat
		return Texto_Novo
	end if
	
end Texto_Novo

on write_to_file(this_data, target_file)
	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
		write this_data to the open_target_file starting at eof
		close access the open_target_file
	on error
		try
			close access file target_file
		end try
	end try
end write_to_file

Thanks!

Bernardo
Rio de Janeiro - Brazil

The main problem is that you use a variable with the same name as a handler (“Texto_Novo”). The language was hard to follow (my problem, not yours) but I think this is what you want:

set the iDay to day of the (current date) as integer
if iDay < 10 then set iDay to "0" & iDay as text
set the iMonth to month of the (current date) as text
set CaminhoArquivo to (path to desktop as text) & "Teste" & iDay & "_" & iMonth & ".txt"

set TextoFinal to (my Texto_Novo("Qual ? a raz?o social", 53, "Raz?oSocial:", " ")) & (my Texto_Novo("Qual ? o endere?o", 49, "Endere?o:", " "))
my write_to_file(TextoFinal, CaminhoArquivo)
return TextoFinal

on Texto_Novo(pergunta, MeuLimiteCaracteres, Limitador, caracterespaco)
	set the_text to Limitador & text returned of (display dialog pergunta default answer "Escreva aqui a resposta" buttons "Ok" default button 1)
	set myCount to count of characters of the_text
	if myCount = MeuLimiteCaracteres then
		return the_text
	else if myCount > MeuLimiteCaracteres then
		return text 1 thru MeuLimiteCaracteres of the_text
	else
		repeat (MeuLimiteCaracteres - myCount) times
			set the_text to the_text & caracterespaco
		end repeat
		return the_text
	end if
end Texto_Novo

on write_to_file(this_data, target_file)
	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
		write this_data to the open_target_file starting at eof
		close access the open_target_file
	on error
		try
			close access file target_file
		end try
	end try
end write_to_file

Jon

Hi Jon!

This is what I was trying to do for the last 12 hours!

You guys (experienced guys) make it look much simpler allways!

Sorry for the portuguese comments!

I now can go on with my script, thanks to you.

My big Problem, was that the script would run the first time, and wouldn’t run the second time. I would insert a space before the list, it would recompile, work, and the second time wouldn’t work.

Thanks a lot for the help!

In your original script, because you had a variable set to the same name as the handler, when you compiled and ran the script, the handler took precedence and ran but when it ran, the handler set itself to a string so when it was called a second time in the script it would cause an error. The rest of the clean up involved a compound if-then-else statement and some simplified string functions.

Glad it helped.

Jon