The FileManager class in the Scripting app provides an interface to interact with the iOS filesystem. It includes methods for managing files and directories, checking iCloud status, reading/writing file contents, and working with compressed files. Below is a detailed guide on how to use each feature.
The FileManager class uses several type definitions:
'utf-8', 'ascii', and 'latin1'.To check for iCloud features and manage files stored in iCloud, use the following:
isiCloudEnabled: booleanIndicates if iCloud features are enabled. Ensure iCloud is authorized before accessing iCloud-specific paths.
iCloudDocumentsDirectory: stringPath to the iCloud Documents directory. Requires iCloud to be enabled.
isFileStoredIniCloud(filePath: string): booleanChecks if a file is set to be stored in iCloud.
isiCloudFileDownloaded(filePath: string): booleanChecks if an iCloud file has been downloaded locally.
downloadFileFromiCloud(filePath: string): Promise<boolean>Downloads an iCloud file.
getShareUrlOfiCloudFile(path: string, expiration?: DurationInMilliseconds): stringGenerates a shareable URL for an iCloud file, allowing users to download the file. You need to use try-catch to handle the situation where this method call fails.
iCloudDocumentsDirectory.path: The iCloud file path, prefixed with FileManager.iCloudDocumentsDirectory.expiration: Optional. Set the expiration timestamp in milliseconds.appGroupDocumentsDirectory: stringPath to the App Group Documents directory. Files here are accessible within Widgets but not via the Files app.
documentsDirectory: stringPath to the general Documents directory, accessible via the Files app.
temporaryDirectory: stringPath to the temporary directory for storing transient data.
scriptsDirectory: stringDirectory where scripts are stored.
createDirectory(path: string, recursive?: boolean): Promise<void>Creates a directory at the specified path. If recursive is true, creates parent directories if they don’t exist.
createLink(path: string, target: string): Promise<void>Creates a symbolic link from path to target.
readAsString(path: string, encoding?: Encoding): Promise<string>Reads file contents as a string with optional encoding.
readAsBytes(path: string): Promise<Uint8Array>Reads file contents as a Uint8Array.
readAsData(path: string): Promise<Data>Reads file contents as a Data object.
writeAsString(path: string, contents: string, encoding?: Encoding): Promise<void>Writes a string to a file with optional encoding.
writeAsBytes(path: string, data: Uint8Array): Promise<void>Writes a Uint8Array to a file.
writeAsData(path: string, data: Data): Promise<void>Writes Data to a file.
appendText(path: string, text: string, encoding?: Encoding): Promise<void>Appends the given text to the end of a file at the specified path. If the file or its parent directories do not exist, they will be created automatically.
path: The file path where the text should be appended.text: The string content to append.encoding: Optional. The encoding to use (default is 'utf-8').appendTextSync(path: string, text: string, encoding?: Encoding): voidSynchronous version of appendText. Useful for short, immediate operations where blocking is acceptable.
appendData(path: string, data: Data): Promise<void>Appends binary Data to a file. If the file or its parent directories do not exist, they will be created.
path: The file path to append the data to.data: A Data object containing the binary content to append.appendDataSync(path: string, data: Data): voidSynchronous version of appendData.
stat(path: string): Promise<FileStat>Gets metadata for a file or directory.
mimeType(path: string): stringGets MIME type for a file.
destinationOfSymbolicLink(path: string): string | nullGets destination for a symbolic link.
rename(path: string, newPath: string): Promise<void>Moves or renames a file or directory.
remove(path: string): Promise<void>Deletes a file or directory (including contents if a directory).
zip(srcPath: string, destPath: string, shouldKeepParent?: boolean): Promise<void>Zips files or directories to a specified destination. shouldKeepParent determines if the top-level directory is preserved.
unzip(srcPath: string, destPath: string): Promise<void>Unzips contents from a zip file.
getAllFileBookmarks(): Array<{name: string; path: string}>Get all file bookmarks. File bookmarks are used to bookmark a file or a folder and read or write to it late in your script. They can be created from File Bookmarks tool, they also were automatic created by Intent for Shortcuts app or Share Sheet.
bookmarkExists(name: string): boolReturns a boolean value indicates that whether the bookmark of specified name is exists.
bookmarkedPath(name: string): string | nullTry to get the path of a bookmarked file or folder by a given name, if the bookmark of the name is not exists, returns null.