hammerspoon2-docs
    Preparing search index...

    Function withFile

    • Open a file, pass it to a function, and close it again afterwards. The file is closed when the function returns, even if it throws (in which case the exception propagates to you). The function must do all its work synchronously: an async function would find the file already closed after its first await. hs.fs.withFile(path, f => ...).

      Parameters

      • path: string

        Path to the file. ~ is expanded.

      • Optionalmode: string | ((file: HSFile) => any)

        The open mode, as for hs.fs.open(). Defaults to "r"; if you omit it, pass the callback in its place.

      • Optionalcallback: (file: HSFile) => any

        Called with the open file. Required unless it was passed in place of mode.

      Returns any

      Whatever callback returns, or null if the file could not be opened or no callback was given (see hs.fs.lastError).