#hs.fs

Access/inspect the filesystem

This module is partial superset of LuaFileSystem 1.8.0 (http://keplerproject.github.io/luafilesystem/). It has been modified to remove functions which do not apply to macOS filesystems and additional functions providing macOS specific filesystem information have been added.


#Submodules


#API Overview

Functions - API calls offered directly by the extension


#API Documentation

#Functions

Signaturehs.fs.attributes(filepath [, aName]) -> table or string or nil,error
TypeFunction
DescriptionGets the attributes of a file
Parameters
  • filepath - A string containing the path of a file to inspect
  • aName - An optional attribute name. If this value is specified, only the attribute requested, is returned
Returns
  • A table with the file attributes corresponding to filepath (or nil followed by an error message in case of error). If the second optional argument is given, then a string is returned with the value of the named attribute. attribute mode is a string, all the others are numbers, and the time related attributes use the same time reference of os.time:
  • dev - A number containing the device the file resides on
  • ino - A number containing the inode of the file
  • mode - A string containing the type of the file (possible values are: file, directory, link, socket, named pipe, char device, block device or other)
  • nlink - A number containing a count of hard links to the file
  • uid - A number containing the user-id of owner
  • gid - A number containing the group-id of owner
  • rdev - A number containing the type of device, for files that are char/block devices
  • access - A number containing the time of last access modification (as seconds since the UNIX epoch)
  • change - A number containing the time of last file status change (as seconds since the UNIX epoch)
  • modification - A number containing the time of the last file contents change (as seconds since the UNIX epoch)
  • permissions - A 9 character string specifying the user access permissions for the file. The first three characters represent Read/Write/Execute permissions for the file owner. The first character will be "r" if the user has read permissions, "-" if they do not; the second will be "w" if they have write permissions, "-" if they do not; the third will be "x" if they have execute permissions, "-" if they do not. The second group of three characters follow the same convention, but refer to whether or not the file's group have Read/Write/Execute permissions, and the final three characters follow the same convention, but apply to other system users not covered by the Owner or Group fields.
  • creation - A number containing the time the file was created (as seconds since the UNIX epoch)
  • size - A number containing the file size, in bytes
  • blocks - A number containing the number of blocks allocated for file
  • blksize - A number containing the optimal file system I/O blocksize
Notes
  • This function uses stat() internally thus if the given filepath is a symbolic link, it is followed (if it points to another link the chain is followed recursively) and the information is about the file it refers to. To obtain information about the link itself, see function hs.fs.symlinkAttributes()
ExamplesNone
Sourceextensions/fs/libfs.m line 742

Signaturehs.fs.chdir(path) -> true or (nil,error)
TypeFunction
DescriptionChanges the current working directory to the given path.
Parameters
  • path - A string containing the path to change working directory to
Returns
  • If successful, returns true, otherwise returns nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 127

Signaturehs.fs.currentDir() -> string or (nil,error)
TypeFunction
DescriptionGets the current working directory
Parameters
  • None
Returns
  • A string containing the current working directory, or if an error occurred, nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 155

Signaturehs.fs.dir(path) -> iter_fn, dir_obj, nil, dir_obj
TypeFunction
DescriptionCreates an iterator for walking a filesystem path
Parameters
  • path - A string containing a directory to iterate
Returns
  • An iterator function
  • A data object to pass to the iterator function or an error message as a string
  • nil as the initial argument for the iterator (unused and unnecessary in this case, but conforms to Lua spec for iterators). Ignore this value if you are not using this function with for (see Notes).
  • A second data object used by for to close the directory object immediately when the loop terminates. Ignore this value if you are not using this function with for (see Notes).
Notes
  • Unlike most functions in this module, hs.fs.dir will throw a Lua error if the supplied path cannot be iterated.
  • The simplest way to use this function is with a for loop. When used in this manner, the for loop itself will take care of closing the directory stream for us, even if we break out of the loop early.
  • </li><li> for file in hs.fs.dir("/Users/Guest/Documents") do</li><li> print(file)</li><li> end</li><li>
  • It is also possible to use the dir_obj directly if you wish:
  • </li><li> local iterFn, dirObj = hs.fs.dir("/Users/Guest/Documents")</li><li> local file = dirObj:next() -- get the first file in the directory</li><li> while (file) do</li><li> print(file)</li><li> file = dirObj:next() -- get the next file in the directory</li><li> end</li><li> dirObj:close() -- necessary to make sure that the directory stream is closed</li><li>
ExamplesNone
Sourceextensions/fs/libfs.m line 473

Signaturehs.fs.displayName(filepath) -> string
TypeFunction
DescriptionReturns the display name of the file or directory at a specified path.
Parameters
  • filepath - The path to the file or directory
Returns
  • a string containing the display name of the file or directory at a specified path; returns nil if no file with the specified path exists.
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 1026

Signaturehs.fs.fileUTI(path) -> string or nil
TypeFunction
DescriptionReturns the Uniform Type Identifier for the file location specified.
Parameters
  • path - the path to the file to return the UTI for.
Returns
  • a string containing the Uniform Type Identifier for the file location specified or nil if an error occurred
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 938

| | | | --------------------------------------------|-------------------------------------------------------------------------------------| | Signature | hs.fs.fileUTIalternate(fileUTI, type) -> string | | Type | Function | | Description | Returns the fileUTI's equivalent form in an alternate type specification format. | | Parameters |

  • a string containing a file UTI, such as one returned by hs.fs.fileUTI.
  • a string specifying the alternate format for the UTI. This string may be one of the following:extension - as a file extension, commonly used for platform independent file sharing when file metadata can't be guaranteed to be cross-platform compatible. Generally considered unreliable when other file type identification methods are available. mime - as a mime-type, commonly used by Internet applications like web browsers and email applications. pasteboard - as an NSPasteboard type (see hs.pasteboard). ostype - four character file type, most common pre OS X, but still used in some legacy APIs.
| | Returns |
  • the file UTI in the alternate format or nil if the UTI does not have an alternate of the specified type.
| | Notes | None | | Examples | None | | Source | extensions/fs/libfs.m line 962 |


Signaturehs.fs.getFinderComments(path) -> string
TypeFunction
DescriptionGet the Finder comments for the file or directory at the specified path
Parameters
  • path - the path to the file or directory you wish to get the comments of
Returns
  • a string containing the Finder comments for the file or directory specified. If no comments have been set for the file, returns an empty string. If an error occurs, most commonly an invalid path, this function will throw a Lua error.
Notes
  • This function uses hs.osascript to access the file comments through AppleScript
ExamplesNone
Sourceextensions/fs/fs.lua line 68

Signaturehs.fs.link(old, new[, symlink]) -> true or (nil,error)
TypeFunction
DescriptionCreates a link
Parameters
  • old - A string containing a path to a filesystem object to link from
  • new - A string containing a path to create the link at
  • symlink - An optional boolean, true to create a symlink, false to create a hard link. Defaults to false
Returns
  • True if the link was created, otherwise nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 346

Signaturehs.fs.lock(filehandle, mode[, start[, length]]) -> true or (nil,error)
TypeFunction
DescriptionLocks a file, or part of it
Parameters
  • filehandle - An open file
  • mode - A string containing either "r" for a shared read lock, or "w" for an exclusive write lock
  • start - An optional number containing an offset into the file to start the lock at. Defaults to 0
  • length - An optional number containing the length of the file to lock. Defaults to the full size of the file
Returns
  • True if the lock was obtained successfully, otherwise nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 209

Signaturehs.fs.lockDir(path, [seconds_stale]) -> lock or (nil,error)
TypeFunction
DescriptionLocks a directory
Parameters
  • path - A string containing the path to a directory
  • seconds_stale - An optional number containing an age (in seconds) beyond which to consider an existing lock as stale. Defaults to INT_MAX (which is, broadly speaking, equivalent to "never")
Returns
  • If successful, a lock object, otherwise nil and an error string
Notes
  • This is not a low level OS feature, the lock is actually a file created in the path, called lockfile.lfs, so the directory must be writable for this function to succeed
  • The returned lock object can be freed with lock:free()
  • If the lock already exists and is not stale, the error string returned will be "File exists"
ExamplesNone
Sourceextensions/fs/libfs.m line 241

Signaturehs.fs.mkdir(dirname) -> true or (nil,error)
TypeFunction
DescriptionCreates a new directory
Parameters
  • dirname - A string containing the path of a directory to create
Returns
  • True if the directory was created, otherwise nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 382

Signaturehs.fs.pathFromBookmark(data) -> string | nil, string
TypeFunction
DescriptionGets the file path from a binary encoded bookmark.
Parameters
  • data - The binary encoded Bookmark.
Returns
  • A string containing the path to the Bookmark URL or nil if an error occurs.
  • An error message if an error occurs.
Notes
  • A bookmark provides a persistent reference to a file-system resource.
  • When you resolve a bookmark, you obtain a URL to the resource’s current location.
  • A bookmark’s association with a file-system resource (typically a file or folder)
  • usually continues to work if the user moves or renames the resource, or if the
  • user relaunches your app or restarts the system.
  • No volumes are mounted during the resolution of the bookmark data.
ExamplesNone
Sourceextensions/fs/libfs.m line 1078

Signaturehs.fs.pathToAbsolute(filepath) -> string
TypeFunction
DescriptionGets the absolute path of a given path
Parameters
  • filepath - Any kind of file or directory path, be it relative or not
Returns
  • A string containing the absolute path of filepath (i.e. one that doesn't include ., .. or symlinks)
  • Note that symlinks will be resolved to their target file
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 999

Signaturehs.fs.pathToBookmark(path) -> string | nil
TypeFunction
DescriptionReturns the path as binary encoded bookmark data.
Parameters
  • path - The path to encode
Returns
  • Bookmark data in a binary encoded string or nil if path is invalid.
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 1047

Signaturehs.fs.rmdir(dirname) -> true or (nil,error)
TypeFunction
DescriptionRemoves an existing directory
Parameters
  • dirname - A string containing the path to a directory to remove
Returns
  • True if the directory was removed, otherwise nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 411

Signaturehs.fs.setFinderComments(path, comment) -> boolean
TypeFunction
DescriptionSet the Finder comments for the file or directory at the specified path to the comment specified
Parameters
  • path - the path to the file or directory you wish to set the comments of
  • comment - a string specifying the comment to set. If this parameter is missing or is an explicit nil, the existing comment is cleared.
Returns
  • true on success; on error, most commonly an invalid path, this function will throw a Lua error.
Notes
  • This function uses hs.osascript to access the file comments through AppleScript
ExamplesNone
Sourceextensions/fs/fs.lua line 95

Signaturehs.fs.symlinkAttributes (filepath [, aname]) -> table or string or nil,error
TypeFunction
DescriptionGets the attributes of a symbolic link
Parameters
  • filepath - A string containing the path of a link to inspect
  • aName - An optional attribute name. If this value is specified, only the attribute requested, is returned
Returns
  • A table or string if the values could be found, otherwise nil and an error string.
Notes
  • The return values for this function are identical to those provided by hs.fs.attributes() with the following addition: the attribute name "target" is added and specifies a string containing the absolute path that the symlink points to.
ExamplesNone
Sourceextensions/fs/fs.lua line 127

Signaturehs.fs.tagsAdd(filepath, tags)
TypeFunction
DescriptionAdds one or more tags to the Finder tags of a file
Parameters
  • filepath - A string containing the path of a file
  • tags - A table containing one or more strings, each containing a tag name
Returns
  • true if the tags were updated; throws a lua error if an error occurs updating the tags
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 857

Signaturehs.fs.tagsGet(filepath) -> table or nil
TypeFunction
DescriptionGets the Finder tags of a file
Parameters
  • filepath - A string containing the path of a file
Returns
  • A table containing the list of the file's tags, or nil if the file has no tags assigned; throws a lua error if an error accessing the file occurs
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 825

Signaturehs.fs.tagsRemove(filepath, tags)
TypeFunction
DescriptionRemoves Finder tags from a file
Parameters
  • filepath - A string containing the path of a file
  • tags - A table containing one or more strings, each containing a tag name
Returns
  • true if the tags were updated; throws a lua error if an error occurs updating the tags
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 901

Signaturehs.fs.tagsSet(filepath, tags)
TypeFunction
DescriptionSets the Finder tags of a file, removing any that are already set
Parameters
  • filepath - A string containing the path of a file
  • tags - A table containing zero or more strings, each containing a tag name
Returns
  • true if the tags were set; throws a lua error if an error occurs setting the new tags
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 880

Signaturehs.fs.temporaryDirectory() -> string
TypeFunction
DescriptionReturns the path of the temporary directory for the current user.
Parameters
  • None
Returns
  • The path to the system designated temporary directory for the current user.
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 924

Signaturehs.fs.touch(filepath [, atime [, mtime]]) -> true or (nil,error)
TypeFunction
DescriptionUpdates the access and modification times of a file
Parameters
  • filepath - A string containing the path of a file to touch
  • atime - An optional number containing the new access time of the file to set (as seconds since the Epoch). Defaults to now
  • mtime - An optional number containing the new modification time of the file to set (as seconds since the Epoch). Defaults to the value of atime
Returns
  • True if the operation was successful, otherwise nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 599

Signaturehs.fs.unlock(filehandle[, start[, length]]) -> true or (nil,error)
TypeFunction
DescriptionUnlocks a file or a part of it.
Parameters
  • filehandle - An open file
  • start - An optional number containing an offset from the start of the file, to unlock. Defaults to 0
  • length - An optional number containing the length of file to unlock. Defaults to the full size of the file
Returns
  • True if the unlock succeeded, otherwise nil and an error string
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 315

Signaturehs.fs.urlFromPath(path) -> string | nil
TypeFunction
DescriptionReturns the encoded URL from a path.
Parameters
  • path - The path
Returns
  • A string or nil if path is invalid.
NotesNone
ExamplesNone
Sourceextensions/fs/libfs.m line 1130