dialog
ファイルを開いたり、保存したり、アラートを出したりするために、ネイティブのシステムダイアログを表示します。
Process: Main
以下は複数のファイルを選択する dialog を表示する例です。
const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))
メソッド
dialog モジュールには以下のメソッドがあります。
dialog.showOpenDialogSync([window, ]options)
windowBaseWindow (任意)
戻り値 string[] | undefined - ユーザが選択したファイルパス。dialog がキャンセルされた場合は undefined を返します。
window の引数で、ダイアログは親ウインドウにアタッチされ、モーダル表示になります。
特定の種類だけにユーザーを制限したいとき、filters には、表示または選択できるファイルの種類の配列を指定します。 以下がその例です。
{
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
{ name: 'Custom File Type', extensions: ['as'] },
{ name: 'All Files', extensions: ['*'] }
]
}
extensions の配列には、ワイルドカードやドットを含む拡張子 (例えば、'png' は問題ありませんが、'.png' や '*.png' はいけません) を入れないで下さい。 すべてのファイルを表示するには、'*' ワイルドカードを使用して下さい (その他のワイルドカードはサポートされていません)。
[!NOTE] On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set
propertiesto['openFile', 'openDirectory']on these platforms, a directory selector will be shown.
dialog.showOpenDialogSync(mainWindow, {
properties: ['openFile', 'openDirectory']
})
[!NOTE] On Linux
defaultPathis not supported when using portal file chooser dialogs unless the portal backend is version 4 or higher. You can use--xdg-portal-required-versioncommand-line switch to force gtk or kde dialogs.
dialog.showOpenDialog([window, ]options)
windowBaseWindow (任意)
戻り値 Promise<Object> - 以下を含むオブジェクトで実行されます。
canceledboolean - dialog がキャンセルされたかそうでないか。filePathsstring[] - ユーザーによって選択されたファイルパスの配列. ダイアログがキャンセルされた場合、これは空の配列になります。bookmarksstring[] (任意)macOS MAS - セキュリティスコープ付きブックマークを含む base64 エンコードされたfilePaths配列にマッチする配列。 データを取り込むためにsecurityScopedBookmarksを有効にする必要があります。 (戻り値については、この表 を参照してください。)
window の引数で、ダイアログは親ウインドウにアタッチされ、モーダル表示になります。
特定の種類だけにユーザーを制限したいとき、filters には、表示または選択できるファイルの種類の配列を指定します。 以下がその例です。
{
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
{ name: 'Custom File Type', extensions: ['as'] },
{ name: 'All Files', extensions: ['*'] }
]
}
extensions の配列には、ワイルドカードやドットを含む拡張子 (例えば、'png' は問題ありませんが、'.png' や '*.png' はいけません) を入れないで下さい。 すべてのファイルを表示するには、'*' ワイルドカードを使用して下さい (その他のワイルドカードはサポートされていません)。
[!NOTE] On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set
propertiesto['openFile', 'openDirectory']on these platforms, a directory selector will be shown.
dialog.showOpenDialog(mainWindow, {
properties: ['openFile', 'openDirectory']
}).then(result => {
console.log(result.canceled)
console.log(result.filePaths)
}).catch(err => {
console.log(err)
})
[!NOTE] On Linux
defaultPathis not supported when using portal file chooser dialogs unless the portal backend is version 4 or higher. You can use--xdg-portal-required-versioncommand-line switch to force gtk or kde dialogs.
dialog.showSaveDialogSync([window, ]options)
windowBaseWindow (任意)
戻り値 string - ユーザが選択したファイルパス。dialog がキャンセルされた場合は空文字列を返します。
window の引数で、ダイアログは親ウインドウにアタッチされ、モーダル表示になります。
filters には、表示することのできるファイルの種類の配列を指定します。例については、dialog.showOpenDialog を参照して下さい。
dialog.showSaveDialog([window, ]options)
windowBaseWindow (任意)
戻り値 Promise<Object> - 以下を含むオブジェクトで実行されます。
canceledboolean - dialog がキャンセルされたかそうでないか。filePathstring - ダイアログがキャンセルされると、これは空文字列になります。bookmarkstring (任意)macOS MAS - 保存されたファイルのセキュリティスコープのブックマークデータを含む Base64 エンコードされた文字列。 出力するためにsecurityScopedBookmarksを有効にする必要があります。 (戻り値については、この表 を参照してください。)
window の引数で、ダイアログは親ウインドウにアタッチされ、モーダル表示になります。
filters には、表示することのできるファイルの種類の配列を指定します。例については、dialog.showOpenDialog を参照して下さい。
[!NOTE] On macOS, using the asynchronous version is recommended to avoid issues when expanding and collapsing the dialog.
dialog.showMessageBoxSync([window, ]options)
windowBaseWindow (任意)
戻り値 Integer - クリックされたボタンのインデックス。
メッセージボックスを表示し、メッセージボックスが閉じられるまでプロセスをブロックします。 クリックされたボタンのインデックスを返します。
window の引数で、ダイアログは親ウインドウにアタッチされ、モーダル表示になります。 window が表示されていない場合、dialog はアタッチされません。 この場合は独立したウインドウとして表示されます。
dialog.showMessageBox([window, ]options)
windowBaseWindow (任意)
戻り値 Promise<Object> - 以下のプロパティを含む Promise で解決されます。
responsenumber - クリックされたボタンのインデックス。checkboxCheckedboolean -checkboxLabelが設定された場合、チェックボックスのチェック状態。 そうでない場合はfalseになります。
メッセージボックスを表示します。
window の引数で、ダイアログは親ウインドウにアタッチされ、モーダル表示になります。
dialog.showErrorBox(title, content)
titlestring - エラーボックスに表示するタイトル.contentstring - エラーボックスに表示するテキストの内容.
エラーメッセージを表示するモーダルダイアログを表示します。
app モジュールで ready イベントが発生する前でも、このAPIは安全に呼び出すことができます。これは、起動の初期段階でのエラーを報告するのによく使用されます。 Linuxで、appの ready イベントの前に呼び出すと、メッセージは標準エラーに出力され、GUIのダイアログは表示されません。
dialog.showCertificateTrustDialog([window, ]options) macOS Windows
windowBaseWindow (任意)
戻り値 Promise<void> - 証明書信頼ダイアログが表示されると実行されます。
macOSでは、これはメッセージと証明書情報を表示するモーダルダイアログを表示し、ユーザーに証明書を信頼/インポートする選択肢を提供します。 window の引数を指定すると、ダイアログは親ウインドウにアタッチされ、モーダル表示になります。
Windowsでは、使用されているWin32 APIのため、オプションはより限定的です。
- OSが独自の確認ダイアログを提供しているため、
messageの引数は使用されません。 - この確認ダイアログをモーダル表示にすることができないため、
windowの引数は無視されます。
ブックマーク配列
showOpenDialog and showSaveDialog resolve to an object with a bookmarks field. This field is an array of Base64 encoded strings that contain the security scoped bookmark data for the saved file. The securityScopedBookmarks option must be enabled for this to be present.
| ビルド種別 | securityScopedBookmarks 真偽値 | 戻り値の型 | 返り値 |
|---|---|---|---|
| macOS mas | True | Success | ['LONGBOOKMARKSTRING'] |
| macOS mas | True | エラー | [''] (空文字列の配列) |
| macOS mas | False | なし | [] (空の配列) |
| non mas | any | なし | [] (空の配列) |
シート
macOS では、window パラメータに BaseWindow の参照を指定した場合、ダイアログはウインドウにアタッチされたシートとして表示されます。ウインドウを指定しない場合、モーダルで表示されます。
BaseWindow.getCurrentWindow().setSheetOffset(offset) を呼び出すことで、シートがアタッチされるウインドウフレームからのオフセットを変更することができます。