File and Folder References
SenseTalk provides several methods for working with files and file systems. You can use the commands and functions described here to access files and file system information, to determine or change your working directory, and other details about paths and directories. You can also access properties for individual files, such as file size and assigned permissions.
SenseTalk commands and functions for working with files and file system objects operate on the local machine rather than on a system under test (SUT).
The examples below show you how to access file and file system information by using SenseTalk. For topics such as creating or deleting files, writing to files, and other ways of altering files or file system objects, see File and Folder Interaction.
As a best practice, any files referenced within a SenseTalk script should be added to Eggplant Functional through the Resources pane in the Suite window. This method stores files to the Resources directory within the suite directory for the given suite. Although SenseTalk can access files stored elsewhere on the local file system, using the Resources directory provides additional capabilities. See the Resources Pane for more information.
Referring to Files in a Script
File
, Folder
Expression
To refer to a file in a script, just use the word file
followed by an expression that evaluates to the name of the file. You can refer to folders in a similar way, using the word folder
instead of file
.
Syntax:
file filePath
folder filePath
directory filePath
The words folder
and directory
are used interchangeably within SenseTalk scripts—wherever the word folder
is used in a script, you can use the word directory
instead.
The filePath can be the text of a path in either Mac/Linux or Windows format, or it can be a list in which each item is one component of the path.
The name given can be either the full (absolute) path name of the file or folder, or the path relative to the current working folder (see the folder
global property). SenseTalk determines the full “absolute” path name of the file based on the name given, according to the following rules:
- if the name begins with a slash (/) or the first item in the path list is "/" it is already an absolute file name
- if the name begins with a drive letter and colon followed by a slash or backslash (like "C:\") or the first item in the path list is a drive letter and colon and the second item is a slash it is already an absolute file name
- if the name begins with a tilde and a slash (~/) it represents a path relative to the user’s home folder
- if the name begins with a tilde (~) followed by a user name it represents a path relative to that specific user’s home folder
- if the name begins with a period and a slash (./) or two periods and a slash (../) it represents a relative path from either the current working folder or the current working folder's parent folder, respectively
- otherwise, it represents a file or path within the current working folder (
the folder
global property)
File paths on Mac and Linux systems and on the web use the slash character /
to separate path components, but Windows systems use the backslash character \
instead. SenseTalk accepts file paths in either format interchangeably (as well as lists of path components), but defaults to using slash /
when representing paths. You may use the FilePath
or WindowsFilePath
functions if you need a platform-specific standard representation.
Example:
open file"/etc/passwd"
move file "runlog24" into folder "archivedLogs"
Folder
, Directory
Function
Behavior: Returns the parent folder of a given file path.
Use the folder
function (or its synonym, directory
) to obtain the path to the folder containing a given file or folder.
The value returned will end with a slash, unless the folderNamesEndWithSlash
global property is set to false. The slash at the end makes it easy to create a full path name by simply appending a file name.
Syntax:
{the} folder of filePath
folder( filePath )
If the filePath given is not an absolute path it will be taken relative to the current working folder, as given by the folder
global property. The folder
function may also be called with a fileDescription object (such as returned by the files()
or fileDescription()
functions) or with a script file object, to obtain the parent folder of the indicated file.
The value returned by the folder function is actually a fileDescription object, but can simply be treated as a string for most purposes. Its string value is the full path to the folder.
Examples:
put the folder of myFile into myFolder
put folder(someFile) & "siblingFileName" into newFile
LastPathComponent
Function
Behavior: Returns the local name of a file system object, removing the path to the parent folder.
Use the lastPathComponent
function when you have the full path name of a file or folder and want to obtain just the local name, without the full path.
Syntax:
{the} lastPathComponent of filePath
lastPathComponent( filePath )
Examples:
put the lastPathComponent of fullPath into fileName
put lastPathComponent("/Users/jc/Documents/Jan24.data") --> Jan24.data
FileExtension
Function
Behavior: Returns the file extension from a file name.
Use the fileExtension
function when you have the full name of a file or folder and want to obtain just the file extension. The extension is the part of the name following the last period. The period is not included in the extension that is returned.
Syntax:
{the} fileExtension of fileName
fileExtension( fileName )
Examples:
put the fileExtension of fileName into extension
put fileExtension("/Users/jc/Documents/Jan24.data") --> data
PathList
Function
Behavior: Returns a file path as a list of individual path components in a standard format.
Use the pathList
function when you have the full or partial path name of a file or folder and want to obtain a path list containing the individual components of that path in a standard format.
Syntax:
{the} pathList of filePath
pathList( filePath )
Examples:
put the pathList of filePath into filePathList
put pathList("/Users/sj/Documents/MLK.txt") --> ["/","Users","sj","Documents","MLK.txt"]
FilePath
, WindowsFilePath
Functions
Behavior: The filePath
function returns a file path as a string in a standard (Mac/Linux/web) format, with slashes /
as the separator between path components. The windowsFilePath
function is similar, but returns a file path string in Windows format, using backslashes \
as the separator between components.
Use the filePath
or windowsFilePath
function when you have a full or partial path name of a file or folder in any format and want to obtain either a standard Mac/Linux/web text representation of that path that uses slashes between path components, or a Windows text representation that uses backslashes between path components.
Syntax:
{the} filePath of filePath
filePath( filePath )
{the} windowsFilePath of filePath
windowsFilePath( filePath )
Examples:
put the filePath of fullPath into stdPath
put filePath("\wiki\en\Home") --> /wiki/en/Home
put windowsFilePath("/Admin/theo/x32.jpg") --> \Admin\theo\x32.jpg
put the filePath of ["a","b","c"] --> a/b/c
put windowsFilePath of ["a","b","c"] --> a\b\c
ResolvedFilePath
Function
Behavior: Returns a file path as a string in a "resolved" standard format, which is the full absolute path to the file, taking into account the current folder if necessary, and resolving components such as ".." and "~".
Use the resolvedFilePath
function when you want to determine the full actual path fo a file or folder. Because of path mechanisms such as "~" which refers to the user's home folder and ".." which refers to the parent folder of any folder it is possible to have multiple different paths which all refer to the same location. The resolvedFilePath can be used to obtain a standard representation of the path which can be used to compare paths to see if they represent the same file, for example.
Syntax:
{the} resolvedFilePath of filePath
resolvedFilePath( filePath )
Examples:
put the resolvedFilePath of fileName into resolvedName
if resolvedFilePath(it) is resolvedFilePath(saveFile) then
//Do things
end if
FileDescription
Function
Behavior: Returns a fileDescription object containing information about a given file.
Use the fileDescription()
function to obtain a packet of information about a file or folder. The value returned is a fileDescription object. A fileDescription is a SenseTalk object (property list) that appears as the short name of the file if displayed as text, but knows its full path and also contains many pieces of additional information about the file.
Syntax:
{the} fileDescription of filePath
fileDescription( filePath )
The filePath may be the full path to a file, or the file name relative to the current working folder. The value returned is a fileDescription object.
FileDescription Object
A fileDescription object is a property list with its
objectType
set to “fileDescription”. A fileDescription object contains anasText
property that is set to the local name of the file, so displaying the object will simply show the file’s name.Each fileDescription object also holds a number of additional items of information. In particular, the
long name
property contains the full path name of the file. Other properties include the parent folder where the file is located, and such information as the file size, owner, and permissions. Use thekeys()
orarchive()
function (or delete the object’s asText property before displaying it) to find out exactly what information is available.SenseTalk commands and functions that work with files, such as the
copy file
andrename
commands and thediskSpace()
function, recognize fileDescription objects that are used in place of file names, and will use the long name to identify the actual file. In this way, fileDescription objects can serve as file identifiers that can be stored in variables, passed as parameters, and so forth.
FileDescription objects can also be obtained from the files()
and folders()
functions, which each return a list of fileDescriptions.
Examples:
put fileDescription("/tmp/data") into fileInfo
put fileInfo is a fileDescription --> True
put fileInfo --> "data" (the fileDescription displays itself as the local name)
put the long name of fileInfo --> "/tmp/data" (the object contains the full path name)
put fileInfo's NSFileSize into dataSize