Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FileManager

FileManager objects can be used to read from or write to files in either the local Drafts app Documents directory, or iCloud Drive (inside the Drafts folder).Note that local files are not visible on iOS, and are only available for reading and writing via scripting.

Example

// create a local file in App documents
let fmLocal = FileManager.createLocal(); // Local file in app container
let success = fmLocal.writeString("/ScriptedFile.txt", "This is the file  * content");

// read from file in iCloud
let fmCloud = FileManager.createCloud(); // iCloud
let content = fmCloud.readString("/Test Folder/Test.txt")

// create a directory, and move a file to it
fmCloud.createDirectory("My Folder", "/");
fmCloud.moveItem("/TestFile.txt", "/My Folder/TestFile.txt", false);

Hierarchy

  • FileManager

Index

Constructors

constructor

  • Create new instance.

    Parameters

    • isLocal: boolean

      If true, the FileManager will be using the to the local Drafts app documents directory as its root directory, as it appears in the "On my …" area in the Files.app. If false, it will use the Drafts5 iCloud folder as its root directory.

    Returns FileManager

Properties

Readonly basePath

basePath: string

The base POSIX-style path to the directory used by this FileManager.

Readonly baseURL

baseURL: string

The base local URL (file:/// format) to the directory used by this FileManager.

lastError

lastError: string | undefined

If a function fails, this property will contain the last error as a string message, otherwise it will be undefined.

Attribute Methods

getCreationDate

  • getCreationDate(path: string): Date
  • Get creation date of file at path.

    Parameters

    • path: string

    Returns Date

getModificationDate

  • getModificationDate(path: string): Date
  • Get modification date of file at path.

    Parameters

    • path: string

    Returns Date

getTags

  • getTags(path: string): string[]
  • Get tags on file at path.

    Parameters

    • path: string

    Returns string[]

setCreationDate

  • setCreationDate(path: string, date: Date): boolean
  • Set creation date of file at path. Returns true if successful, false if not.

    Parameters

    • path: string
    • date: Date

    Returns boolean

setModificationDate

  • setModificationDate(path: string, date: Date): boolean
  • Set modification date of file at path. Returns true if successful, false if not.

    Parameters

    • path: string
    • date: Date

    Returns boolean

setTags

  • setTags(path: string, tags: string[]): boolean
  • Set tags on the file at path.

    Parameters

    • path: string
    • tags: string[]

    Returns boolean

Other Methods

copyItem

  • copyItem(fromPath: string, toPath: string, overwrite?: boolean): boolean
  • Copy file or directory at fromPath to the toPath. From and to path should be complete paths with file names included.

    Parameters

    • fromPath: string
    • toPath: string
    • Optional overwrite: boolean

      If true, replace existing files in at the toPath. If false, abort operation if file exists at destination.

    Returns boolean

createDirectory

  • createDirectory(name: string, path: string): boolean
  • Create a directory with the specified name in the specified path. Returns true if directory successfully created.

    Parameters

    • name: string
    • path: string

    Returns boolean

listContents

  • listContents(path: string): string[]
  • List files and directories at the specified path. Array of full path will be returned.

    Parameters

    • path: string

    Returns string[]

moveItem

  • moveItem(fromPath: string, toPath: string, overwrite?: boolean): boolean
  • Move file or directory at fromPath to the toPath. From and to path should be complete paths with file names included.

    Parameters

    • fromPath: string
    • toPath: string
    • Optional overwrite: boolean

      If true, replace existing files in at the toPath. If false, abort operation if file exists at destination.

    Returns boolean

readJSON

  • readJSON(path: string): object | undefined
  • Reads the contents of a JSON formatted file at the path. Returns undefined value if the file does not exist or could not be read and parsed as JSON. Contents could be an object, array of objects, etc., depending on the content of the JSON file.

    Parameters

    • path: string

      should begin with a / and be relative to the root directory of the FileManager.

    Returns object | undefined

readString

  • readString(path: string): string | undefined
  • Reads the contents of the file at the path. Returns undefined value if the file does not exist or could not be read.

    Parameters

    • path: string

      should begin with a / and be relative to the root directory of the FileManager.

    Returns string | undefined

writeJSON

  • writeJSON(path: string, content: object): boolean
  • Write the contents to the path in JSON format. Returns true if successful, false if the file could not be written successfully. This will override existing files!

    Parameters

    • path: string
    • content: object

    Returns boolean

writeString

  • writeString(path: string, content: string): boolean
  • Write the contents of the file at the path. Returns true if successful, false if the file could not be written successfully. This will override existing files!

    Parameters

    • path: string
    • content: string

    Returns boolean

Static create

  • Creates a new FileManager object.

    Parameters

    • isLocal: boolean

      If true, the FileManager will be using the to the local Drafts app documents directory as its root directory, as it appears in the "On my …" area in the Files.app. If false, it will use the Drafts5 iCloud folder as its root directory.

    Returns FileManager

Static createCloud

Static createLocal

  • Convenience method to create local file manager. Note that local files are not visible on iOS in the Files app and are only available through the use of scripting.

    Returns FileManager

Generated using TypeDoc