How do I get the path to common folders on my computer?
When creating apps you will often need to save data outside your app. You might want to store preferences, settings and state between uses of the app or export data to an accessible location on disk.
This lesson will explain how to use the
specialFolderPath function to specify read from/write to common paths on the different operating systems.
Differences between platforms
specialFolderPath function is especially useful in cross-platform development work in which your stack will need, for example, to keep track of a user's performance, preferences, etc.
Consider, for example, the pathing differences between Mac OS X and Windows with respect to writing files to "the desktop":
Operating System Path to "the Desktop" .
- Mac OS X: /Users/username/Desktop
- Windows: C:/Users/username/Desktop
- Linux: /home/username/Desktop
While, for the sake of making the written files obvious so that you won't forget to delete them later because they are buried down deep in some obscure folder, we will be writing our files to the desktop, you should be aware that LiveCode allows you to write them to a number of places using the
specialFolderPath function; check the LiveCode Dictionary for more details.
The syntax of the
specialFolderPath function is as follows:
Parameters: The folderIdentifier is one of the following:
For OS X systems:
- Desktop: The user's Desktop folder
- System: The active System Folder
- Fonts: The system Fonts folder
- Preferences: The Preferences folder inside the user's Library folder
For Windows systems:
- Desktop: For icons on the desktop
- System: For system components
- Start: For items in the Start menu
- Documents: For user documents
- Fonts: For font storage
- Temporary: For storage of temporary files
For Unix systems:
- Home: The current user's home directory (e.g. "/home/fred")
- Desktop: The current user's desktop (e.g. "/home/fred/Desktop")
- Temporary: For storage of temporary files (typically "/tmp")
In addition, CSIDL numbers maybe used to identify certain Windows directories; from the documentation, these are:
- 0x001a: The user application data folder (e.g. "C:/Documents and settings/Fred/Application Data")
- 0x0023: The "all users" application data folder (e.g. "C:/Documents and settings/all users/Application Data")
- 0x000d: The "My Music" folder
- 0x000e: The "My Videos" folder
- 0x0027: The "My Pictures" folder
- 0x000a: The recycle bin
More information on obtaining CSIDL values can be found at http://vbnet.mvps.org/index.html?code/browse/csidlversions.htm
Example of using the specialFolderPath function to write a file to disk using the URL keyword
This code snippet puts the contents of a LiveCode field into a text file on the desktop of a Mac or Windows system, referring to the file by a file URL. If the file does not exist it is created.
on mouseUp put field "Rotten Wood" into url ("file:" & specialFolderPath("desktop") & "/myData.txt") end mouseUp
Example of using the specialFolderPath function to write a file to disk using file commands
The below code snippet, which writes data from a LiveCode field to a text file on the desktop of a Mac or Windows system, serves as an example of using the
specialFolderPath function using
on mouseUp open file specialFolderPath("desktop") & "/myData.txt" for write -- creates a file on a Mac or Windows "desktop" for LiveCode to write to put field "Rotten Wood" into myText -- places the text from the field "Rotten Wood" into a local variable write myText to file specialFolderPath("desktop") & "/myData.txt" -- writes the text placed in the local variable into the open text file we created close file specialFolderPath("desktop") & "/myData.txt" -- closes/saves the text file containing the text form the field/local variable into a text file on the desktop end mouseUp
Example of using the specialFolderPath function to delete a file from disk
Below is an example of using the
specialFolderPath function in conjunction with the
delete file command to delete a file from the desktop of a Mac or Windows system when a stack is closed (perhaps a temporary file that the stack created but no longer needs):
on closeStack delete file specialFolderPath("desktop") & "/Temp.txt" end closeStack
The original version of this lesson was based on the stack "How to work with Files" by Ken Ray.