clipboard
システムのクリップボードでコピーやペーストの操作を行います。
Process: Main, Renderer (non-sandboxed only)
[!IMPORTANT] If you want to call this API from a renderer process with context isolation enabled, place the API call in your preload script and expose it using the
contextBridgeAPI.
Linux には、 selection クリップボードも存在します。 これを操作するには、各メソッドに selection を渡す必要があります。
const { clipboard } = require('electron')
clipboard.writeText('Example string', 'selection')
console.log(clipboard.readText('selection'))
メソッド
clipboard モジュールには以下のメソッドがあります。
[!NOTE] Experimental APIs are marked as such and could be removed in future.
clipboard.readText([type])
typestring (任意) -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])
textstringtypestring (任意) -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])
typestring (任意) -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])
markupstringtypestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
クリップボードに markup を書き込みます。
const { clipboard } = require('electron')
clipboard.writeHTML('<b>Hi</b>')
clipboard.readImage([type])
typestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
Returns NativeImage - The image content in the clipboard.
clipboard.writeImage(image[, type])
imageNativeImagetypestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
クリップボードに image を書き込みます。
clipboard.readRTF([type])
typestring (任意) -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])
textstringtypestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
RTFでクリップボードに text を書き込みます。
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:
titlestringurlstring
クリップボード内のブックマークを表す title と url のキーを含む Object を返します。 ブックマークが無効なとき、title と url の値は空文字です。 title の値は Windows では常に空です。
clipboard.writeBookmark(title, url[, type]) macOS Windows
titlestring - Windows では未使用urlstringtypestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
ブックマークとしてクリップボードに title (macOS のみ) と url を書き込みます。
[!NOTE] Most apps on Windows don't support pasting bookmarks into them so you can use
clipboard.writeto write both a bookmark and fallback text to the clipboard.
const { clipboard } = require('electron')
clipboard.writeBookmark('Electron Homepage', 'https://electronjs.org')
clipboard.readFindText() macOS
戻り値 string - 検索ペーストボード上のテキストです。これは、アクティブなアプリケーションの検索パネルの現在の状態に関する情報を保持するペーストボードです。
このメソッドは、レンダラープロセスから呼び出されたとき同期 IPC を使います。 アプリケーションがアクティブにされるたびに、キャッシュされた値は、検索ペーストボードから再読込されます。
clipboard.writeFindText(text) macOS
textstring
text をプレーンテキストとして検索ペーストボード (アクティブなアプリケーションの検索パネルの現在の状態に関する情報を保持するペーストボード) に書き込みます。 このメソッドは、レンダラープロセスから呼び出されたとき同期 IPC を使います。
clipboard.clear([type])
typestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
クリップボードの内容を消去します。
clipboard.availableFormats([type])
typestring (任意) -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]) 実験的
formatstringtypestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
戻り値 boolean - クリップボードが指定した format をサポートしているかどうか。
const { clipboard } = require('electron')
const hasFormat = clipboard.has('public/utf8-plain-text')
console.log(hasFormat)
// 'true' または 'false'
clipboard.read(format) 実験的
formatstring
戻り値 string - クリップボードから format 形式で読み出します。
format は有効な ASCII 文字を含み、/ で区切られている必要があります。 a/c, a/bc are valid formats while /abc, abc/, a/, /a, a are not valid.
clipboard.readBuffer(format) 実験的
formatstring
戻り値 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]) 実験的
formatstringbufferBuffertypestring (任意) -selectionまたはclipboard。既定値は 'clipboard' です。selectionは Linux のみで有効です。
format でクリップボードに buffer を書き込みます。
const { clipboard } = require('electron')
const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public/utf8-plain-text', buffer)
clipboard.write(data[, type])
dataObjecttextstring (任意)htmlstring (任意)imageNativeImage (optional)rtfstring (任意)bookmarkstring (任意) - URL のタイトルのtext。
typestring (任意) -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' }