livecode.com
  • Home
  • Download
  • Developers
  • Academy
  • Community
  • Store
  • Support
  • About
LiveCode Lessons » Getting Started with LiveCode Development » Reading and Writing to File

Topics

  • Before you start 2
    • The Structure of a LiveCode Application
    • The LiveCode Message Path
  • Basic Concepts 7
    • Adding Objects to a Stack
    • Navigating Around a Stack
    • LiveCode Properties
    • Scripting a Button
    • Communicating with the User
    • Working With Text
    • Reading and Writing to File
  • LiveCode Objects 8
    • Using Buttons and Default Buttons
    • Using Fields
    • Using Radio Buttons
    • Using Option Menus
    • Text in the Tab Panel
    • Using Progress Bars
    • Using Images
    • Using Players
  • Building a User Interface 2
    • Using Button Icons and Background Images
    • Using Visual Effects
  • Building a Standalone Application 1
    • Building Standalone Applications
  • Your first LiveCode Application 1
    • Hello World! 3 ways
  • The next step - Creating a 3-5-7 Game 3
    • Game 3-5-7 - A Simple Interface
    • Game 3-5-7 - Basic Functionality
    • Game 3-5-7 - Game Logic

Last Updated

Oct 11, 2011

Download Lesson PDF

Other Resources

  • Getting Started with LiveCode

  • Get Up and Running with LiveCode
  • Getting Started with LiveCode Development
  • LiveCode Concepts

  • Features, concepts and aspects of LiveCode
  • LiveCode Lessons

  • How To - Step-By-Step Guides To Tasks In LiveCode
  • How To - LiveCode Server Tasks
  • How To - LiveCode Mobile Tasks
  • How To - LiveCode Sample Scripts
  • How to - LiveCode Marketplace Products
  • How to Purchase and License LiveCode
  • Tutorials

  • Creating a Video Library Application
  • Data Grid

  • LiveCode Data Grid
  • Data Grid Tips & Tricks
  • Converting the Stock Program

Comments

4 for this lesson

  • Prev: Working With Text
  • Next: Using Buttons and Default Buttons

Reading and Writing to File

Your application might need to be able to read data in from a file or write data out to a file.

This might be a way of importing data or text into your application or saving preferences, settings or the current state of the application when it is closed.

With LiveCode reading frmm and writing to files is very simple.

The URL Keyword

As explained in the lesson Working With Text containers are sources of information. One of the LiveCode container types is URL. A URL is a container for a file (or other resource), which is on the same system the application is running on, or on another system that's accessible via the Internet.

URLs in LiveCode are written like the URLs you see in a browser. You use the URL keyword to designate a URL, enclosing the URL's name in double quotes:

put field "Info" into URL "file:myfile.txt"
get URL "http://www.example.org/stuff/nonsense.html"
put URL "ftp://ftp.example.net/myfile" into field "Data"

Working with URL Contents

You use a URL like any other container. You can get the content of a URL or use its content in any expression. You can also put any data into a URL, putting data into a file that does not exist causes it to be created.

You can also use chunk expressions with URLs, as with any other type of container:

put field "Info" into line 1 of URL "file:myfile.txt
get word 10 to 15 of URL "file:myfile.txt"

The http Scheme

An http URL designates a document from a web server:

put URL "http://www.runrev.com" into field "text"

When you use an http URL in an expression, LiveCode downloads the URL from the server and substitutes the downloaded data for the URL.

When you put something into an http URL, LiveCode uploads the data to the web server.

An Example

Zoom

Create a stack with a field called "text" and a button called "Choose file". The script of the button asks the user to choose a file and then puts the contents of that file into the field.

on mouseUp
    ## Ask the user to choose a file
    answer file "Please choose a file"

    ## If the dialog is not cancelled put the path to the selected file into a variable
    ## Use the URL keyword to put the contents of the file into a field
    if the result is not "cancel" then
        put it into tFilename
        put url ("file:" & tFilename) into field "text"
    end if
end mouseUp

Finding the Path to Common Folders

A very common task is saving a file to, or reading a file from, a standard location. You can use the specialFolderPath function to build paths to system related folder and standard locations such as the Documents folder, desktop, preferences or home directory.

For example:

put specialFolderPath("documents") & "/Test Doc.txt" into tFilePath

This command puts the full path to a file in the documents folder called "Text Doc.txt" into the variable tFilePath. You can then use that path to read from or write to the file.

  • Prev: Working With Text
  • Next: Using Buttons and Default Buttons

Comments (4)

William Volkoff Sunday Nov 04 at 12:33 PM

Q: The following line:

put URL "http//www.runrev.com" into field "GetText"

in the button script only produces the name of the website into the field text as in:

"http//www.runrev.com",

However, when inserted into the Messag Box, the whole website code is insterted, what is going on?

Thank you.

Hanson Schmidt-Cornelius Monday Nov 05 at 03:40 AM

Hi William,

there is a little typing error in the URL. Try http://www.runrev.com

Kind Regards,

Hanson

Tyrone Armstead Friday Nov 23 at 11:01 AM

Currently I am reading and writing to the desktop, I want to prepare the code to read within the app bundle for a standalone. How do I change the path to read within the applicaiton bundle? I am using the specialfoderpath.
Thank you

Hanson Schmidt-Cornelius Monday Nov 26 at 08:00 AM

Hi Tyrone,

it really depends on what platform you intend to deploy to and the information you are reading and writing. For a mobile application you may want to consider if the data should remain available when the application is relaunched or be backed up as part of iTunes sync.
You can find out more information on what paths are available and what they do in the dictionary entry for the function "specialFolderPath".

Kind Regards,

Hanson

Add your comment

E-Mail me when someone replies to this comment
Brought to you by RunRev Ltd, Registered in Scotland, No. SC200728