How do I import a text file?
This lesson shows how I create a simple stack, which imports a text file and shows it in a field.
You can download the sample stack from this url: https://tinyurl.com/yb8xudfa
When I start a task, I always think about it in a visual way. As I want to show some text, I add a field and name it "text". Then, because I want to do a task, I add a button with the name of that task: "Open File...".
Asking about which file
First I want to ask about what file to show in the field. To do this, I add the following code to my button:
on mouseUp answer file "A text file" end mouseUp
But when I tested it, it didn't actually do much, it just showed a dialog, and nothing else changed.
Getting a value from the file dialogue
The following script does not only get the file path out of the dialogue, it also has some precautions against the user clicking cancel. I think it's always a good idea to give the user the chance to change his or her mind.
on mouseUp answer file "A text file" if it <> "" then put it into field "text" else --no file was selected, or cancel was pressed beep end if end mouseUp
Unfortunately, this does not exactly result in the contents of the file being imported.
Getting the content from a file path
Generally, if you want a whole document, the URL approach is the easiest way to get outside text sources into LiveCode. Therefore I use the URL keyword to get the content from the file path.
URL's can be files on your hard disk as well as on some remote web server. to differentiate them, LiveCode needs me to specify a protocol, before the actual URL. As I am trying to import a text file, I use file: as my protocol. Other valid examples of protocols are binfile:, http:// or ftp://.
on mouseUp answer file "A text file" if it <> "" then put it into theFilePath put URL ("file:" & theFilePath) into field "text" else --no file was selected, or cancel was pressed beep end if end mouseUp
The above script now finally does what I set out to do. But I could just select any non-text file, and LiveCode would show its contents as text in the field. I do not want that.
Beware: Sadly, the URL keyword is evaluated before the text combining keywords like &, && or , (comma). That is why I always have to use brackets when using the URL keyword, just as I did above.
Restrict the files that the user can select
As a final step, I'll add a "with type" specification to the dialogue. As I only want text files, I'll restrict the dialogue to files that have "txt" as a suffix. There's also an additional parameter, for the Macs file type ressource. Because I am on a Mac, I add the simple text descriptor "tTXT".
If I'd ever want to select a file that does not meet my criteria, I also added the possibility to choose among all files, using * as a wildcard. This will add a little drop down to the file dialog, showing the two options I specified.
on mouseUp answer file "A text file" with type ("text files|txt|tTXT" & return & "all files|*|*") if it <> "" then put it into theFilePath put URL ("file:" & theFilePath) into field "text" else --no file was selected, or cancel was pressed beep end if end mouseUp