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

Create objects

Create objects

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

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

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

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

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

19 Comments

lestroso

This example is very easy and usefull to understand. You teach to us thery well. I hope you write again codes like this to improve our programming skill.
I thank you very much.

Bye,

Lestroso from Italy

www.fasasoftware.com

Jan Schenkel

Great lesson and easy to understand for people new to revTalk (while showing off how a one-liner replaces 10 lines of C/Java code.

An alternative to checking if 'it' is empty after an 'answer' or 'ask' command, is to check 'the result' and 'exit' the handler.

on mouseUp
answer file "Select a file"
-- exit if the user cancelled
if the result is "Cancel" then exit mouseUp
-- proceed with the user selection
...
end mouseUp

Cheer,

Jan Schenkel.

Greg DeVore

This is a great tutorial. I really like how you step through the process gradually.

One thing on the last step. I believe that with OS 10.6 the file type meta information is no longer available so tTXT won't work. Or am I confusing that with something else.?

Björnke von Gierke

What changed with 10.6 is the policy. Previously the guidelines by Apple stated that one should use a suffix _and_ a Mac OS filetype. As of 10.6, the filetype is not mandated anymore.

Of course many developers do not follow the guidelines by word, and many applications do never set any filetype (for example Apples own Text Edit). So I agree that in the mid to long term, the file type will most likely disappear.

Marilyn

For attaching files, I am finding that the syntax for Windows, and the syntax for Android is different. Currently I am commenting out the one I am not using, but this is a bit of a pain when testing the program out on Windows. Is there any way I can say,

If platform is Windows then . . .
else if platform is Android then . . .

Would this be the correct syntax?

Thank you, Marilyn

Hanson Schmidt-Cornelius

Hi Marilyn,

yes, you would use a very similar syntax to the one you posted. If you are sure you are only developing for these two platforms, then something like the following will be fine:

if the platform is "Win32" then
// Do Windows stuff
....
else
// Do Android stuff
....
end if

If there are possible other platforms, then you could extend the condition a bit like this:

if the platform is "Win32" then
// Do Windows stuff
....
else if the platform is "android"
// Do Android stuff
....
else
// All other platforms
answer "No rule to handle this platform" with "Okay"
end if

Kind Regards,

Hanson

Marilyn

Thank you! I used your syntax example and it works perfectly.

This saves me from commenting out Windows every time I want to deploy to Android, and vice versa.

Faisal

Hi there,
Great work, thanks.
Only one question though!
Does livecode allow text files to be draged into the field and show the content of the text file in the field? and how?
Best regards.
Faisal

Hanson Schmidt-Cornelius

Hi Faisal,

Yes, you can drag a file into a field and display the contents.

Have a look at the dictionary entry dragData.

You may also want to test this code in the script of the field and see if it does what you would expect:

on dragdrop
put URL ("file:" & line 1 of the dragData["files"]) into me
end dragdrop

Kind Regards,

Hanson

Faisal

Thanks Hanson,

That is excellent.

May I ask if I can have number of text field depending on number of text files in a folder if I drarg drop a folder in a stack?

Can this be possible?

Best regards.

Faisal

Hanson Schmidt-Cornelius

Hi Faisal,

you can drag and drop a folder into a text field. Just like you drop a file as per my last comment. If you drop a folder, you will have to list all of the files in the folder, create a field for each file found and then open the files and write their content to the respective fields. You may also want to consider the position of the text fields when they are opened.
This functionality will require a bit more code than I gave you in my last comment, but you can use much of the previous code to populate the field content.

Have a look at the following dictionary entires for more help on the parts of the functionality you will need:
command "create"
object "field"
property "rectangle"
function "files"
property "defaultFolder"
control structure "repeat"

Kind Regards,

Hanson

Faisal

Hi Hanson,

Thanks for the suggestion, I have had a look at the entries you asked, but I am afraid I need a little bit more help than that. For example how can I consider the position of the text fields when they are opened. what does that mean?
Sorry I am very new to LiveCode and still struggling due to lack of proper time investment.

Best regards,

Faisal

Faisal

Further to my last comment,

Also just noticed that some of the files are pictures, meaning the folder is mix of text and some picture files.

I dont think that the text field will be the right choice for pictures.

How can I go about that?

Best regards,

Faisal

Hanson Schmidt-Cornelius

Hi Faisal,

with position of the text fields, I referred to the location at which you may want to position the fields on the stack. Are you going to have a fixed number of text fields or are you going to have as many text fields opened as there are text files. You will then have to think about how you are going to managed these text field windows.
Use syntax such as:

set the location of field "ZZZ" to X,Y

If the files you are opening are mixed, then I am assuming that the file extensions may look something link .txt, .jpg, .png ... . If that is the case, then you can use text manipulation to extract the type of file you are processing. For example if you have a list of files that is return delimited, then you can categorise between text and image files as follows:

local tFile, tDirectoryFiles

// Populate the tDirectoryFiles here
// ...

set the itemDelimiter to "."
repeat for each line tFile in tDirectoryFiles
switch item -1 of tFile
case "txt"
// Do something with a text file here
break
case "png"
case "jpg"
// Do something with an image here
break
default
// Handle unknown files here
break
end switch
end repeat

Image files can be loaded with the following syntax:

new image
set the fileName of the last image to "SOME FILE NAME"

I have not run the code listed here, but it should give you an idea of what you will need to implement the functionality.

Kind Regards,

Hanson

maitreyee

Hi, I am building a GUI which could compare a list of words with the text in the field entered by a user, but I am unable to find a way to create a list and compare the input.Please help, I am a beginner

Elanor Buchanan

Hi maitreyee

I think what you are looking for is the words chunk expression. This allows you to look at each word in a field in turn.

Imagine you have 2 fields, one called "list" which contains your list of words and one called "user input", which the user enters text into. You could loop over the words of the "user input" field and check if they appear in the "list" field. This example assumes you have a button named "compare" which does the comparison when it is clicked.

on mouseUp
repeat for each word tWord in field "user input"
if tWord is among the lines of field "list" then
## do something

else
## do something else

end if
end repeat
end mouseUp

I hope that helps, there is a lesson explaining how to list all the unique words in a piece of text which might also help.

http://lessons.runrev.com/m/2592/l/125593-listing-all-the-unique-words-in-a-piece-of-text

Kind regards

Elanor

yasha

Hi
I'm not so sure this is the correct place for this question.
I'm building an Android application that reads from a text file that goes along with it. In the scripts I'm using relative paths to point to the file.
In development mode it works perfectly, but when I save the standalone app, copy the apk file to my cell and install it on my Android device, it seems that the txt file doesn't come along. How could I make sure that the file will be copied along with the apk file on installation?
I feel I'm missing something on the standalone configuration. I selected the text file on the copy file tab of the configuration menu, but it still didn't work
Thank you for these tutorials and the answers to the comments. They are great!
Yasha

Elanor Buchanan

Hi Yasha

On mobile any files you include in the Copy Files pane can be found my building a path to them using specialFolderPath("engine"). You can find more on reading and writing files on mobile in this lesson

http://lessons.runrev.com/m/4069/l/14301-how-do-i-read-write-to-files-on-mobile

I hope that helps.

Kind regards

Elanor

yasha

Thank you Elanor, I'll try it.
Best wishes
Yasha

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.