Save to text file

Deploying to Windows? Utilizing VB Script execution? This is the place to ask Windows-specific questions.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
micro04
Posts: 5
Joined: Sat Mar 30, 2019 12:48 am

Save to text file

Post by micro04 » Sat Mar 30, 2019 9:24 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6495
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Save to text file

Post by FourthWorld » Sat Mar 30, 2019 11:00 pm

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
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

bogs
Posts: 3101
Joined: Sat Feb 25, 2017 10:45 pm

Re: Save to text file

Post by bogs » Sat Mar 30, 2019 11:03 pm

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

Image

Klaus
Posts: 10820
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Save to text file

Post by Klaus » Sat Mar 30, 2019 11:06 pm

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

micro04
Posts: 5
Joined: Sat Mar 30, 2019 12:48 am

Re: Save to text file

Post by micro04 » 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)

bogs
Posts: 3101
Joined: Sat Feb 25, 2017 10:45 pm

Re: Save to text file

Post by bogs » Sun Mar 31, 2019 4:01 am

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

Klaus
Posts: 10820
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Save to text file

Post by Klaus » Sun Mar 31, 2019 1:42 pm

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!

Post Reply

Return to “Windows”