Ask how to save data in LiveCode

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
katyperry
Posts: 1
Joined: Wed Oct 01, 2025 9:58 am

Ask how to save data in LiveCode

Post by katyperry » Wed Oct 01, 2025 9:59 am

Hello everyone, I'm new to LiveCode and want to save data from a field to a text file. I'm not sure which command to use and how to reopen saved data. Can someone share a basic example for me to refer to?

Klaus
Posts: 14242
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Ask how to save data in LiveCode

Post by Klaus » Wed Oct 01, 2025 10:32 am

Hi Katy,

welcome to the forum!

OK, we always need to store stuff in a folder where we have write permission,
which can be different on any platform (Mac/Win/Linux/Android/iOS).

During development you can use the folder where your stack resides, that is -> specialfolderpath("resources")
Later in a standalone you will need to use e.g. specialfolderpath("documents")
In that folder we have write permission on EVERY platform!

Do like like this:

Code: Select all

...
## This ... stands for the name of your "trigger" like "on mouseup" or whenever you want to do this
## 1. create the full path to the target file and put it into a variable:
put specialfolderpaht("resources") & "/" & "name of your resulting text file.txt" into tPathname

## 2. Write the text to that file:
put the text of fld "name of your field" into url("file:" & tPathname)
## Done :-)
...
We use the URL syntax with FILE, if we want to store "simple" text and BINFILE for binary data like exporting a screenshot/image from LC

Next, read that text file in:

Code: Select all

...
## 1. Again create the full path to the target file:
put specialfolderpaht("resources") & "/" & "name of your resulting text file.txt" into tPathname

## 2. VERY important, we need to check if that file is actually present at that place!
if there is NO file tPathname then
   # Inform the user, if neccessary
   answer "There is no such file!"
   ## Now leave the handler
   exit to top
end if

## 3. File is present, read it and put it into a field:
put url("file:" & tPathname) into fld ""name of your field"
## Done! :-)
...
Read up every new command in the dictionary and come back here if you have more question, which is very likely,
since LC is very powerfuld but has a steep learning curve, ;-)

Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10381
Joined: Wed May 06, 2009 2:28 pm

Re: Ask how to save data in LiveCode

Post by dunbarx » Wed Oct 01, 2025 4:57 pm

What Klaus said.

But as a new user, here is what I have done for a stack I use. This is on a Mac. This uses the "open File" command and creates and writes to a file on my desktop. In this simple example, the variable "filepath" is "/users/craignewman/desktop/myFile.txt" The data it writes is up to you.

Code: Select all

/open file filePath for text write --write text file
   write dataToWrite to file filePath  --your data here
   close file filePath
Another very nice variant is the ability to instantly create a PDF:

Code: Select all

open printing to pdf (yourFilePathHere & ".pdf")
Understand that LC will create a file if none exists, or write to one if it does.

This takes just a little practice...

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10180
Joined: Fri Feb 19, 2010 10:17 am

Re: Ask how to save data in LiveCode

Post by richmond62 » Wed Oct 01, 2025 5:13 pm

I tend to export RTF text from fields like this:

Code: Select all

on mouseUp
  ask file "Choose where you wish to export your text"
  if the result = "cancel" then
  exit mouseUp
  else
     put the RTFtext of field "myField" into url("file:" & it & ".rtf")
     get the longFilePath of it
     set the itemDelimiter to slash
     set the defaultFolder to item 1 to -2 of the longFilePath of it
     end if
end mouseUp

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10065
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Ask how to save data in LiveCode

Post by FourthWorld » Wed Oct 01, 2025 5:52 pm

katyperry wrote:
Wed Oct 01, 2025 9:59 am
Hello everyone, I'm new to LiveCode and want to save data from a field to a text file. I'm not sure which command to use and how to reopen saved data. Can someone share a basic example for me to refer to?
Hi Katy Perry. I enjoy your music.

What are you building with LiveCode?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 3129
Joined: Sun Jun 04, 2006 9:39 pm

Re: Ask how to save data in LiveCode

Post by stam » Thu Oct 02, 2025 12:21 pm

katyperry wrote:
Wed Oct 01, 2025 9:59 am
Hello everyone, I'm new to LiveCode and want to save data from a field to a text file. I'm not sure which command to use and how to reopen saved data. Can someone share a basic example for me to refer to?

Either you are a bot, or perhaps just lazy? I would argue finding answers online is a basic developer skill and if you are a real person and can’t do that maybe this isn’t for you…

I mean you can just google this exact question and get an answer. I went a step further and put your question verbatim into ChatGPT. This is the response:
“ChatGPT” wrote:
There are a few ways to do file I/O in LiveCode, but the simplest is to use the put ... into URL ("file:...") form.



Example: Save and Load from a Text File

Suppose you have a field on your card called fld "MyData".

To save the contents of the field to a text file:

Code: Select all

on mouseUp
   -- pick a location and name
   answer file "Save data as:"
   if it is empty then exit mouseUp

   put field "MyData" into URL ("file:" & it)
end mouseUp
To load it back later:

Code: Select all

on mouseUp
   answer file "Choose a file to open:"
   if it is empty then exit mouseUp

   put URL ("file:" & it) into field "MyData"
end mouseUp

What’s happening here:
• answer file shows a standard open/save dialog so the user can pick a file.
• put field "MyData" into URL ("file:" & it) writes the field contents directly to the file.
• put URL ("file:" & it) into field "MyData" reads the file back into the field.



A fixed-name file (no dialogs)

If you always want to use the same file (say, on desktop):

Code: Select all

 
 -- Save
put specialFolderPath("desktop") & "/myData.txt" into tFile
put field "MyData" into URL ("file:" & tFile)

-- Load
put specialFolderPath("desktop") & "/myData.txt" into tFile
put URL ("file:" & tFile) into field "MyData"


Here’s the “under-the-hood” way to do file I/O in LiveCode using open file, read, write, and close file. I’ll show save, load, and append patterns, plus basic error handling.



Save (overwrite) the contents of a field to a text file

Code: Select all

command SaveFieldToFile pFieldName
   -- Pick/save to a file
   answer file "Save data as:"
   if it is empty then exit SaveFieldToFile
   put it into tPath

   -- Open for write (creates or truncates)
   open file tPath for write
   if the result is not empty then
      answer error "Couldn't open file:" && the result
      exit SaveFieldToFile
   end if

   -- Write the text and close
   put field pFieldName into tText
   write tText to file tPath
   close file tPath
end SaveFieldToFile
Load (read) a text file into a field — line by line

Code: Select all

command LoadFileIntoField pFieldName
   answer file "Choose a file to open:"
   if it is empty then exit LoadFileIntoField
   put it into tPath

   open file tPath for read
   if the result is not empty then
      answer error "Couldn't open file:" && the result
      exit LoadFileIntoField
   end if

   put empty into tBuffer
   repeat until eof(tPath)
      read from file tPath for 1 line
      put it after tBuffer
   end repeat

   close file tPath
   put tBuffer into field pFieldName
end LoadFileIntoField
Append a single line to an existing file (or create it)

Code: Select all

command AppendLineToFile pLine
   answer file "Pick a file to append to:"
   if it is empty then exit AppendLineToFile
   put it into tPath

   open file tPath for append
   if the result is not empty then
      answer error "Couldn't open file for append:" && the result
      exit AppendLineToFile
   end if

   write pLine & cr to file tPath
   close file tPath
end AppendLineToFile
Read a file in fixed-size chunks (useful for big files)

Code: Select all

command LoadFileChunked pFieldName
   answer file "Choose a file to open:"
   if it is empty then exit LoadFileChunked
   put it into tPath

   open file tPath for read
   if the result is not empty then
      answer error "Couldn't open file:" && the result
      exit LoadFileChunked
   end if

   put empty into tBuffer
   repeat until eof(tPath)
      read from file tPath for 4096  -- bytes
      put it after tBuffer
   end repeat

   close file tPath
   put tBuffer into field pFieldName
end LoadFileChunked
Read a file in fixed-size chunks (useful for big files)

Code: Select all

command LoadFileChunked pFieldName
   answer file "Choose a file to open:"
   if it is empty then exit LoadFileChunked
   put it into tPath

   open file tPath for read
   if the result is not empty then
      answer error "Couldn't open file:" && the result
      exit LoadFileChunked
   end if

   put empty into tBuffer
   repeat until eof(tPath)
      read from file tPath for 4096  -- bytes
      put it after tBuffer
   end repeat

   close file tPath
   put tBuffer into field pFieldName
end LoadFileChunked

Notes & tips
• Check errors with the result: After open, read, or write, if the result is not empty, an error occurred (e.g., permissions, locked file, bad path).
• Line endings: read ... for 1 line reads up to the next line break; write ... doesn’t add a line break unless you add cr.
• Overwrite vs append:
• open ... for write truncates/creates the file.
• open ... for append writes at the end.
• open ... for read is read-only.
• Binary data: Use the binfile: form with the URL syntax, or still use open file but treat content as bytes. For pure text, the above is fine.
• UTF-8 text: The open/write form writes raw bytes in the engine’s default encoding. If you need UTF-8 reliably, the simplest approach is:

Code: Select all

put textEncode(field "MyData","UTF-8") into tUTF8
put tUTF8 into URL ("file:" & tPath)  -- URL form writes bytes directly
And to read back:

Code: Select all

 
put URL ("file:" & tPath) into tBytes
put textDecode(tBytes,"UTF-8") into field "MyData"
Common path helpers: specialFolderPath("documents"), specialFolderPath("desktop"), etc.

It really couldn’t be simpler to find examples and stuff like this online. The forum is here to help with problems but recently we’ve had a flood of apparent bots with one post asking stuff that is really quite basic and usually inane, invariably easy to find in LCs own documentation but just online searches and/or AI can give you clear and perhaps more helpful answers.

I don’t mean to be nasty and if you are a real person and respond, then perhaps this advice will be helpful to you. But like the myriad of other 1-post posters (bots, the point of whom I cannot fathom), I suspect we won’t see a reply from you. I would be ecstatic to be proven wrong though…

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10180
Joined: Fri Feb 19, 2010 10:17 am

Re: Ask how to save data in LiveCode

Post by richmond62 » Thu Oct 02, 2025 12:35 pm

Maybe the 'real' people in this generation are so lazy, that, not being bothered to take the trouble to find the answers to basic questions, their questions look as if they have been generated by bots. 8)

stam
Posts: 3129
Joined: Sun Jun 04, 2006 9:39 pm

Re: Ask how to save data in LiveCode

Post by stam » Thu Oct 02, 2025 12:36 pm

richmond62 wrote:
Thu Oct 02, 2025 12:35 pm
Maybe the 'real' people in this generation are so lazy, that, not being bothered to take the trouble to find the answers to basic questions, their questions look as if they have been generated by bots. 8)
I reckon they should use two AIs: one to generate the question and the other to answer it, and just leave this forum alone lol ;)

Post Reply