*Here is the conundrum. I have the following scripts that work perfectly:
set x to (random number from 1 to 10)
say "the number you are thinking of, is " & (x as text)
do shell script “echo The number you were thinking of, was " & x & " > ~/Memory/LastThingSaid.txt”
*new script
set lastThingSaid to “Users:user:Memory:LastThingSaid.txt”
set x to open for access file lastThingSaid
set iSaid to read x
say "I Said " & iSaid
*as you can see. I have the computer say “the number you are thinking of is (random number)”, and in the next script, the computer remembers the number it guessed and says, “I said, the number you were thinking of was (same random number)” Everything works fine so far. But I am trying to do a more complicated series of scripts based on this idea:
set userName to “Users:user:Memory:yourName.txt”
set x to open for access file userName
set yourName to read x
say "your name is " & yourName
do shell script "echo your name is " & yourName & “> Users/user/Memory/LastThingSaid.txt”
*I am try to get the computer to remember the variable yourName by reading it from the file yourName.txt. This works sometimes, but usually, and this is the part I don’t understand, the shell script clears the file LastThingSaid.txt instead of writing it. so when I run:
set lastThingSaid to “Users:user:Memory:LastThingSaid.txt”
set x to open for access file lastThingSaid
set iSaid to read x
say "I Said " & iSaid
*I get an “end of file error” on command “read x” because LastThingSaid.txt was cleared by a script that was supposed to write to it. This is the part I really need help with. If you know how to fix this you don’t need to keep reading.
*It gets REALLY weird when I try to do the following:
set lastThingSaid to “Users:user:Memory:LastThingSaid.txt”
set x to open for access file lastThingSaid
try
set iSaid to read x
on error
do shell script “echo …uhm …wait …sorry …i forgot what I said now. > Users/user/Memory/LastThingSaid.txt”
end try
set iSaid to read x
say "I Said " & iSaid
*When the “set iSaid to read x” command within the try loop works, it does not change the file yourName.txt and moves on. But when it does the exact same command outside of the try loop, I get a “end of file error” on “read x” even though the file yourName.txt has not been cleared and in fact contains the correct data. When I get rid of the line “set iSaid to read x” outside of the try loop, the script runs and the computer accurately remembers the last thing it said. The only problem is, if I get rid of the second “set iSaid to read x”, LastThingSaid.txt will still get cleared by ‘do shell script "echo your name is " & yourName & “> Users/user/Memory/LastThingSaid.txt”’ and iSaid wont be defined when “read x” fails.
I am soo confused. Any help would be much appreciated.