W3cubDocs

/Hammerspoon

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

API Documentation

Functions

attributes
Signature hs.fs.attributes(filepath [, aName]) -> table or string or nil,error
Type Function
Description

Gets 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()
Source extensions/fs/libfs.m line 742
chdir
Signature hs.fs.chdir(path) -> true or (nil,error)
Type Function
Description

Changes 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
Source extensions/fs/libfs.m line 127
currentDir
Signature hs.fs.currentDir() -> string or (nil,error)
Type Function
Description

Gets the current working directory

Parameters
  • None
Returns
  • A string containing the current working directory, or if an error occurred, nil and an error string
Source extensions/fs/libfs.m line 155
dir
Signature hs.fs.dir(path) -> iter_fn, dir_obj, nil, dir_obj
Type Function
Description

Creates 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.

       for file in hs.fs.dir("/Users/Guest/Documents") do
           print(file)
       end
    
  • It is also possible to use the dir_obj directly if you wish:

       local iterFn, dirObj = hs.fs.dir("/Users/Guest/Documents")
       local file = dirObj:next() -- get the first file in the directory
       while (file) do
           print(file)
           file = dirObj:next() -- get the next file in the directory
       end
       dirObj:close() -- necessary to make sure that the directory stream is closed
    
Source extensions/fs/libfs.m line 473
displayName
Signature hs.fs.displayName(filepath) -> string
Type Function
Description

Returns 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.
Source extensions/fs/libfs.m line 1026
fileUTI
Signature hs.fs.fileUTI(path) -> string or nil
Type Function
Description

Returns 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
Source extensions/fs/libfs.m line 938
fileUTIalternate
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.
Source extensions/fs/libfs.m line 962
getFinderComments
Signature hs.fs.getFinderComments(path) -> string
Type Function
Description

Get 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
Source extensions/fs/fs.lua line 68
lock
Signature hs.fs.lock(filehandle, mode[, start[, length]]) -> true or (nil,error)
Type Function
Description

Locks 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
Source extensions/fs/libfs.m line 209
lockDir
Signature hs.fs.lockDir(path, [seconds_stale]) -> lock or (nil,error)
Type Function
Description

Locks 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"
Source extensions/fs/libfs.m line 241
mkdir
Signature hs.fs.mkdir(dirname) -> true or (nil,error)
Type Function
Description

Creates 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
Source extensions/fs/libfs.m line 382
pathFromBookmark
Signature hs.fs.pathFromBookmark(data) -> string | nil, string
Type Function
Description

Gets 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.
Source extensions/fs/libfs.m line 1078
pathToAbsolute
Signature hs.fs.pathToAbsolute(filepath) -> string
Type Function
Description

Gets 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
Source extensions/fs/libfs.m line 999
pathToBookmark
Signature hs.fs.pathToBookmark(path) -> string | nil
Type Function
Description

Returns 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.
Source extensions/fs/libfs.m line 1047
rmdir
Signature hs.fs.rmdir(dirname) -> true or (nil,error)
Type Function
Description

Removes 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
Source extensions/fs/libfs.m line 411
setFinderComments
Signature hs.fs.setFinderComments(path, comment) -> boolean
Type Function
Description

Set 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
Source extensions/fs/fs.lua line 95
symlinkAttributes
Signature hs.fs.symlinkAttributes (filepath [, aname]) -> table or string or nil,error
Type Function
Description

Gets 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.
Source extensions/fs/fs.lua line 127
tagsAdd
Signature hs.fs.tagsAdd(filepath, tags)
Type Function
Description

Adds 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
Source extensions/fs/libfs.m line 857
tagsGet
Signature hs.fs.tagsGet(filepath) -> table or nil
Type Function
Description

Gets 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
Source extensions/fs/libfs.m line 825
tagsRemove
Signature hs.fs.tagsRemove(filepath, tags)
Type Function
Description

Removes 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
Source extensions/fs/libfs.m line 901
tagsSet
Signature hs.fs.tagsSet(filepath, tags)
Type Function
Description

Sets 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
Source extensions/fs/libfs.m line 880
temporaryDirectory
Signature hs.fs.temporaryDirectory() -> string
Type Function
Description

Returns 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.
Source extensions/fs/libfs.m line 924
touch
Signature hs.fs.touch(filepath [, atime [, mtime]]) -> true or (nil,error)
Type Function
Description

Updates 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
Source extensions/fs/libfs.m line 599
unlock
Signature hs.fs.unlock(filehandle[, start[, length]]) -> true or (nil,error)
Type Function
Description

Unlocks 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
Source extensions/fs/libfs.m line 315
urlFromPath
Signature hs.fs.urlFromPath(path) -> string | nil
Type Function
Description

Returns the encoded URL from a path.

Parameters
  • path - The path
Returns
  • A string or nil if path is invalid.
Source extensions/fs/libfs.m line 1130

© 2014–2017 Hammerspoon contributors
Licensed under the MIT License.
https://www.hammerspoon.org/docs/hs.fs.html