Ask how to save data in LiveCode
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Ask how to save data in LiveCode
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?
Re: Ask how to save data in LiveCode
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:
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:
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
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 :-)
...
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! :-)
...
since LC is very powerfuld but has a steep learning curve,

Best
Klaus
Re: Ask how to save data in LiveCode
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.
Another very nice variant is the ability to instantly create a 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
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
Code: Select all
open printing to pdf (yourFilePathHere & ".pdf")
This takes just a little practice...
Craig
-
- Livecode Opensource Backer
- Posts: 10180
- Joined: Fri Feb 19, 2010 10:17 am
Re: Ask how to save data in LiveCode
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
-
- VIP Livecode Opensource Backer
- Posts: 10065
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Ask how to save data in LiveCode
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Ask how to save data in LiveCode
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:
To load it back later: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
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 fileLoad (read) a text file into a field — line by lineCode: 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
Append a single line to an existing file (or create it)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
Read a file in fixed-size chunks (useful for big files)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
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:And to read back:Code: Select all
put textEncode(field "MyData","UTF-8") into tUTF8 put tUTF8 into URL ("file:" & tPath) -- URL form writes bytes directly
Common path helpers: specialFolderPath("documents"), specialFolderPath("desktop"), etc.Code: Select all
put URL ("file:" & tPath) into tBytes put textDecode(tBytes,"UTF-8") into field "MyData"
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…
-
- Livecode Opensource Backer
- Posts: 10180
- Joined: Fri Feb 19, 2010 10:17 am
Re: Ask how to save data in LiveCode
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. 

Re: Ask how to save data in LiveCode
I reckon they should use two AIs: one to generate the question and the other to answer it, and just leave this forum alone lolrichmond62 wrote: ↑Thu Oct 02, 2025 12:35 pmMaybe 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.![]()
