Page 1 of 1

Save to text file

Posted: Sat Mar 30, 2019 9:24 pm
by micro04
Why is this not saving text from Field1 to the selected file.

Code: Select all

ask file "Save to"  with type "text files|TXT"
         put ".txt"  after it
         if it is empty then 
            break
         end if
                
         put field "Field1" into myText
         open file it for write
            
         write myText to file it
         Close  file it

Re: Save to text file

Posted: Sat Mar 30, 2019 11:00 pm
by FourthWorld
Difficult to say without error-checking.

Try adding this:

Code: Select all

   open file it for write
   if the result is not empty then
      answer the result &"( "& sysError() &")"
      exit to top
   end if

Re: Save to text file

Posted: Sat Mar 30, 2019 11:03 pm
by bogs
Heya micro04,

You have a lot of stuff going on you don't actually need. Try something like this instead -

Code: Select all

         ask file "Please select a name and location for this file..." with "Untitled.text"
         if it is not empty then
            put it into tmpFilePath
            put field 1 into url("File:" & tmpFilePath)
         end if
         break


Re: Save to text file

Posted: Sat Mar 30, 2019 11:06 pm
by Klaus
Hi micro04,

well...

Code: Select all

...
ask file "Save to"  with type "text files|TXT"
put ".txt"  after it

## Since you put something after IT, IT is of course never empty, so this:
if it is empty then 
## does not make sense!

 ##  break
 ## BREAK will exit a SWITCH structure, but you need to exit this handler.
 ## so BREAK will not work here
end if
...
You should get used to the shorter URL syntax, do like this:

Code: Select all

...
ask file "Save to"  with type "text files|TXT"
if IT = empty then
   exit mouseup ## if this is inside a mouseup handler!
end if

## IT may change when you least exspect IT, 
## so better put IT into another var immediately!
put it into tFileName
put ".txt"  after tFileName

## Now the URL one-liner :-)
put field "Field1" into url("file:" & tFileName)
...
That should do the trick.


Best

Klaus

Re: Save to text file

Posted: Sun Mar 31, 2019 1:56 am
by micro04
added the line

Code: Select all

close file tFileName
after

Code: Select all

put field "Field1" into url("file:" & tFileName)

Re: Save to text file

Posted: Sun Mar 31, 2019 4:01 am
by bogs
You don't need to close that. tFileName is only a variable holding 'it' in Klaus's example, 'it' is holding the path to the file your saving too.
Klaus wrote:
Sat Mar 30, 2019 11:06 pm
## break ## BREAK will exit a SWITCH structure, but you need to exit this handler. ## so BREAK will not work here
Your example wasn't clear if you were using this inside of a menu item or not (I made the poor assumption you were using this inside a save or save as menu item). If this is inside a handler, such as mouseUp on a button, then Klaus's point is valid, you'd need an exit instead.

Re: Save to text file

Posted: Sun Mar 31, 2019 1:42 pm
by Klaus
micro04 wrote:
Sun Mar 31, 2019 1:56 am
added the line

Code: Select all

close file tFileName
after

Code: Select all

put field "Field1" into url("file:" & tFileName)
I wrote ONE-LINER and also meant it!
No need to close the file when using the URL syntax, believe me! 8)

Do yourself a favour and read up all unknown terms in the dictionary for further infos!