メインコンテンツへスキップ

clipboard

システムのクリップボードでコピー&ペースト操作を実行します。

プロセス: メインレンダラー (サンドボックス化されていない場合のみ)

Linux では、selection クリップボードもあります。それを操作するには、各メソッドに selection を渡す必要があります。

const { clipboard } = require('electron')

clipboard.writeText('Example string', 'selection')
console.log(clipboard.readText('selection'))

メソッド

clipboard モジュールには次のメソッドがあります

注: 実験的な API はそのようにマークされており、将来削除される可能性があります。

clipboard.readText([type])

  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

戻り値 string - クリップボード内の内容をプレーンテキストとして返します。

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!'

clipboard.writeText(text[, type])

  • text string
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

text をプレーンテキストとしてクリップボードに書き込みます。

const { clipboard } = require('electron')

const text = 'hello i am a bit of text!'
clipboard.writeText(text)

clipboard.readHTML([type])

  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

戻り値 string - クリップボード内の内容をマークアップとして返します。

const { clipboard } = require('electron')

clipboard.writeHTML('<b>Hi</b>')
const html = clipboard.readHTML()

console.log(html)
// <meta charset='utf-8'><b>Hi</b>

clipboard.writeHTML(markup[, type])

  • markup string
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

markup をクリップボードに書き込みます。

const { clipboard } = require('electron')

clipboard.writeHTML('<b>Hi</b>')

clipboard.readImage([type])

  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

戻り値 NativeImage - クリップボード内の画像コンテンツ。

clipboard.writeImage(image[, type])

  • image NativeImage
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

image をクリップボードに書き込みます。

clipboard.readRTF([type])

  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

戻り値 string - クリップボード内の内容を RTF として返します。

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}

clipboard.writeRTF(text[, type])

  • text string
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

text を RTF でクリップボードに書き込みます。

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)

clipboard.readBookmark() macOS Windows

戻り値 Object

  • title string
  • url string

クリップボード内のブックマークを表す、title および url キーを含むオブジェクトを返します。ブックマークが利用できない場合、title および url の値は空の文字列になります。Windows では、title の値は常に空になります。

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

  • title string - Windows では未使用
  • url string
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

title (macOS のみ) および url をブックマークとしてクリップボードに書き込みます。

注: Windows のほとんどのアプリは、ブックマークを貼り付けることをサポートしていないため、clipboard.write を使用して、ブックマークとフォールバックテキストの両方をクリップボードに書き込むことができます。

const { clipboard } = require('electron')

clipboard.writeBookmark('Electron Homepage', 'https://electron.dokyumento.jp')

clipboard.readFindText() macOS

戻り値 string - 検索ペーストボードにあるテキスト。これは、アクティブなアプリケーションの検索パネルの現在の状態に関する情報を保持するペーストボードです。

このメソッドは、レンダラープロセスから呼び出されたときに同期 IPC を使用します。キャッシュされた値は、アプリケーションがアクティブ化されるたびに検索ペーストボードから再読み込みされます。

clipboard.writeFindText(text) macOS

  • text string

text を、検索ペーストボード (アクティブなアプリケーションの検索パネルの現在の状態に関する情報を保持するペーストボード) にプレーンテキストとして書き込みます。このメソッドは、レンダラープロセスから呼び出されたときに同期 IPC を使用します。

clipboard.clear([type])

  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

クリップボードの内容をクリアします。

clipboard.availableFormats([type])

  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

戻り値 string[] - クリップボードの type でサポートされている形式の配列。

const { clipboard } = require('electron')

const formats = clipboard.availableFormats()
console.log(formats)
// [ 'text/plain', 'text/html' ]

clipboard.has(format[, type]) 実験的

  • format string
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

戻り値 boolean - クリップボードが指定された format をサポートしているかどうか。

const { clipboard } = require('electron')

const hasFormat = clipboard.has('public/utf8-plain-text')
console.log(hasFormat)
// 'true' or 'false'

clipboard.read(format) 実験的

  • format string

戻り値 string - クリップボードから format タイプを読み込みます。

format には有効な ASCII 文字が含まれている必要があり、/ セパレーターが必要です。a/ca/bc は有効な形式ですが、/abcabc/a//aa は有効ではありません。

clipboard.readBuffer(format) 実験的

  • format string

戻り値 Buffer - クリップボードから format タイプを読み込みます。

const { clipboard } = require('electron')

const buffer = Buffer.from('this is binary', 'utf8')
clipboard.writeBuffer('public/utf8-plain-text', buffer)

const ret = clipboard.readBuffer('public/utf8-plain-text')

console.log(buffer.equals(ret))
// true

clipboard.writeBuffer(format, buffer[, type]) 実験的

  • format string
  • buffer Buffer
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

bufferformat としてクリップボードに書き込みます。

const { clipboard } = require('electron')

const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public/utf8-plain-text', buffer)

clipboard.write(data[, type])

  • data Object
    • text string (オプション)
    • html string (オプション)
    • image NativeImage (オプション)
    • rtf string (オプション)
    • bookmark string (オプション) - text にある URL のタイトル。
  • type string (オプション) - selection または clipboard を指定できます。デフォルトは 'clipboard' です。selection は Linux でのみ使用可能です。

data をクリップボードに書き込みます。

const { clipboard } = require('electron')

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' }