API Docs

Watches a filesystem path for changes and invokes a callback when they occur.

Created via hs.fs.createPathWatcher(path). Set a callback with setCallback(), then call start() to begin receiving events.

The callback receives (paths, flags):

  • paths: array of changed file/directory paths
  • flags: parallel array of string arrays, one per path, describing what changed

Possible flag values in each per-path array:

Flag Meaning
"itemCreated" Item was created
"itemRemoved" Item was removed
"itemRenamed" Item was renamed or moved
"itemModified" File data was modified
"itemInodeMetaMod" Inode metadata changed (permissions, timestamps, etc.)
"itemFinderInfoMod" Finder info changed
"itemChangeOwner" Ownership or group changed
"itemXattrMod" Extended attributes changed
"itemIsFile" The item is a file
"itemIsDir" The item is a directory
"itemIsSymlink" The item is a symbolic link
"itemIsHardlink" The item is a hard link
"itemIsLastHardlink" This is the last hard link to the inode
"itemCloned" Item was cloned
"ownEvent" Event was generated by this process
"mustScanSubDirs" Subtree must be rescanned (events may have been dropped)
"userDropped" Events were dropped at the user-space level
"kernelDropped" Events were dropped at the kernel level
"rootChanged" The watched root path itself changed
"mount" A volume was mounted under the watched path
"unmount" A volume was unmounted from under the watched path

"ownEvent" marks changes made by the Hammerspoon 2 process itself (e.g. via hs.fs or hs.spoons.install()), so a watcher can tell them apart from changes made by other apps. Changes made by child processes (such as a command run with hs.task) are not marked, because they come from a different process. FSEvents also combines changes to the same path that happen within about a second of each other, so if Hammerspoon and another process both change a path at around the same time, the combined event may or may not include "ownEvent". Treat it as a hint, not as proof of who changed a file.

Example:

const w = hs.fs.createPathWatcher("/Users/me/Documents")
w.setCallback((paths, flags) => {
    paths.forEach((p, i) => console.log(flags[i].join(",") + ": " + p))
}).start()

Properties

identifier

string

The unique identifier assigned to this watcher.

Methods

start() -> HSPathWatcher

Starts monitoring the watched path for filesystem changes.

start() -> HSPathWatcher
self, for chaining
A started watcher, and its callback, stay alive until it's stopped, even if you don't keep a reference to it
const w = hs.fs.createPathWatcher("/tmp")
w.setCallback((paths, flags) => console.log(paths.join(", "))).start()

stop() -> HSPathWatcher

Stops monitoring the watched path.

stop() -> HSPathWatcher
self, for chaining
w.stop()

setCallback(fn) -> HSPathWatcher

Sets the callback invoked when filesystem changes are detected.

setCallback(fn) -> HSPathWatcher
Name Type Description
fn (paths: string[], flags: string[][]) => void Called with an array of changed paths and a parallel array of per-path flag string arrays.
self, for chaining
w.setCallback((paths, flags) => {
    paths.forEach((p, i) => console.log(p + ": " + flags[i].join(", ")))
})

destroy() -> None

Stops the watcher and releases all resources. Called automatically during shutdown.

destroy() -> None
None
w.destroy()