text/uri-list MIME type mapping
The 'text/uri-list' MIME type is mapped to the operating system's native 'copied files' clipboard format: CF_HDROP on Windows, NSFilenamesPboardType on macOS, and text/uri-list on Linux. This allows clipboard entries written by Electron to be pasted as files into native applications like Finder, Explorer, or file managers, and allows Electron to read files copied from those applications.
text/uri-list payload format
The text/uri-list payload is an RFC 2483 URI list: one file:// URI per line, separated by CRLF. Use url.pathToFileURL to convert an absolute path into a file:// URI. Although RFC 2483 permits any URI scheme, this format is files-only and non-file:// URIs are ignored.
text/uri-list file path privilege in main process
When reading text/uri-list from the clipboard in the main process, getType('text/uri-list') resolves to a Blob whose text is the file:// URI list containing real absolute paths of files. This is a privileged main-process API behavior, unlike the renderer's navigator.clipboard which sanitizes file paths for privacy.
ClipboardItem write example with multiple MIME types
const { clipboard, ClipboardItem, nativeImage } = require('electron')
const png = nativeImage.createFromPath('/path/to/icon.png').toPNG()
clipboard.write([
new ClipboardItem({
'text/plain': 'hello',
'text/html': '<b>hello</b>',
'image/png': new Blob([png], { type: 'image/png' }),
'electron application/bookmark': {
title: 'Electron',
url: 'https://electronjs.org'
}
})
])
This example shows creating a ClipboardItem with multiple MIME-typed representations including text, HTML, image, and bookmark.
ClipboardItem write files example with text/uri-list
const { clipboard, ClipboardItem } = require('electron')
const { pathToFileURL } = require('node:url')
clipboard.write([
new ClipboardItem({
'text/uri-list': [
pathToFileURL('/path/to/first.txt').href,
pathToFileURL('/path/to/second.txt').href
].join('\r\n')
})
])
This example shows writing two files to the clipboard using text/uri-list format so they can be pasted into the OS file manager.
ClipboardItem read files example with text/uri-list
async function readFiles () {
const [item] = await clipboard.read()
if (item.types.includes('text/uri-list')) {
const blob = await item.getType('text/uri-list')
if (blob instanceof Blob) {
const uriList = await blob.text()
return uriList.split(/\r?\n/).filter(Boolean)
}
}
return []
}
This example shows reading files currently on the clipboard by checking for text/uri-list type and extracting the URI list.
ClipboardItem getType example
async function dumpClipboard () {
const items = await clipboard.read()
for (const item of items) {
for (const type of item.types) {
const payload = await item.getType(type)
console.log(type, payload)
}
}
}
This example shows iterating through clipboard items and retrieving the payload for each MIME type.
ClipboardItem getType bookmark example
async function dumpClipboard () {
const bookmarkType = 'electron application/bookmark'
const items = await clipboard.read()
for (const item of items) {
if (item.types.includes(bookmarkType)) {
const bookmark = await item.getType(bookmarkType)
console.log('Bookmark found: ', bookmark)
} else {
console.log('There is no bookmark present')
}
}
}
This example shows checking for and retrieving a bookmark from clipboard items.
clipboard custom formats with electron prefix
Electron exposes custom formats using an electron prefix to avoid collisions with web formats. These custom formats are: electron application/bookmark (a URL bookmark with ClipboardBookmark object payload containing title and url), electron application/findtext (macOS only, contents of active app's find pasteboard), and electron application/osclipboard;format="<name>" (raw payload for platform-specific clipboard format, where <name> is the platform format like HTML Format on Windows or public.utf8-plain-text on macOS).
clipboard web custom format prefix
Both clipboard.read() and clipboard.write() accept arbitrary MIME types including custom formats starting with the web prefix (followed by a space, e.g. web application/x.my-format) that follow the W3C web custom format proposal.
clipboard.selection Linux-only clipboard
On Linux, there is a selection clipboard exposed via clipboard.selection sub-namespace that mirrors the top-level clipboard interface. The selection clipboard operates against the selection clipboard instead of the system clipboard. It exposes the same surface as the top-level clipboard module, with each method targeting the selection clipboard rather than the system clipboard. The two clipboards are independent: writing via clipboard.selection does not affect data returned by clipboard.read() and vice versa. The selection clipboard does not support the W3C web custom format.
clipboard.selection property
clipboard.selection is a property available on Linux only (readonly). It is a Clipboard object on Linux that operates against the selection clipboard instead of the system clipboard, and undefined on all other platforms. It exposes the same read, write, readText, writeText, has, and clear methods as the top-level clipboard module.
clipboard.writeText() example
Example: const { clipboard } = require('electron')
async function writeClipboardText () {
await clipboard.writeText('hello i am a bit of text!')
}
writeClipboardText()
clipboard.read() example
Example: const { clipboard } = require('electron')
async function dumpClipboard () {
const items = await clipboard.read()
for (const item of items) {
for (const type of item.types) {
const blob = await item.getType(type)
console.log(type, blob)
}
}
}
dumpClipboard()
clipboard.write() example with multiple formats
Example: const { clipboard, ClipboardItem, nativeImage } = require('electron')
const png = nativeImage.createFromPath('/path/to/icon.png').toPNG()
async function writeClipboard () {
await clipboard.write([
new ClipboardItem({
'text/plain': 'hello',
'text/html': '<b>hello</b>',
'image/png': new Blob([png], { type: 'image/png' }),
'electron application/bookmark': {
title: 'Electron',
url: 'https://electronjs.org'
}
})
])
}
writeClipboard()
clipboard.has() example
Example: const { clipboard } = require('electron')
async function check () {
const hasFormat = await clipboard.has('text/html')
console.log(hasFormat)
// 'true' or 'false'
const rawFormat = 'electron application/osclipboard;format="public/utf8-plain-text"'
const hasRawFormat = await clipboard.has(rawFormat)
}
check()
clipboard.selection example
Example: const { clipboard } = require('electron')
async function run () {
await clipboard.selection.writeText('Example string')
console.log(await clipboard.selection.readText())
}
run()
clipboard web custom format example
Example: const { clipboard, ClipboardItem } = require('electron')
async function writeClipboard () {
await clipboard.write([
new ClipboardItem({
'web application/x.my-app-clip': new Blob(['arbitrary payload'])
})
])
}
writeClipboard()
ses.setDownloadPath() method
ses.setDownloadPath(path) sets the download saving directory where path is a string specifying the download location. By default, the download directory will be the Downloads folder under the respective app folder.
ses.getBlobData() method
ses.getBlobData(identifier) retrieves blob data. identifier parameter is a string containing a valid UUID. Returns Promise<Buffer> - resolves with blob data.
ses.downloadURL() method
ses.downloadURL(url[, options]) initiates a download of the resource at url. url is a string; options is an optional Object with headers (Record<string, string>, optional) for HTTP request headers. The API generates a DownloadItem accessible via the will-download event. Note: does not perform security checks related to page origin, unlike webContents.downloadURL.