URLSessionDownloadTask represents a background download task created by
BackgroundURLSession.startDownload() or BackgroundURLSession.resumeDownload().
It allows scripts in the Scripting app to download files in the foreground or background, and the task can continue even if the script is terminated or the app is suspended.
Each download task is managed by the system and provides detailed state, progress, and event callbacks.
id: stringA unique identifier for the download task. You can use this ID to recognize or reattach to the same task when your script restarts.
Example
state: URLSessionTaskStateThe current state of the task.
Possible values:
"running" — The task is currently downloading."suspended" — The task is paused."canceling" — The task is being canceled."completed" — The task has finished."unknown" — The state is unknown (usually when the task has been removed by the system).Example
progress: URLSessionProgressReal-time progress information for the task.
Contains the following fields:
fractionCompleted: number — Progress fraction between 0 and 1.totalUnitCount: number — Total number of bytes to download.completedUnitCount: number — Number of bytes downloaded so far.isFinished: boolean — Whether the task has completed.estimatedTimeRemaining: number | null — Estimated remaining time (in seconds), or null if unknown.Example
priority: numberThe priority of the download task (range: 0.0–1.0).
Defaults to 0.5.
Higher values increase the likelihood that the system will prioritize this task.
Example
earliestBeginDate?: Date | nullThe earliest date when the download task is allowed to begin. Useful for delaying downloads or optimizing bandwidth usage.
Example
countOfBytesClientExpectsToSend: numberA best-guess upper bound on the number of bytes expected to send (for system estimation only).
countOfBytesClientExpectsToReceive: numberA best-guess upper bound on the number of bytes expected to receive (for system estimation only).
onProgress?: (details) => voidCalled periodically as the download progresses.
details includes:
progress: number — Progress fraction between 0 and 1.bytesWritten: number — Bytes written since the last update.totalBytesWritten: number — Total bytes downloaded so far.totalBytesExpectedToWrite: number — Total expected bytes for the download.Example
onFinishDownload?: (error, details) => voidCalled when the download finishes (successfully or with an error).
Parameters
error: Error | null — Error object if the download failed, otherwise null.details.temporary: string — The temporary file path used by the system.details.destination: string | null — The final destination path (if successful) or null if the download failed.The downloaded file is automatically moved to the specified
destinationpath upon completion.
Example
onComplete?: (error, resumeData) => voidCalled when the task finishes completely (either successfully or unsuccessfully).
Parameters
error: Error | null — Error object if the download failed, otherwise null.resumeData: Data | null — Resume data available if the download can be resumed later.Example
suspend(): voidSuspends the download task.
While suspended, the task produces no network activity and is not subject to timeouts.
You can later call resume() to continue.
Example
resume(): voidResumes a suspended task.
Call this only if the task is in a "suspended" state.
Example
cancel(): voidCancels the download task immediately.
The task enters the "canceling" state, and the onComplete callback is triggered with an error once cancellation finishes.
Example
cancelByProducingResumeData(): Promise<Data | null>Cancels the download task and attempts to produce resume data.
If resumable, the returned promise resolves to Data which can be used later with
BackgroundURLSession.resumeDownload().
Otherwise, it resolves to null.
Example
resume() start the download when you create a task by BackgroundURLSession.startDownload().cancelByProducingResumeData() to implement download resumption.BackgroundURLSession.getDownloadTasks() to recover existing tasks and reattach callbacks.notifyOnFinished to inform the user when a download completes.