What's most reliable way to save text to external file

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

What's most reliable way to save text to external file

Post by urbaud » Mon Apr 07, 2025 11:10 pm

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
urbaud

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

Re: What's most reliable way to save text to external file

Post by dunbarx » Tue Apr 08, 2025 3:12 pm

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":

Code: Select all

  open file filePath for text write
      write dataToWrite to file filePath
      close file filePath
I am sure you can enhance this with additional parameters to the command "write".

Craig

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

Re: What's most reliable way to save text to external file

Post by dunbarx » Tue Apr 08, 2025 3:16 pm

Know that if you do not already have an explicit folder to write to, LC. (like HC before it) will create one.

Craig

FourthWorld
VIP Livecode Opensource Backer
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

Post by FourthWorld » Tue Apr 08, 2025 3:54 pm

dunbarx wrote:
Tue Apr 08, 2025 3:16 pm
Know that if you do not already have an explicit folder to write to, LC. (like HC before it) will create one.
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")
... would succeed even tho we probably don't want it to.

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

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

Re: What's most reliable way to save text to external file

Post by Klaus » Tue Apr 08, 2025 5:53 pm

dunbarx wrote:
Tue Apr 08, 2025 3:16 pm
Know that if you do not already have an explicit folder to write to, LC. (like HC before it) will create one.
Craig
No, it doesn't!
When you try, the result will be:
invalid URL: /Users/klaus2/Desktop/notpresentfolder/ttt.txt

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: What's most reliable way to save text to external file

Post by SparkOut » Tue Apr 08, 2025 7:19 pm

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

FourthWorld
VIP Livecode Opensource Backer
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

Post by FourthWorld » Tue Apr 08, 2025 7:50 pm

urbaud wrote:
Mon Apr 07, 2025 11:10 pm
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.
...
1. Open, read/write, Close format
2. Open, read/write binary, Close format
3. URL format
4. URL binfile format
The URL syntax is a shorthand way to accomplish the same underlying operations as open/write/close.

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

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

Re: What's most reliable way to save text to external file

Post by dunbarx » Tue Apr 08, 2025 7:55 pm

Klaus, Richard. Sparkout.

I misSpoke. The write command will create a new document named from the data supplied to it. :oops:

Craig

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: What's most reliable way to save text to external file

Post by urbaud » Wed Apr 09, 2025 9:49 pm

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

FourthWorld
VIP Livecode Opensource Backer
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

Post by FourthWorld » Thu Apr 10, 2025 12:58 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply