LiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCode LiveCode tipsHow do I get the path to common folders on my computer?

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. Yoe might want to stores preferences, settings and state beweeen uses of the app or export data to 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

The 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" .

  • MacOS Classic:   /HD/Desktop Folder
  • Mac OS X:  /Users/userName/Desktop
  • Windows:   C:/Documents and Settings/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 specialFolderPathfunction is as follows and is taken directly from the documentation:

specialFolderPath(folderIdentifier) 

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, refering to the file by a file URL. If the file does not exist is 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

References

The original version of this lesson was based on the stack "How to work with Files" by Ken Ray.

Supplemental materials:

http://www.sonsothunder.com/devres/revolution/tips/file010.htm

http://www.sonsothunder.com/devres/revolution/revolution.htm

2 Comments

Warren

This is especially handy for working with directories within your user's "home" directory. Two notes; Apple has available a long list of four-letter shortcuts to standard directory locations which may be usable and this does NOT work to navigate to the Applications directory in OS X. For example, using 'answer file "somePrompt" with specialFolderPath(Applications)' or 'answer file "somePrompt" with specialFolderPath('apps')' will open the dialog in whatever directory the application issuing this command is in, be it the Revolution IDE or your standalone. This would have to be considered a bug, IMO, and it demands that one use the actual path to 'Applications', which of course is not likely to be changed by a user ;)

Matthew Turney

on mouseUp
    
ask "What is the name of your file?"
put "/" & it & ".txt" into mynameholder
open file specialFolderPath("desktop") & mynameholder for write  
put field "Shakespeare" into myText   
write myText to file specialFolderPath("desktop") & mynameholder 
close file specialFolderPath("desktop") & mynameholder
end mouseUp

Add your comment

E-Mail me when someone replies to this comment