Drafts Script Reference
    Preparing search index...

    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.

    // 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);

    // create file manager using a Bookmark
    let bookmark = Bookmark.findOrCreate("My-Folder");
    let fm = FileManager.createForBookmark(bookmark);
    Index

    Attribute

    • Get creation date of file at path.

      Parameters

      • path: string

      Returns Date

    • Get modification date of file at path.

      Parameters

      • path: string

      Returns Date

    • Get tags on file at path.

      Parameters

      • path: string

      Returns string[]

    • Set creation date of file at path. Returns true if successful, false if not.

      Parameters

      • path: string
      • date: Date

      Returns boolean

    • Set modification date of file at path. Returns true if successful, false if not.

      Parameters

      • path: string
      • date: Date

      Returns boolean

    • Set tags on the file at path.

      Parameters

      • path: string
      • tags: string[]

      Returns boolean

    Constructors

    • 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

    • Convenience method to create iCloud file manager. iCloud file managers work with files in the iCloud Drive/Drafts folder

      Returns FileManager

    • 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

    Other

    basePath: string

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

    baseURL: string

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

    lastError: string

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

    • 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
      • Optionaloverwrite: boolean

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

      Returns 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

    • Check if a file already exists at the given path.

      Parameters

      • path: string

      Returns boolean

    • List files and directories at the specified path. Array of full path will be returned.

      Parameters

      • path: string

      Returns string[]

    • 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
      • Optionaloverwrite: boolean

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

      Returns boolean

    • 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

    • 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

    • 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

    • 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