1

So, I'm incredibly new to LiveCode and I have an external file in the same directory as the .livecode file called 'words.txt', with english words, each on a new line. I plan to read this file into a variable and then pick a random word from that variable. However, I am stumped as to how I must find the file path and insert this into the syntax required for me to do this. My code is as follows:

    put url ("binfile:" & filePathGoesHere) into dictionary
    replace crlf with lf in dictionary
    replace numToChar(13) with lf in dictionary
    put any line of dictionary into randomword

The file path is supposed to be inserted into the code at filePathGoesHere. Once the program is compiled I will be moving it and its resources around a bit (from computer to computer), so, beyond the text file staying in the same folder as the compiled program, the file path will change. What extra code would I need to add to make this work, if the folder the compiled program and the txt file is in is called "MyProgram"?

Help is much appreciated, and if further specification is required I can provide it. I also have a folder called "resources" if moving it there can help.

notHalfBad
  • 213
  • 2
  • 9

2 Answers2

3

If the stack you're building is for your own use, you can place external files anywhere, but if you're going to deliver your stack to other users, you need plan where you external files are going to be placed, and how.

An easy way to determine the path to a file that sits immediately outside your stack is using the stack's filename:

put the fileName of this stack into theFilePath
set the itemDel to "/"
put "words.txt" into the last item of theFilePath

Now theFilePath variable will an absolute path reference to your external file. If the file is placed inside a folder "TextFiles" you can do this:

put the fileName of this stack into theFilePath
set the itemDel to "/"
put "TextFiles/words.txt" into the last item of theFilePath

If you're going to deliver your stack to other people, you should write your external file/s into a common system folder, or you need to use an installer to define where your files/folders will be placed. Common folder paths are found using the specialFolderPath function:

put specialFolderPath("Documents") into the theFolderPath

A somewhat recent addition to LiveCode is a "Resources" folder -- specialFolderPath("Resources") -- which can be handy for delivering on desktop and mobile platforms. Also, keep in mind that few of these folders allow writing to existing files contained in them for security reasons. "Preferences" and "Documents" are two examples of folders where you can change the contents of files.

The LC dictionary contains details of each of the folders.

Scott Rossi
  • 885
  • 5
  • 6
0

If you use the file: scheme instead of bindle: LiveCode will automatically convert end of line characters to LF, so that step may not be necessary. (Although you might need it if you are reading a text file produced in native Windows encoding on a Mac.) You don't even necessarily need to read it into a variable. You could do this:

put any line of URL ("file:" & specialFolderPath("resources") & "/words.txt") \
   into tRandomWord
Devin
  • 593
  • 1
  • 3
  • 8