What's most reliable way to save text to external file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
What's most reliable way to save text to external file
In LiveCode, I want to know which is the best and most reliable and robust way to Save and retrieve (Open) text in a text field from/to an external file. Part of the Save/Open function would be to use htmlText to format the text for size, color and style. Also, where would the ideal place be for the external file to be saved to: Desktop, documents, Preferences, home, engine, etc. Which of the following formats is the best, most reliable way to save and retrieve the text:
1. Open, read/write, Close format
2. Open, read/write binary, Close format
3. URL format
4. URL binfile format
1. Open, read/write, Close format
2. Open, read/write binary, Close format
3. URL format
4. URL binfile format
urbaud
Re: What's most reliable way to save text to external file
Hi.
I do just a little of this, but basically I simply build a string like "myDestination.txt" and put it into a variable "filePath":
I am sure you can enhance this with additional parameters to the command "write".
Craig
I do just a little of this, but basically I simply build a string like "myDestination.txt" and put it into a variable "filePath":
Code: Select all
open file filePath for text write
write dataToWrite to file filePath
close file filePath
Craig
Re: What's most reliable way to save text to external file
Know that if you do not already have an explicit folder to write to, LC. (like HC before it) will create one.
Craig
Craig
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: What's most reliable way to save text to external file
When attempting to write to a folder that doesn't exist, LC should note that as an error in the result, rather than create the folder.
If this has changed then this typo:
Code: Select all
put tData into URL ("file:~/Desjtop")
All these years I've had to use "create folder" to create folders I want to use that don't already exist.
Do I misunderstand what you wrote?
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: What's most reliable way to save text to external file
@Craig, naybe you are remembering it the other way round? If you script a "create folder" command and a folder already exists, then LC does not return an error.
I am intrigued now and want to test this but can't at the moment.
For the OP, "best" or "most robust" or "optimal" are all subjective. Pretty much all the suggestions you mentioned are equally valid, there might be just the tiniest but of overhead in maintaining the open handles on a file before explicitly closing but for the mentioned task of saving html text from a field into a file, I would say the simpler way would be to put into url ("file:" & thePath), and let LiveCode handle the file system operations without thinking about it.
Where to store the file depends entirely on the expectation of what it is for. "engine" is not a valid location these days, where "resources" should be used instead, but that is not a writeable location on some operating systems (mobile).
If the file is meant to keep data relevant to your app (especially if not meant to be viewed by the user independently of your app) that suggests a "preferences" type location. If it is meant to be data exported so that other apps can access it, then perhaps "documents" but it really depends on what the intention is.
I am intrigued now and want to test this but can't at the moment.
For the OP, "best" or "most robust" or "optimal" are all subjective. Pretty much all the suggestions you mentioned are equally valid, there might be just the tiniest but of overhead in maintaining the open handles on a file before explicitly closing but for the mentioned task of saving html text from a field into a file, I would say the simpler way would be to put into url ("file:" & thePath), and let LiveCode handle the file system operations without thinking about it.
Where to store the file depends entirely on the expectation of what it is for. "engine" is not a valid location these days, where "resources" should be used instead, but that is not a writeable location on some operating systems (mobile).
If the file is meant to keep data relevant to your app (especially if not meant to be viewed by the user independently of your app) that suggests a "preferences" type location. If it is meant to be data exported so that other apps can access it, then perhaps "documents" but it really depends on what the intention is.
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: What's most reliable way to save text to external file
The URL syntax is a shorthand way to accomplish the same underlying operations as open/write/close.urbaud wrote: ↑Mon Apr 07, 2025 11:10 pmIn LiveCode, I want to know which is the best and most reliable and robust way to Save and retrieve (Open) text in a text field from/to an external file.
...
1. Open, read/write, Close format
2. Open, read/write binary, Close format
3. URL format
4. URL binfile format
Have you experienced some unreliability with either method? If so, let's examine the code to see where error checking can be added to guide it.
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: What's most reliable way to save text to external file
Klaus, Richard. Sparkout.
I misSpoke. The write command will create a new document named from the data supplied to it.
Craig
I misSpoke. The write command will create a new document named from the data supplied to it.

Craig
Re: What's most reliable way to save text to external file
To all of you who replied to my post I want to thank you. The reason I'm asking about this is because I have on occasion, inadvertently, deleted the external file. I don't want that to happen again. I'm a LiveCode hobbyist and do coding to keep my old brain (80 plus yrs.) from declining to fast. I use the file to keep bits and pieces of info that might be needed or interesting in the future. The app I built was to get rid of paper notes and consolidate the info in one place where I could search it.
Also, as an added task for myself and for fun, I asked several AI chat bots the same question. I thought I'd share the simple results with you. See below
Claude = open read/write binary close
Chatgpt = URL not binary
Gemini = open read/write close
Ninja agent = open read/write close
Deepseek = URL binfile
YesChat = URL not binary
Each of them had long explanations for their recommendations. Interesting exercise. Again, thanks to all of you for responding to my post.
Also, as an added task for myself and for fun, I asked several AI chat bots the same question. I thought I'd share the simple results with you. See below
Claude = open read/write binary close
Chatgpt = URL not binary
Gemini = open read/write close
Ninja agent = open read/write close
Deepseek = URL binfile
YesChat = URL not binary
Each of them had long explanations for their recommendations. Interesting exercise. Again, thanks to all of you for responding to my post.
urbaud
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: What's most reliable way to save text to external file
Regenerative software is designed to emulate human writing, but literally cannot understand a single word it writes.
Given the statistical probability models or uses, it often guesses correctly. And sometimes not so much.
We've addressed the difference between the URL syntax as a shortcut for open/write/close.
The last bit is the difference between reading and text and reading as binary.
Reading as text alters the data, to make it conform to the standards of the host OS. Line endings may be changed, and a couple other edge cases I can't recall offhand.
For complete fidelity with writing and reading, use the binary options.
Given the statistical probability models or uses, it often guesses correctly. And sometimes not so much.
We've addressed the difference between the URL syntax as a shortcut for open/write/close.
The last bit is the difference between reading and text and reading as binary.
Reading as text alters the data, to make it conform to the standards of the host OS. Line endings may be changed, and a couple other edge cases I can't recall offhand.
For complete fidelity with writing and reading, use the binary options.
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