Page 1 of 1

Questions re: URL read/write and if-then

Posted: Wed Jun 24, 2015 11:58 am
by ittarter
Hi, resident dunce here. Here's another two questions I have.

1. I'm trying to load existing user data into some variables. However, when I run this command, even though the program correctly says that the URL exists, it can't "put" data from the URL into a field or variable. I was under the impression that I could read and write files with the URL command.

2. I'm having unexpected problems with my if-then statements. When I apply changes to my card script I get the error "Handler: not a command" on the line "else". I've tried several variations and the only one that doesn't produce an error is if I begin each line in the if-then sequence with "then". However, none of the examples I've seen in the Dictionary or in any online tutorials ever does this. Why is mine behaving differently?

Code: Select all

if there is a URL ("file:" & gName & ".txt") then put line 2 of URL ("file:" & gName & ".txt") into gLanguage
   put line 1 of URL ("file:" & gName & ".txt") into fld "userdata"
   answer "User data loaded"
else answer "We cannot find your user data. Please set folder and try again."
setfolder
end if
I should add that I have learned a great deal from previous responses on this forum and feel like I've made huge steps forward :D

Re: Questions re: URL read/write and if-then

Posted: Wed Jun 24, 2015 12:29 pm
by Klaus
Hi ittarter,

1. there shouldn't be any problem with reading and writing files with the URL syntax!
Unless you try to write in folders where you do not have write permissions!

You should check the result after you try to read or write something and it fails.
The result = EMPTY on success!

Code: Select all

...
put url("file:xcxcxcx.txt") into tWhatever

## The result may give a hint about what goes wrong if it goes wrong:
if the result <> empty then
   answer the result
end if
...
2. I always use the correct and maybe longer snytax with IF THEN and never had problems this way,
which will also intend correctly and nicely when you hit TAB in the script editor:

Code: Select all

...
## Save some typing:
   put ("file:" & gName & ".txt") into tFileURL
   if there is a URL (tFileURL) then 
      put line 2 of URL (tFileURL) into gLanguage
      put line 1 of URL (tFileURL) into fld "userdata"
      answer "User data loaded"
   else
      answer "We cannot find your user data. Please set folder and try again."
      setfolder
   end if
...
Best

Klaus

Re: Questions re: URL read/write and if-then

Posted: Wed Jun 24, 2015 2:24 pm
by ittarter
Thank you, Klaus! I still can't get it to work when I try to put the entire URL into a single variable, so I have to type out the combination each time, but... it works!

Re: Questions re: URL read/write and if-then

Posted: Wed Jun 24, 2015 3:17 pm
by Klaus
What does THE RESULT say when you try that?

Re: Questions re: URL read/write and if-then

Posted: Wed Jun 24, 2015 4:30 pm
by FourthWorld
In addition to "the result" with file I/O the "sysError" function can be very helpful to let the OS inform you of the exact reason the file can't be read.

Re: Questions re: URL read/write and if-then

Posted: Thu Jun 25, 2015 12:32 am
by ittarter
Klaus wrote:What does THE RESULT say when you try that?
Nothing, as it should. I'll play around with it a bit more to see if I can come up with any other result.