FileManager
Read and write files on disk.
A FileManager lets you read files stored on the disk and make changes to them. Paths to files are supplied as strings.
Methods
Section titled “Methods”
addTag(filePath, tag)
Section titled “addTag(filePath, tag)”Adds a tag to a file.
addTag(filePath: string, tag: string)A tag can only be added to a file once. It is not possible to specify a color for the tag. You can create the tags using the Files app to specify the color and then add them to files afterwards using the FileManager API.
The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of file to add the tag to.
tag string
Tag to add. This can be an existing tag or a new tag.
allExtendedAttributes(filePath)
Section titled “allExtendedAttributes(filePath)”Reads all extended attributes on a file.
allExtendedAttributes(filePath: string): [string]Extended attributes are metadata that can be stored on a file. Note that extended attributes are not synced with iCloud.
Parameters
Section titled “Parameters”filePath string
Path of file to read extended attributes from.
Return value
Section titled “Return value”[string]
An array of all extended attributes.
allFileBookmarks()
Section titled “allFileBookmarks()”Reads all file bookmarks created in settings.
allFileBookmarks(): [{string: string}]File bookmarks are used to bookmark a file or a folder and read or write to it later. File bookmarks are created from Scriptables settings.
This function returns all file bookmarks as an array of objects that take the following form.
{ "name": "My Bookmark", "source": "host"}The source can either be host for file bookmarks that can be used in the app or siri_shortcuts for file bookmarks that can be used in Siri and Shortcuts.
Return value
Section titled “Return value”[{string: string}]
allTags(filePath)
Section titled “allTags(filePath)”Reads all tags from a file.
allTags(filePath: string): [string]The tags are read from the file at the specified path. Tags can either be read, added and removed using the Files app by using the APIs provided by a FileManager.
The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of file to read tags from.
Return value
Section titled “Return value”[string]
Read tags.
bookmarkedPath(name)
Section titled “bookmarkedPath(name)”Get path to a bookmarked file or folder.
bookmarkedPath(name: string): stringGets the path to a bookmarked file or filder. Use file bookmarks to access files and folders outside Scriptables documents directory.
You can edit your file bookmarks from Scriptables settings.
The function will throw an error if the bookmark doesn’t exist.
Please beware that bookmarks created from Scriptables settings only can be used when running a script in the app and not from the Share Sheet, Siri and Shortcuts. If you wish to use a bookmark from Siri or the Shortcuts app, the bookmark must be created using Scriptables “Create File Bookmark” shortcut action using the Shortcuts app.
Parameters
Section titled “Parameters”name string
Name of bookmark to create path for.
Return value
Section titled “Return value”string
Path to the bookmarked file or folder.
bookmarkExists(name)
Section titled “bookmarkExists(name)”Check if a bookmark exists.
bookmarkExists(name: string): boolChecks if a file bookmark exists with the specified name.
You can edit your file bookmarks from Scriptables settings.
Please beware that bookmarks created from Scriptables settings only can be used when running a script in the app and not from the Share Sheet, Siri and Shortcuts. If you wish to use a bookmark from Siri or the Shortcuts app, the bookmark must be created using Scriptables “Create File Bookmark” shortcut action using the Shortcuts app.
Parameters
Section titled “Parameters”name string
Name of bookmark.
Return value
Section titled “Return value”bool
True of a bookmark exists for the specified name, otherwise false.
cacheDirectory()
Section titled “cacheDirectory()”Path of cache directory.
cacheDirectory(): stringUsed to retrieve the path of a cache directory on disk. The operating system may at any time delete files stored in this directory and therefore you should not rely on it for long time storage.
Data persisted in the cache directory will generally live longer than data persisted in a temporary directory.
If you need long time storage, see documentsDirectory() or libraryDirectory(). This directory is not shared between the app, the action extension and Siri.
Return value
Section titled “Return value”string
Path to temporary directory.
copy(sourceFilePath, destinationFilePath)
Section titled “copy(sourceFilePath, destinationFilePath)”Copies a file.
copy(sourceFilePath: string, destinationFilePath: string)Copies the file from the source path to the destination path. If a file already exists at the destination file path, the operation will fail and the file will not be copied.
Parameters
Section titled “Parameters”sourceFilePath string
Path of the file to copy.
destinationFilePath string
Path to copy the file to.
createDirectory(path, intermediateDirectories)
Section titled “createDirectory(path, intermediateDirectories)”Creates a directory at the specified path.
createDirectory(path: string, intermediateDirectories: bool)You can optionally create all intermediate directories.
Parameters
Section titled “Parameters”path string
Path of directory to create.
intermediateDirectories bool
Whether to create all intermediate directories. Defaults to false.
creationDate(filePath)
Section titled “creationDate(filePath)”Reads the creation date of a file.
creationDate(filePath: string): DateThe returned value will be null if the creation date cannot be read.
Parameters
Section titled “Parameters”filePath string
Path of file.
Return value
Section titled “Return value”Date
The date the file was created.
documentsDirectory()
Section titled “documentsDirectory()”Path of documents directory.
documentsDirectory(): stringUsed to retrieve the path to the documents directory. Your scripts are stored in this directory. If you have iCloud enabled, your scripts will be stored in the documents directory in iCloud otherwise they will be stored in the local documents directory. The directory can be used for long time storage. Documents stored in this directory can be accessed using the Files app. Files stored in the local documents directory will not appear in the Files app.
Return value
Section titled “Return value”string
Path to documents directory.
downloadFileFromiCloud(filePath)
Section titled “downloadFileFromiCloud(filePath)”Download file from iCloud if necessary.
downloadFileFromiCloud(filePath: string): PromiseDownloads the file from iCloud if it have not already been downloaded. If you pass in a path to a file that is not stored in iCloud, the returned promise will be resolved immediately making it safe to pass in any file path.
Parameters
Section titled “Parameters”filePath string
Path of file to download from iCloud.
Return value
Section titled “Return value”Promise
Promise that is fulfilled when the file have been downloaded.
fileExists(filePath)
Section titled “fileExists(filePath)”Checks if the file exists.
fileExists(filePath: string): boolChecks if the file exists at the specified file path. Checking this before moving or copying to a destination can be a good idea as those operations will replace any existing file at the destination file path.
Parameters
Section titled “Parameters”filePath string
File path to examine.
Return value
Section titled “Return value”bool
True if the file exists otherwise false.
fileExtension(filePath)
Section titled “fileExtension(filePath)”Get extension of a file.
fileExtension(filePath: string): stringTakes a file path and returns the extension of the file, e.g. “.jpg” or “.js”. Returns en empty string for directories.
Parameters
Section titled “Parameters”filePath string
Path of file to get extension from.
Return value
Section titled “Return value”string
Extension of the file.
fileName(filePath, includeFileExtension)
Section titled “fileName(filePath, includeFileExtension)”Get name of a file.
fileName(filePath: string, includeFileExtension: bool): stringTakes a file path and returns the name of the file. Also supports getting the name of a directory. The returned file name optionally includes the extension of the file.
Parameters
Section titled “Parameters”filePath string
path of file to get name of.
includeFileExtension bool
Whether or not the file extension should be included. Defaults to false.
Return value
Section titled “Return value”string
Name of the file.
fileSize(filePath)
Section titled “fileSize(filePath)”Size of the file in kilobytes.
fileSize(filePath: string): numberThe returned value will be null if the file size cannot be read.
Parameters
Section titled “Parameters”filePath string
Path of file.
Return value
Section titled “Return value”number
The file size measured in kilobytes.
getUTI(filePath)
Section titled “getUTI(filePath)”Gets the UTI of the specified file.
getUTI(filePath: string): stringThe Uniform Type Identifier is a string that identifies the type of file.
Parameters
Section titled “Parameters”filePath string
Path of file to get UTI of.
Return value
Section titled “Return value”string
The UTI of the file.
iCloud()
Section titled “iCloud()”Creates an iCloud FileManager.
static iCloud(): FileManagerCreates a file manager for operating with files stored in iCloud. iCloud must be enabled on the device in order to use this.
Return value
Section titled “Return value”FileManager
iCloud FileManager.
isDirectory(path)
Section titled “isDirectory(path)”Checks if a path points to a directory.
isDirectory(path: string): boolParameters
Section titled “Parameters”path string
Path to examine.
Return value
Section titled “Return value”bool
True if the path points to a directory otherwise false.
isFileDownloaded(filePath)
Section titled “isFileDownloaded(filePath)”Checks if a file have been downloaded.
isFileDownloaded(filePath: string): boolIf a file is stored in iCloud and it has not been downloaded, this function returns false. In that case, the file can be downloaded using downloadFileFromiCloud(filePath. If the file is not stored in iCloud but rather locally on the device, this function returns true.
The function returns false if the file does not exist. Check if a file exists using fileExists(filePath)
Parameters
Section titled “Parameters”filePath string
Path of file.
Return value
Section titled “Return value”bool
True if the file have been downloaded otherwise false.
isFileStoredIniCloud(filePath)
Section titled “isFileStoredIniCloud(filePath)”Checks if a file is stored in iCloud.
isFileStoredIniCloud(filePath: string): boolChecks if a file is stored in iCloud or locally on the device.
The function returns false if the file does not exist. Check if a file exists using fileExists(filePath)
Parameters
Section titled “Parameters”filePath string
Path of file.
Return value
Section titled “Return value”bool
True if the file is stored in iCloud otherwise false.
joinPath(lhsPath, rhsPath)
Section titled “joinPath(lhsPath, rhsPath)”Joins two path components.
joinPath(lhsPath: string, rhsPath: string): stringJoins two paths to created one path. For example to join the path to a directory with the name of a file. This is the suggested approach for creating new file paths passed to the read and write functions of a FileManager.
Parameters
Section titled “Parameters”lhsPath string
Left-hand side part of the new path.
rhsPath string
Right-hand side part of the new path.
Return value
Section titled “Return value”string
Path with the two path components joined.
libraryDirectory()
Section titled “libraryDirectory()”Path of library directory.
libraryDirectory(): stringUsed to retrieve the path to the library directory. The directory can be used for long time storage. Documents stored in this directory cannot be accessed using the Files app.
Return value
Section titled “Return value”string
Path to library directory.
listContents(directoryPath)
Section titled “listContents(directoryPath)”Lists content of directory.
listContents(directoryPath: string): [string]Lists all the contents in the specified directory. The returned array contains filenames to all files and directories in the specified directory.
Parameters
Section titled “Parameters”directoryPath string
Path to directory.
Return value
Section titled “Return value”[string]
Names of all the files and directories in the specified directory.
local()
Section titled “local()”Creates a local FileManager.
static local(): FileManagerCreates a file manager for operating with files stored locally.
Return value
Section titled “Return value”FileManager
Local FileManager.
modificationDate(filePath)
Section titled “modificationDate(filePath)”Reads the modification date of a file.
modificationDate(filePath: string): DateThe returned value will be null if the modification date cannot be read.
Parameters
Section titled “Parameters”filePath string
Path of file.
Return value
Section titled “Return value”Date
The date the file was last modified.
move(sourceFilePath, destinationFilePath)
Section titled “move(sourceFilePath, destinationFilePath)”Moves a file.
move(sourceFilePath: string, destinationFilePath: string)Moves the file from the source path to the destination path. Caution: This operation will replace any existing file at the the destination.
Parameters
Section titled “Parameters”sourceFilePath string
Path of the file to move.
destinationFilePath string
Path to move the file to.
read(filePath)
Section titled “read(filePath)”Read contents of a file as data.
read(filePath: string): DataReads the contents of the file specified by the file path as raw data. To read the file as a string see readString(filePath) and to read it as an image see readImage(filePath).
The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of the file to read.
Return value
Section titled “Return value”Data
Contents of the file as a data or null if the file could not be read.
readExtendedAttribute(filePath, name)
Section titled “readExtendedAttribute(filePath, name)”Reads an extended attribute from a file.
readExtendedAttribute(filePath: string, name: string): stringExtended attributes are metadata that can be stored on a file. Note that extended attributes are not synced with iCloud.
The function will return null if the attribute does not exist.
The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of file to read extended attribute from.
name string
Name of the extended attribute to read.
Return value
Section titled “Return value”string
Value of the extended attribute.
readImage(filePath)
Section titled “readImage(filePath)”Read contents of a file as an image.
readImage(filePath: string): ImageReads the contents of the file specified by the file path and converts it to an image.
The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of the file to read.
Return value
Section titled “Return value”Image
Contents of the file as an image or null if the file could not be read.
readString(filePath)
Section titled “readString(filePath)”Read contents of a file as string.
readString(filePath: string): stringThe function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of the file to read.
Return value
Section titled “Return value”string
Contents of the file as a string or null if the file could not be read.
remove(filePath)
Section titled “remove(filePath)”Removes a file.
remove(filePath: string)Removes the file at the specified path. Use with caution. Removed files cannot be restored.
Parameters
Section titled “Parameters”filePath string
Path of file to remove.
removeExtendedAttribute(filePath, name)
Section titled “removeExtendedAttribute(filePath, name)”Removes an extended attribute from a file.
removeExtendedAttribute(filePath: string, name: string)Extended attributes are metadata that can be stored on a file. Note that extended attributes are not synced with iCloud.
The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of file to add an extended attribute to.
name string
Name of the extended attribute to remove.
removeTag(filePath, tag)
Section titled “removeTag(filePath, tag)”Removes a tag from a file.
removeTag(filePath: string, tag: string)The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of file to remove the tag from.
tag string
Tag to remove.
temporaryDirectory()
Section titled “temporaryDirectory()”Path of temporary directory.
temporaryDirectory(): stringUsed to retrieve the path of a temporary directory on disk. Data persisted in a temporary directory will generally live shorter than data persisted in the cache directory.
The operating system may at any time delete files stored in this directory and therefore you should not rely on it for long time storage. If you need long time storage, see documentsDirectory() or libraryDirectory(). This directory is not shared between the app, the action extension and Siri.
Return value
Section titled “Return value”string
Path to temporary directory.
write(filePath, content)
Section titled “write(filePath, content)”Write data to a file.
write(filePath: string, content: Data)Parameters
Section titled “Parameters”filePath string
Path of file to write to.
content Data
Data to write to disk.
writeExtendedAttribute(filePath, value, name)
Section titled “writeExtendedAttribute(filePath, value, name)”Writes an extended attribute to a file.
writeExtendedAttribute(filePath: string, value: string, name: string)Extended attributes are metadata that can be stored on a file. Note that extended attributes are not synced with iCloud.
The function will error if the file does not exist or if it exists in iCloud but has not been download. Use fileExists(filePath) to check if a file exists and downloadFileFromiCloud(filePath) to download the file. Note that it is always safe to call downloadFileFromiCloud(filePath), even if the file is stored locally on the device.
Parameters
Section titled “Parameters”filePath string
Path of file to add an extended attribute to.
value string
Value of the extended attribute.
name string
Name of the extended attribute. This is used to retrieve the value at a later point.
writeImage(filePath, image)
Section titled “writeImage(filePath, image)”Write an image to a file.
writeImage(filePath: string, image: Image)Writes the image to the specified file path on disk. If the file does not already exist, it will be created. If the file already exists the contents of the file will be overwritten with the new content.
Parameters
Section titled “Parameters”filePath string
Path of file to write to.
image Image
Image to write to disk.
writeString(filePath, content)
Section titled “writeString(filePath, content)”Write a string to a file.
writeString(filePath: string, content: string)Writes the content to the specified file path on disk. If the file does not already exist, it will be created. If the file already exists the contents of the file will be overwritten with the new content.
Parameters
Section titled “Parameters”filePath string
Path of file to write to.
content string
Content to write to disk.