Thursday, September 20, 2018

#1 2008-11-21 09:11:17 am

roboFlop
Member
Registered: 2008-11-21
Posts: 2

Need help solving "end of file error"

*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.


Filed under: file, text, save, as

Offline

 

#2 2008-11-21 09:43:26 am

Martin Michel
Administrator
From:: Berlin, Germany
Registered: 2008-03-03
Posts: 701
Website

Re: Need help solving "end of file error"

roboFlop wrote:

I am soo confused. Any help would be much appreciated.


Hi roboFlop,

My first question is: Why don't you save the variables as properties directly in the script? I think saving the results into files is way to complicated.

Applescript:


property myname : missing value

on run
   tell me
       activate
       display dialog "Last name entered:" & return & return & myname
   end tell
   
   tell me
       activate
       display dialog "Please enter your name:" default answer "" buttons {"Enter"} default button 1
       set dlgresult to result
   end tell
   set myname to text returned of dlgresult
end run

But if you really want to use «do shell script» to save your results into files, then you must use «quoting»:

This works:

Applescript:


do shell script "echo My name is Martin > ~/Desktop/test.txt"

This won't:

Applescript:


do shell script "echo My name is M'artin > ~/Desktop/test.txt"

But this does:

Applescript:


set phrase to quoted form of "My name is M'artin"
do shell script "echo " & phrase & " > ~/Desktop/test.txt"

Also all file paths should be quoted!


Plonum - Convert images to iWork Numbers documents

Offline

 

#3 2008-11-21 10:08:17 am

cwtnospam
Member
Registered: 2008-06-08
Posts: 274

Re: Need help solving "end of file error"

You're not closing the file access.

Applescript:

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"

Applescript:

set myhome to path to home folder as text
set lastThingSaid to (myhome & "Memory:LastThingSaid.txt") as alias
set x to open for access lastThingSaid
set iSaid to read x
close access lastThingSaid
say "I Said " & iSaid

Applescript:

set myhome to path to home folder as text
set userName to (myhome & "Memory:yourName.txt") as alias
set x to open for access userName
set yourName to read x
close access userName
say "your name is " & yourName

do shell script "echo your name is " & yourName & "> ~/Memory/LastThingSaid.txt"

Applescript:

set myhome to path to home folder as text
set lastThingSaid to (myhome & "Memory:LastThingSaid.txt") as alias
set x to open for access lastThingSaid
set iSaid to read x
close access lastThingSaid
say "I Said " & iSaid

Offline

 

#4 2008-11-21 10:22:45 am

roboFlop
Member
Registered: 2008-11-21
Posts: 2

Re: Need help solving "end of file error"

Thanks martin.

I think it just wanted the variable in quoted form. It seems to work fine for now.

The reason I want them to be files is so that I can access them as files outside of the script. But I will consider doing it your way if more problems arise.

Much thanks.

Offline

 

#5 2008-11-21 11:24:22 am

cwtnospam
Member
Registered: 2008-06-08
Posts: 274

Re: Need help solving "end of file error"

Did you see my post? If you don't close the file, the next read will start from where the last left off, and that's at the end of the file!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)