DocumentPicker

The DocumentPicker class provides an interface to the iOS document picker, allowing users to select files or directories and export files from within the Files app. This is useful for scripts that need to access user files, share content, or organize resources in a specified directory.

Type Definitions

PickFilesOption

Options for configuring file selection with pickFiles.

  • initialDirectory (optional)

    • Type: string
    • Description: Specifies the initial directory that the document picker displays.
  • types (optional)

    • Type: string[]
    • Description: An array of uniform type identifiers (UTIs) for the document picker to display. For more details, see Uniform Type Identifiers.
  • shouldShowFileExtensions (optional)

    • Type: boolean
    • Description: Indicates if file extensions should be visible. Defaults to true.
  • allowsMultipleSelection (optional)

    • Type: boolean
    • Description: Allows selecting multiple files. Defaults to false.

ExportFilesOptions

Options for exporting files using exportFiles.

  • initialDirectory (optional)

    • Type: string
    • Description: Specifies the initial directory that the document picker displays.
  • files

    • Type: Array<{ data: Data; name: string }>
    • Description: An array of files to be exported. Each file object must contain:
      • data: The file data.
      • name: The file name.

Class Methods

DocumentPicker.pickFiles(options?: PickFilesOption): Promise<string[]>

Allows users to pick files from the Files app.

Parameters

  • options (optional): PickFilesOption
    • Configuration options for file selection.

Returns

  • A promise that resolves with an array of file paths (string[]).

Example

1async function run() {
2  const imageFilePath = await DocumentPicker.pickFiles()
3  if (imageFilePath != null) {
4    // Handle the selected file paths
5  }
6}
7run()

DocumentPicker.pickDirectory(initialDirectory?: string): Promise<string | null>

Allows users to pick a directory from the Files app.

Parameters

  • initialDirectory (optional): string
    • The initial directory that the document picker displays.

Returns

  • A promise that resolves with the selected directory path as a string, or null if the user canceled the picker.

Example

1const selectedDirectory = await DocumentPicker.pickDirectory()
2if (selectedDirectory == null) {
3  // User canceled the picker
4}

DocumentPicker.exportFiles(options: ExportFilesOptions): Promise<string[]>

Exports files to the Files app.

Parameters

  • options: ExportFilesOptions
    • Configuration options for file export, including file data and names.

Returns

  • A promise that resolves with an array of exported file paths (string[]).

Example

1async function run() {
2  const textContent = "Hello Scripting!"
3  const result = await DocumentPicker.exportFiles({
4    files: [
5      {
6        data: Data.fromString(textContent)!,
7        name: 'greeting.txt',
8      }
9    ]
10  });
11
12  if (result.length > 0) {
13    console.log('Exported files: ', result)
14  }
15}
16run()

DocumentPicker.stopAcessingSecurityScopedResources(): void

Relinquishes access to security-scoped resources, like files or directories accessed via the document picker. Use this method when you no longer need access to these resources to ensure your app manages resources efficiently.