Ion.io
Contains properties and methods that provide I/O functionality.
Available since: 1.0
Properties
Get the path to a temporary file area on the system for the active user.
E.g.:
- Mac OS X/Linux:
/tmp/ - Windows:
C:/Users/jarred/AppData/Local/Temp/
// Read our temp file that is storing cache
var result = Ion.io.readFile(Ion.io.tempPath + 'MyApp.cache');
if (result.success) {
// Cache was present and read successfully, so let's load it up!
MyApp.loadCache(result.data);
}
Available since: 1.0
Get the path to a data file area on the system for the active user.
NOTE: In the near future, custom Organization and Application names will be configurable. The Organization
and Application names currently default to "Sencha" and "Sencha Ion" respectively.
E.g.:
- Mac OS X:
/Users/jarred/Library/Application Support/OrgName/AppName/ - Windows:
C:/Users/jarred/AppData/Local/OrgName/AppName/ - Linux:
/home/jarred/.local/share/data/OrgName/AppName/
Available since: 1.0
Get the path to the desktop area on the system for the active user.
E.g.:
- Mac OS X:
/Users/jarred/Desktop/ - Windows:
C:/Users/jarred/Desktop/ - Linux:
/home/jarred/Desktop/
Available since: 1.0
Gets the path to the documents or "My Documents" area on the system for the active user.
E.g.:
- Mac OS X:
/Users/jarred/Documents/ - Windows:
C:/Users/jarred/Documents/ - Linux:
/home/jarred/Documents/
Available since: 1.0
Gets the path to the home directory on the system for the active user.
E.g.:
- Mac OS X:
/Users/jarred/ - Windows:
C:/Users/jarred/ - Linux:
/home/jarred/
Available since: 1.0
An array of directory paths currently being watched.
An array of directory paths currently being watched.
Available since: Desktop Packager 1.1
Methods
Opens native directory chooser dialog.
// Let the user choose where to save our text file.
var result = Ion.io.browseDirectories({
path: Ion.io.userHomePath
});
if (result.success) {
Ion.io.writeFile(result.value + '/myfile.txt', data);
}
Available since: 1.0
Parameters
- options : Object (optional)
Object containing the following options:
- caption : String (optional)
The title of the dialog window.
Defaults to:
"Choose Directory" - path : String (optional)
The initial directory that the user will begin navigating.
pathwill be canonicalized (seeIon.io.canonicalizePath())NOTE: This option has no effect on Windows at this time.
- caption : String (optional)
Returns
- Object
Returns an object with the following structure:
success: Boolean
trueif the user selected a directory.falseif the user closed/cancelled the dialog.value: String
The full path to the selected directory.
Opens native file chooser dialog.
// Let the user choose the JSON file to open and read.
var result = Ion.io.browseFiles({
path: Ion.io.userHomePath,
type: 'open',
filters: [{
name: 'JSON Files',
pattern: '*.json'
}]
});
if (result.success) {
var read = Ion.io.readFile(result.value);
if (read.success) {
var obj = JSON.parse(read.data);
// Do something with obj...
}
}
Filters
On some platforms the file browser dialog supports a list of filters that the end user can use to filter which files are shown in the dialog. Each filter in the list consists of two parts:
name: Descriptive text that the end user will see.pattern: The pattern (String) or patterns (String[]) this filter matches.
Filters can be defined as Objects with the fields name and pattern or as an
Array of two parts, the name followed by the pattern String or String[].
For example:
filters: [
['Image Files', ['*.jpg', '*.jpeg', '*.png']],
['All Files', '*.*']
]
and
filters: [{
name: 'Image Files',
pattern: ['*.jpg', '*.jpeg', '*.png']
}, {
name: 'All Files',
pattern: '*.*'
}]
both define the same list of filters.
Available since: 1.0
Parameters
- options : Object (optional)
Object containing the following options:
- caption : String (optional)
The title of the dialog window.
Defaults to:
"Open File"or"Save File"based ontype - defaultValue : String (optional)
The name of the file that will attempt to be pre-populated as the selected value.
- filters : Array[]/Object[] (optional)
Defines the list of filters to provide in the file browser dialog. See above for details.
Note: Mac OS X will enable selections which match at least one of the provided filter patterns. This is temporary.
Defaults to:
[["All Files", "*.*"]] - path : String (optional)
The initial directory that the user will begin navigating.
pathwill be canonicalized (seeIon.io.canonicalizePath()) - type : String (optional)
Whether this is a save dialog (allows a path/value that is non-existent) or an open dialog (the path/value must exist on the file system).
Defaults to:
"save"
- caption : String (optional)
Returns
- Object
Returns an object with the following structure:
success: Boolean
trueif the user selected a file.falseif the user closed/cancelled the dialog.value: String The full path to the selected file.
Canonicalizes a file system path if necessary.
- "~/" prefix expands to
Ion.io.userHomePath() - "-/" prefix expands to
Ion.io.userDataPath()
If no convertable prefix is found, the path passed in as a parameter is returned unmodified. * All Ion.io member functions that take a path as a parameter, call this function internally,
so you can pass in paths using the above prefixes.
// Open `config.json` in user's home directory
var config = Ion.io.readFile(Ion.io.canonicalizePath('~/config.json'));
// Can also open the file like this, because `Ion.io.readFile()` calls
// `canonicalizePath()` for you:
var config = Ion.io.browseFiles({path: '~/config.json'});
Available since: Desktop Packager 1.1
Parameters
- The : String
path to be converted.
Returns
- String
The canonicalized path.
If fromPath is a Directory: copies deeply all files and directories from fromPath to toPath.
If fromPath is a File: copies the file to toPath.
If the destination directory does not already exist, an attempt to create it will be made.
// Copy dir1 and its contents to dir2, resulting in /path/to/dir2/dir1/<dir1_contents>
if (!Ion.io.copyPath('/path/to/dir1', '/path/to/dir2')) {
alert('Failed to copy directory!');
}
// Copy dir1 contents to dir2, resulting in /path/to/dir2/<dir1_contents>
if (!Ion.io.copyPath('/path/to/dir1/.', '/path/to/dir2')) {
alert('Failed to copy directory!');
}
// Copy file to destination path, resulting in /path/to/dir/new_file
if (!Ion.io.copyPath('/path/to/file', '/path/to/dir/new_file')) {
alert('Failed to copy file!');
}
// Copy file to dir, resulting in /path/to/dir/file
if (!Ion.io.copyPath('/path/to/file', '/path/to/dir/.')) {
alert('Failed to copy file!');
}
Available since: 1.0
Parameters
- fromPath : String
The file or directory to copy to
toPath.toPathwill be canonicalized (seeIon.io.canonicalizePath()) - toPath : String
The destination path to where
fromPathwill be copied.fromPathwill be canonicalized (seeIon.io.canonicalizePath())
Returns
- Boolean
Returns whether or not the copy operation finished completely and successfully.
If path is a Directory: deeply deletes all files and folders from path and then deletes path.
If path is a File: deletes path.
// Delete dir completely
if (!Ion.io.deletePath('/path/to/dir')) {
alert('Failed to delete directory!');
}
// Delete file
if (!Ion.io.deletePath('/path/to/file')) {
alert('Failed to delete file!');
}
Available since: 1.0
Parameters
- path : String
The file or directory to delete.
pathwill be canonicalized (seeIon.io.canonicalizePath())
Returns
- Boolean
Returns whether or not the delete operation finished completely and successfully.
Lists the names of all files and directories under the specified path.
This operation is not deep and will only list immediate children of path.
var items = Ion.io.listDirectory(Ion.io.userDataPath + 'records', { directories: false });
for (var i = 0; i < items.length; i++) {
MyApp.loadRecordFromFile(Ion.io.userDataPath + 'records/' + items[i]);
}
Available since: 1.0
Parameters
- path : String
The path of the directory to list.
pathwill be canonicalized (seeIon.io.canonicalizePath()) - options : Object (optional)
- directories : Boolean (optional)
Whether or not to include directories in the result set.
Defaults to:
true - files : Boolean (optional)
Whether or not to include files in the result set.
Defaults to:
true
- directories : Boolean (optional)
Checks whether or not the specified path (file or directory) exists.
if (Ion.io.pathExists('/path/to/file')) {
Ion.io.deletePath('/path/to/file');
}
Available since: 1.0
Parameters
- path : String
pathwill be canonicalized (seeIon.io.canonicalizePath())
Returns
- Boolean
Returns whether or not
pathexists.
Reads the entire contents of filePath. If the mode is text, the contents are
read as UTF-8 encoded text. If the mode is binary then the contents are read as binary data.
NOTE: Files in UTF-16BE and UTF-16LE formats will be automatically detected and decoded accordingly.
// Let the user choose the JSON file to open and read.
var result = Ion.io.browseFiles({
path: Ion.io.userHomePath,
type: 'open',
filters: [{
name: 'JSON Files',
pattern: '*.json'
}]
});
if (result.success) {
var read = Ion.io.readFile(result.value);
if (read.success) {
var obj = JSON.parse(read.data);
// Do something with obj...
}
}
Available since: 1.0
Parameters
- filePath : String
The path of the file to read.
- options : Object (optional)
- mode : String (optional)
The mode in which to read the file, either
textorbinary.Defaults to:
"text"
- mode : String (optional)
Returns
- Object
Returns an object with the following structure:
success: Booleandata: String/Number[]
Either a String when mode istext, or a Number[] (one Number per byte) when mode isbinary.
Remove a path from the list of watched directories (see Ion.io.watchedPaths and Ion.io.pathChanged).
Available since: Desktop Packager 1.1
Parameters
- path : String
The path of the directory to stop watching.
Returns
- Boolean
trueif the path was successfully unwatched,falseif not.
Unzip a file.
// Unzip the contents of myfile.zip into myfolder
var success = Ion.io.unzip('myfile.zip', '~/myfolder');
Available since: Desktop Packager 1.1
Parameters
- filePath : String
The path to the zipped file.
filePathwill be canonicalized (seeIon.io.canonicalizePath()) - path : String
The path to unzip the file into.
pathwill be canonicalized (seeIon.io.canonicalizePath())
Returns
- Boolean
true if the operation succeeded, false if it failed.
Add a path to the list of watched directories (see Ion.io.watchedPaths and Ion.io.pathChanged).
Available since: Desktop Packager 1.1
Parameters
- path : String
The path of the directory to watch.
Returns
- Boolean
trueif the path was successfully watched,falseif not.
Writes data to the specified file as UTF-8 encoded text if data is a String, or as binary if data is a Number[].
If the destination directory does not already exist, an attempt to create it will be made.
// Write our temp file that is storing cache
var result = Ion.io.writeFile(Ion.io.tempPath + 'MyApp.cache', JSON.stringify(cache));
if (!result.success) {
alert('Failed to save cache!');
}
Available since: 1.0
Parameters
- filePath : String
The path of the file which to write.
filePathwill be canonicalized (seeIon.io.canonicalizePath()) - data : String/Number[]
Either a text String or a Number[] (one Number per byte) of data to write to
filePath. - options : Object (optional)
- append : Boolean (optional)
trueto append data to the end of the file,falseto truncate the contents of the file. IffilePathdoes not exist, then it will be created.Defaults to:
false
- append : Boolean (optional)
Returns
- Object
Returns an object with the following structure:
success: Boolean
Zip a file.
// Zip the contents of ~/myfolder into mydata.zip
Ion.io.zip('mydata.zip', '~/myfolder');
Available since: Desktop Packager 1.1
Parameters
- filePath : String
The path to where the zip file.
- path : String
The path to the contents to zip into
filePath. - options : Object (optional)
Optional parameters
- append : Boolean (optional)
If true, the contents of
pathwill be appended tofilePath. - root : String (optional)
Path to use as root. The root will be prepended to all of the paths in the zip file so that when the file is unzipped, all of the contents will be unzipped into a folder called
root.
- append : Boolean (optional)
Returns
- Boolean
true if the operation succeeded, false if it failed.
Events
Fired when a watched directory path has changed (see Ion.io.watchPath).
Available since: Desktop Packager 1.1
Parameters
- path : String
The path that has changed.
- file : String
The relative file path within
paththat caused the event to fire. - changeType : Number
The type of change that occurred. The values and their meanings are as follows:
1: File Added2: File Removed3: File Modified4: File Moved, Original Name5: File Moved, New Name