2013-09-09 15:35:57 +08:00
# clipboard
2013-08-14 15:43:35 -07:00
2016-04-21 15:39:12 -07:00
> Perform copy and paste operations on the system clipboard.
2016-04-21 15:35:29 -07:00
2023-01-19 11:59:35 -08:00
Process: [Main ](../glossary.md#main-process ), [Renderer ](../glossary.md#renderer-process ) (non-sandboxed only)
2016-11-01 16:35:31 -07:00
2019-05-06 08:11:48 -07:00
On Linux, there is also a `selection` clipboard. To manipulate it
2014-06-05 14:48:12 +08:00
you need to pass `selection` to each method:
2023-11-20 23:50:08 -08:00
```js
2018-09-14 02:10:51 +10:00
const { clipboard } = require('electron')
2019-09-18 16:00:43 -07:00
2021-11-16 05:13:18 +01:00
clipboard.writeText('Example string', 'selection')
2016-07-25 18:39:25 -07:00
console.log(clipboard.readText('selection'))
2014-06-05 14:48:12 +08:00
```
2015-08-24 14:35:43 -07:00
## Methods
2014-06-05 14:48:12 +08:00
2015-08-26 16:28:44 -07:00
The `clipboard` module has the following methods:
2015-08-24 14:35:43 -07:00
2015-08-28 10:34:48 -07:00
**Note:** Experimental APIs are marked as such and could be removed in future.
2015-08-24 14:35:43 -07:00
### `clipboard.readText([type])`
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2013-08-14 15:43:35 -07:00
2021-11-16 05:13:18 +01:00
Returns `string` - The content in the clipboard as plain text.
2013-08-14 15:43:35 -07:00
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
clipboard.writeText('hello i am a bit of text!')
const text = clipboard.readText()
console.log(text)
// hello i am a bit of text!'
```
2015-08-24 14:35:43 -07:00
### `clipboard.writeText(text[, type])`
2013-08-14 15:43:35 -07:00
2021-11-16 05:13:18 +01:00
* `text` string
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2013-08-14 15:43:35 -07:00
2015-07-23 18:01:00 +01:00
Writes the `text` into the clipboard as plain text.
2013-08-14 15:43:35 -07:00
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
const text = 'hello i am a bit of text!'
clipboard.writeText(text)
```
2016-05-26 14:26:12 -07:00
### `clipboard.readHTML([type])`
2015-05-22 14:59:11 +05:30
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2015-05-22 14:59:11 +05:30
2021-11-16 05:13:18 +01:00
Returns `string` - The content in the clipboard as markup.
2015-05-22 14:59:11 +05:30
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
clipboard.writeHTML('< b > Hi< / b > ')
const html = clipboard.readHTML()
console.log(html)
// < meta charset = 'utf-8' > < b > Hi< / b >
```
2016-05-26 14:26:12 -07:00
### `clipboard.writeHTML(markup[, type])`
2015-05-22 14:59:11 +05:30
2021-11-16 05:13:18 +01:00
* `markup` string
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2015-05-22 14:59:11 +05:30
2015-08-24 14:35:43 -07:00
Writes `markup` to the clipboard.
2015-05-22 14:59:11 +05:30
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
2022-02-01 03:57:33 -05:00
clipboard.writeHTML('< b > Hi< / b > ')
2019-09-18 16:00:43 -07:00
```
2015-08-24 14:35:43 -07:00
### `clipboard.readImage([type])`
2015-03-26 12:53:48 +08:00
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2015-03-26 12:53:48 +08:00
2016-12-19 09:40:07 -08:00
Returns [`NativeImage` ](native-image.md ) - The image content in the clipboard.
2015-03-26 12:53:48 +08:00
2015-08-24 14:35:43 -07:00
### `clipboard.writeImage(image[, type])`
2015-03-26 12:53:48 +08:00
* `image` [NativeImage ](native-image.md )
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2015-08-24 14:35:43 -07:00
Writes `image` to the clipboard.
2015-03-26 12:53:48 +08:00
2016-05-26 14:26:12 -07:00
### `clipboard.readRTF([type])`
2016-02-05 09:06:21 +01:00
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2016-02-05 09:06:21 +01:00
2021-11-16 05:13:18 +01:00
Returns `string` - The content in the clipboard as RTF.
2016-02-05 09:06:21 +01:00
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
clipboard.writeRTF('{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}')
const rtf = clipboard.readRTF()
console.log(rtf)
// {\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}
```
2016-05-26 14:26:12 -07:00
### `clipboard.writeRTF(text[, type])`
2016-02-04 19:38:47 +01:00
2021-11-16 05:13:18 +01:00
* `text` string
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2016-02-04 19:38:47 +01:00
Writes the `text` into the clipboard in RTF.
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}'
clipboard.writeRTF(rtf)
```
2016-06-24 17:16:38 -07:00
### `clipboard.readBookmark()` _macOS_ _Windows_
2016-06-24 15:20:49 -07:00
2016-09-25 12:59:30 +13:00
Returns `Object` :
2016-10-25 12:35:18 +09:00
2021-11-16 05:13:18 +01:00
* `title` string
* `url` string
2016-09-25 12:59:30 +13:00
2016-06-24 15:35:30 -07:00
Returns an Object containing `title` and `url` keys representing the bookmark in
the clipboard. The `title` and `url` values will be empty strings when the
2021-09-01 15:55:07 -04:00
bookmark is unavailable. The `title` value will always be empty on Windows.
2016-06-24 15:20:49 -07:00
2016-06-24 17:16:38 -07:00
### `clipboard.writeBookmark(title, url[, type])` _macOS_ _Windows_
2016-06-24 15:20:49 -07:00
2021-11-16 05:13:18 +01:00
* `title` string - Unused on Windows
* `url` string
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2016-06-24 15:20:49 -07:00
2021-09-01 15:55:07 -04:00
Writes the `title` (macOS only) and `url` into the clipboard as a bookmark.
2016-06-24 15:20:49 -07:00
2016-09-21 10:28:37 -07:00
**Note:** Most apps on Windows don't support pasting bookmarks into them so
you can use `clipboard.write` to write both a bookmark and fallback text to the
clipboard.
```js
2019-09-18 16:00:43 -07:00
const { clipboard } = require('electron')
2023-06-05 15:26:26 +08:00
clipboard.writeBookmark('Electron Homepage', 'https://electronjs.org')
2016-09-21 10:28:37 -07:00
```
2016-10-25 14:09:42 +09:00
### `clipboard.readFindText()` _macOS_
2021-11-16 05:13:18 +01:00
Returns `string` - The text on the find pasteboard, which is the pasteboard that holds information about the current state of the active application’ s find panel.
2019-09-18 16:00:43 -07:00
This method uses synchronous IPC when called from the renderer process.
The cached value is reread from the find pasteboard whenever the application is activated.
2016-10-25 14:09:42 +09:00
### `clipboard.writeFindText(text)` _macOS_
2021-11-16 05:13:18 +01:00
* `text` string
2016-10-25 14:09:42 +09:00
2019-09-18 16:00:43 -07:00
Writes the `text` into the find pasteboard (the pasteboard that holds information about the current state of the active application’ s find panel) as plain text. This method uses synchronous IPC when called from the renderer process.
2016-10-25 14:09:42 +09:00
2015-08-24 14:35:43 -07:00
### `clipboard.clear([type])`
2015-03-26 12:53:48 +08:00
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2014-06-05 14:48:12 +08:00
2015-08-24 14:35:43 -07:00
Clears the clipboard content.
2013-08-14 15:43:35 -07:00
2015-08-24 14:35:43 -07:00
### `clipboard.availableFormats([type])`
2013-08-14 15:43:35 -07:00
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2013-08-14 15:43:35 -07:00
2021-11-16 05:13:18 +01:00
Returns `string[]` - An array of supported formats for the clipboard `type` .
2013-08-14 15:43:35 -07:00
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
const formats = clipboard.availableFormats()
console.log(formats)
// [ 'text/plain', 'text/html' ]
```
2017-03-16 15:45:41 -07:00
### `clipboard.has(format[, type])` _Experimental_
2013-08-14 15:43:35 -07:00
2021-11-16 05:13:18 +01:00
* `format` string
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2013-08-14 15:43:35 -07:00
2021-11-16 05:13:18 +01:00
Returns `boolean` - Whether the clipboard supports the specified `format` .
2015-05-27 13:35:51 +05:30
2019-09-18 16:00:43 -07:00
```js
2018-09-14 02:10:51 +10:00
const { clipboard } = require('electron')
2019-09-18 16:00:43 -07:00
2021-11-01 10:00:36 -04:00
const hasFormat = clipboard.has('public/utf8-plain-text')
2019-09-18 16:00:43 -07:00
console.log(hasFormat)
2021-07-06 08:17:13 -04:00
// 'true' or 'false'
2015-05-27 13:35:51 +05:30
```
2017-03-16 15:45:41 -07:00
### `clipboard.read(format)` _Experimental_
2015-05-27 13:35:51 +05:30
2021-11-16 05:13:18 +01:00
* `format` string
2017-03-16 15:45:41 -07:00
2021-11-16 05:13:18 +01:00
Returns `string` - Reads `format` type from the clipboard.
2017-03-16 15:45:41 -07:00
2021-07-06 08:17:13 -04:00
`format` should contain valid ASCII characters and have `/` separator.
`a/c` , `a/bc` are valid formats while `/abc` , `abc/` , `a/` , `/a` , `a`
are not valid.
2017-03-16 15:45:41 -07:00
### `clipboard.readBuffer(format)` _Experimental_
2021-11-16 05:13:18 +01:00
* `format` string
2015-05-27 13:35:51 +05:30
2017-03-16 15:45:41 -07:00
Returns `Buffer` - Reads `format` type from the clipboard.
2013-08-14 15:43:35 -07:00
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
const buffer = Buffer.from('this is binary', 'utf8')
2021-07-06 08:17:13 -04:00
clipboard.writeBuffer('public/utf8-plain-text', buffer)
2019-09-18 16:00:43 -07:00
2021-07-06 08:17:13 -04:00
const ret = clipboard.readBuffer('public/utf8-plain-text')
2019-09-18 16:00:43 -07:00
2023-03-02 19:24:59 +08:00
console.log(buffer.equals(ret))
2019-09-18 16:00:43 -07:00
// true
```
2017-04-21 16:26:57 +09:00
### `clipboard.writeBuffer(format, buffer[, type])` _Experimental_
2017-04-21 16:23:25 +09:00
2021-11-16 05:13:18 +01:00
* `format` string
2017-04-21 16:23:25 +09:00
* `buffer` Buffer
2021-11-16 05:13:18 +01:00
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2017-04-21 16:23:25 +09:00
2017-04-21 16:30:23 +09:00
Writes the `buffer` into the clipboard as `format` .
2017-04-21 16:23:25 +09:00
2019-09-18 16:00:43 -07:00
```js
const { clipboard } = require('electron')
const buffer = Buffer.from('writeBuffer', 'utf8')
2021-07-06 08:17:13 -04:00
clipboard.writeBuffer('public/utf8-plain-text', buffer)
2019-09-18 16:00:43 -07:00
```
2015-08-24 14:35:43 -07:00
### `clipboard.write(data[, type])`
2015-07-07 10:47:33 +05:30
* `data` Object
2021-11-16 05:13:18 +01:00
* `text` string (optional)
* `html` string (optional)
2016-10-30 21:46:20 +11:00
* `image` [NativeImage ](native-image.md ) (optional)
2021-11-16 05:13:18 +01:00
* `rtf` string (optional)
* `bookmark` string (optional) - The title of the URL at `text` .
* `type` string (optional) - Can be `selection` or `clipboard` ; default is 'clipboard'. `selection` is only available on Linux.
2015-07-07 10:47:33 +05:30
2019-09-18 16:00:43 -07:00
Writes `data` to the clipboard.
```js
2018-09-14 02:10:51 +10:00
const { clipboard } = require('electron')
2019-09-18 16:00:43 -07:00
clipboard.write({
text: 'test',
html: '< b > Hi< / b > ',
rtf: '{\\rtf1\\utf8 text}',
bookmark: 'a title'
})
console.log(clipboard.readText())
// 'test'
console.log(clipboard.readHTML())
// < meta charset = 'utf-8' > < b > Hi< / b >
console.log(clipboard.readRTF())
// '{\\rtf1\\utf8 text}'
console.log(clipboard.readBookmark())
// { title: 'a title', url: 'test' }
2015-07-07 10:47:33 +05:30
```