clipboard

    Process: Main, (non-sandboxed only)

    On Linux, there is also a selection clipboard. To manipulate it you need to pass selection to each method:

    The clipboard module has the following methods:

    Note: Experimental APIs are marked as such and could be removed in future.

    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Returns string - The content in the clipboard as plain text.

    1. const { clipboard } = require('electron')
    2. clipboard.writeText('hello i am a bit of text!')
    3. const text = clipboard.readText()
    4. console.log(text)
    5. // hello i am a bit of text!'

    clipboard.writeText(text[, type])

    • text string
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Writes the text into the clipboard as plain text.

    1. const { clipboard } = require('electron')
    2. const text = 'hello i am a bit of text!'
    3. clipboard.writeText(text)

    clipboard.readHTML([type])

    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Returns string - The content in the clipboard as markup.

    1. clipboard.writeHTML('<b>Hi</b>')
    2. const html = clipboard.readHTML()
    3. console.log(html)
    4. // <meta charset='utf-8'><b>Hi</b>

    clipboard.writeHTML(markup[, type])

    • markup string
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Writes markup to the clipboard.

    clipboard.readImage([type])

    • type string (optional) - Can be selection or ; default is ‘clipboard’. selection is only available on Linux.

    clipboard.writeImage(image[, type])

    • image
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Writes image to the clipboard.

    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Returns string - The content in the clipboard as RTF.

    1. const { clipboard } = require('electron')
    2. clipboard.writeRTF('{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}')
    3. const rtf = clipboard.readRTF()
    4. console.log(rtf)
    5. // {\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}

    clipboard.writeRTF(text[, type])

    • text string
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Writes the text into the clipboard in RTF.

    1. const { clipboard } = require('electron')
    2. const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}'
    3. clipboard.writeRTF(rtf)

    clipboard.readBookmark() macOS Windows

    Returns Object:

    • title string
    • url string

    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 bookmark is unavailable. The title value will always be empty on Windows.

    clipboard.writeBookmark(title, url[, type]) macOS Windows

    • title string - Unused on Windows
    • url string
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Writes the title (macOS only) and url into the clipboard as a bookmark.

    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.

    1. const { clipboard } = require('electron')
    2. clipboard.writeBookmark({
    3. text: 'https://electronjs.org',
    4. bookmark: 'Electron Homepage'

    clipboard.readFindText() macOS

    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.

    clipboard.writeFindText(text) macOS

    • text string

    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.

    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Clears the clipboard content.

    clipboard.availableFormats([type])

    • string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Returns string[] - An array of supported formats for the clipboard type.

    clipboard.has(format[, type]) Experimental

    • format string
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Returns boolean - Whether the clipboard supports the specified format.

    1. const { clipboard } = require('electron')
    2. const hasFormat = clipboard.has('public/utf8-plain-text')
    3. console.log(hasFormat)
    4. // 'true' or 'false'

    clipboard.read(format) Experimental

    • format string

    Returns string - Reads format type from the clipboard.

    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.

    clipboard.readBuffer(format) Experimental

    • format string

    Returns Buffer - Reads format type from the clipboard.

    1. const { clipboard } = require('electron')
    2. const buffer = Buffer.from('this is binary', 'utf8')
    3. clipboard.writeBuffer('public/utf8-plain-text', buffer)
    4. const ret = clipboard.readBuffer('public/utf8-plain-text')
    5. console.log(buffer.equals(out))
    6. // true

    clipboard.writeBuffer(format, buffer[, type]) Experimental

    • format string
    • buffer Buffer
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. selection is only available on Linux.

    Writes the buffer into the clipboard as format.

    1. const { clipboard } = require('electron')
    2. const buffer = Buffer.from('writeBuffer', 'utf8')
    3. clipboard.writeBuffer('public/utf8-plain-text', buffer)
    • data Object
      • text string (optional)
      • html string (optional)
      • image (optional)
      • rtf string (optional)
      • bookmark string (optional) - The title of the URL at text.
    • type string (optional) - Can be selection or clipboard; default is ‘clipboard’. is only available on Linux.