Skip to main content

dialog

显示用于打开和保存文件、警告等的本地系统对话框。

进程:主进程

🌐 Process: Main

显示选择多个文件的对话框的示例:

🌐 An example of showing a dialog to select multiple files:

const { dialog } = require('electron')

console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))

方法

🌐 Methods

dialog 模块具有以下方法:

🌐 The dialog module has the following methods:

dialog.showOpenDialogSync([window, ]options)

  • window BaseWindow(可选)
  • options 对象
    • title 字符串(可选)
    • defaultPath 字符串(可选)
    • buttonLabel 字符串(可选)- 自定义确认按钮的标签,若留空将使用默认标签。
    • filters 文件过滤器[](可选)
    • properties 字符串数组(可选)- 包含对话框应使用的功能。支持以下值:
      • openFile - 允许选择文件。
      • openDirectory - 允许选择目录。
      • multiSelections - 允许选择多个路径。
      • showHiddenFiles - 在对话框中显示隐藏文件。
      • createDirectory macOS - 允许从对话框中创建新目录。
      • promptToCreate Windows - 如果对话框中输入的文件路径不存在,则提示创建。 这并不会实际在该路径上创建文件,但允许返回不存在的路径,这些路径应由应用创建。
      • noResolveAliases macOS - 禁用自动别名(符号链接)路径解析。所选别名现在将返回别名路径,而不是它们的目标路径。
      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。
      • dontAddToRecent Windows - 不要将正在打开的项目添加到最近文档列表中。
    • message 字符串(可选) macOS - 显示在输入框上方的消息。
    • securityScopedBookmarks 布尔值(可选)macOS MAS - 当为 Mac App Store 打包时创建 安全范围书签

返回 string[] | undefined,即用户选择的文件路径;如果对话框被取消,则返回 undefined

🌐 Returns string[] | undefined, the file paths chosen by the user; if the dialog is cancelled it returns undefined.

window 参数允许对话框附加到父窗口,使其成为模态窗口。

🌐 The window argument allows the dialog to attach itself to a parent window, making it modal.

filters 指定了一个文件类型数组,当你想限制用户只能选择特定类型时,可以显示或选择这些类型。例如:

🌐 The filters specifies an array of file types that can be displayed or selected when you want to limit the user to a specific type. For example:

{
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' 是错误的)。要显示所有文件,请使用 '*' 通配符(不支持其他通配符)。

🌐 The extensions array should contain extensions without wildcards or dots (e.g. 'png' is good but '.png' and '*.png' are bad). To show all files, use the '*' wildcard (no other wildcard is supported).

note

在 Windows 和 Linux 上,打开对话框不能同时作为文件选择器和目录选择器使用,因此如果你在这些平台上将 properties 设置为 ['openFile', 'openDirectory'],将会显示目录选择器。

dialog.showOpenDialogSync(mainWindow, {
properties: ['openFile', 'openDirectory']
})
note

在 Linux 上,除非门户后端版本为 4 或更高,否则在使用门户文件选择器对话框时不支持 defaultPath。 你可以使用 --xdg-portal-required-version 命令行选项 来强制使用 gtk 或 kde 对话框。

dialog.showOpenDialog([window, ]options)

  • window BaseWindow(可选)
  • options 对象
    • title 字符串(可选)
    • defaultPath 字符串(可选)
    • buttonLabel 字符串(可选)- 自定义确认按钮的标签,若留空将使用默认标签。
    • filters 文件过滤器[](可选)
    • properties 字符串数组(可选)- 包含对话框应使用的功能。支持以下值:
      • openFile - 允许选择文件。
      • openDirectory - 允许选择目录。
      • multiSelections - 允许选择多个路径。
      • showHiddenFiles - 在对话框中显示隐藏文件。
      • createDirectory macOS - 允许从对话框中创建新目录。
      • promptToCreate Windows - 如果对话框中输入的文件路径不存在,则提示创建。 这并不会实际在该路径上创建文件,但允许返回不存在的路径,这些路径应由应用创建。
      • noResolveAliases macOS - 禁用自动别名(符号链接)路径解析。所选别名现在将返回别名路径,而不是它们的目标路径。
      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。
      • dontAddToRecent Windows - 不要将正在打开的项目添加到最近文档列表中。
    • message 字符串(可选) macOS - 显示在输入框上方的消息。
    • securityScopedBookmarks 布尔值(可选)macOS MAS - 当为 Mac App Store 打包时创建 安全范围书签

返回 Promise<Object> - 解析为包含以下内容的对象:

🌐 Returns Promise<Object> - Resolve with an object containing the following:

  • canceled 布尔值 - 对话框是否被取消。
  • filePaths string[] - 用户选择的文件路径数组。如果对话框被取消,将会是一个空数组。
  • bookmarks string[](可选)macOS MAS - 一个与 filePaths 数组匹配的 base64 编码字符串数组,包含安全范围书签数据。必须启用 securityScopedBookmarks 才能填充此数据。(有关返回值,请参见此表

window 参数允许对话框附加到父窗口,使其成为模态窗口。

🌐 The window argument allows the dialog to attach itself to a parent window, making it modal.

filters 指定了一个文件类型数组,当你想限制用户只能选择特定类型时,可以显示或选择这些类型。例如:

🌐 The filters specifies an array of file types that can be displayed or selected when you want to limit the user to a specific type. For example:

{
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' 是错误的)。要显示所有文件,请使用 '*' 通配符(不支持其他通配符)。

🌐 The extensions array should contain extensions without wildcards or dots (e.g. 'png' is good but '.png' and '*.png' are bad). To show all files, use the '*' wildcard (no other wildcard is supported).

note

在 Windows 和 Linux 上,打开对话框不能同时作为文件选择器和目录选择器使用,因此如果你在这些平台上将 properties 设置为 ['openFile', 'openDirectory'],将会显示目录选择器。

dialog.showOpenDialog(mainWindow, {
properties: ['openFile', 'openDirectory']
}).then(result => {
console.log(result.canceled)
console.log(result.filePaths)
}).catch(err => {
console.log(err)
})
note

在 Linux 上,除非门户后端版本为 4 或更高,否则在使用门户文件选择器对话框时不支持 defaultPath。 你可以使用 --xdg-portal-required-version 命令行选项 来强制使用 gtk 或 kde 对话框。

dialog.showSaveDialogSync([window, ]options)

  • window BaseWindow(可选)
  • options 对象
    • title 字符串(可选)- 对话框标题。在某些 Linux 桌面环境中可能无法显示。
    • defaultPath 字符串(可选)- 默认使用的绝对目录路径、绝对文件路径或文件名。
    • buttonLabel 字符串(可选)- 自定义确认按钮的标签,若留空将使用默认标签。
    • filters 文件过滤器[](可选)
    • message 字符串(可选)macOS - 显示在文本字段上方的消息。
    • nameFieldLabel 字符串(可选)macOS - 自定义标签,用于显示在文件名文本字段前的文本。
    • showsTagField 布尔值(可选)macOS - 显示标签输入框,默认为 true
    • properties 字符串数组(可选)
      • showHiddenFiles - 在对话框中显示隐藏文件。
      • createDirectory macOS - 允许从对话框中创建新目录。
      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。
      • showOverwriteConfirmation Linux - 设置当用户输入已存在的文件名时是否显示确认对话框。
      • dontAddToRecent Windows - 不要将正在保存的项目添加到最近文档列表中。
    • securityScopedBookmarks 布尔值(可选)macOS MAS - 当为 Mac App Store 打包时,创建一个 安全范围书签。如果启用此选项并且文件尚不存在,将在选定路径创建一个空白文件。

返回 string,即用户选择的文件路径;如果对话框被取消,则返回空字符串。

🌐 Returns string, the path of the file chosen by the user; if the dialog is cancelled it returns an empty string.

window 参数允许对话框附加到父窗口,使其成为模态窗口。

🌐 The window argument allows the dialog to attach itself to a parent window, making it modal.

filters 指定可以显示的文件类型数组,示例请参见 dialog.showOpenDialog

🌐 The filters specifies an array of file types that can be displayed, see dialog.showOpenDialog for an example.

dialog.showSaveDialog([window, ]options)

  • window BaseWindow(可选)
  • options 对象
    • title 字符串(可选)- 对话框标题。在某些 Linux 桌面环境中可能无法显示。
    • defaultPath 字符串(可选)- 默认使用的绝对目录路径、绝对文件路径或文件名。
    • buttonLabel 字符串(可选)- 自定义确认按钮的标签,若留空将使用默认标签。
    • filters 文件过滤器[](可选)
    • message 字符串(可选)macOS - 显示在文本字段上方的消息。
    • nameFieldLabel 字符串(可选)macOS - 自定义标签,用于显示在文件名文本字段前的文本。
    • showsTagField 布尔值(可选) macOS - 显示标签输入框,默认值为 true
    • properties 字符串数组(可选)
      • showHiddenFiles - 在对话框中显示隐藏文件。
      • createDirectory macOS - 允许从对话框中创建新目录。
      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。
      • showOverwriteConfirmation Linux - 设置当用户输入已存在的文件名时是否显示确认对话框。
      • dontAddToRecent Windows - 不要将正在保存的项目添加到最近文档列表中。
    • securityScopedBookmarks 布尔值(可选)macOS MAS - 当为 Mac App Store 打包时,创建一个 安全范围书签。如果启用此选项并且文件尚不存在,将在选定路径创建一个空白文件。

返回 Promise<Object> - 解析为包含以下内容的对象:

🌐 Returns Promise<Object> - Resolve with an object containing the following:

  • canceled 布尔值 - 对话框是否被取消。
  • filePath 字符串 - 如果对话被取消,则此字符串将为空。
  • bookmark 字符串(可选)macOS MAS - 包含已保存文件的安全范围书签数据的 Base64 编码字符串。必须启用 securityScopedBookmarks 才会出现此内容。(有关返回值,请参见 此处表格。)

window 参数允许对话框附加到父窗口,使其成为模态窗口。

🌐 The window argument allows the dialog to attach itself to a parent window, making it modal.

filters 指定可以显示的文件类型数组,示例请参见 dialog.showOpenDialog

🌐 The filters specifies an array of file types that can be displayed, see dialog.showOpenDialog for an example.

note

在 macOS 上,建议使用异步版本,以避免在展开和折叠对话框时出现问题。

dialog.showMessageBoxSync([window, ]options)

  • window BaseWindow(可选)
  • options 对象
    • message 字符串 - 消息框的内容。
    • type 字符串(可选)- 可以是 noneinfoerrorquestionwarning。在 Windows 上,question 显示的图标与 info 相同,除非你使用 icon 选项设置图标。在 macOS 上,warningerror 都显示相同的警告图标。
    • buttons string[](可选)- 按钮文本的数组。在 Windows 上,如果数组为空,将显示一个标记为“确定”的按钮。
    • defaultId 整数(可选)- 消息框打开时默认选中的按钮在按钮数组中的索引。
    • title 字符串(可选)- 消息框的标题,某些平台可能不会显示它。
    • detail 字符串(可选)- 消息的额外信息。
    • icon (NativeImage | 字符串)(可选)
    • textWidth 整数(可选) macOS - 消息框中文本的自定义宽度。
    • cancelId 整数(可选)- 用于取消对话框的按钮索引,通过 Esc 键触发。默认情况下,会分配给第一个标签为“取消”或“否”的按钮。如果不存在此类标签的按钮且未设置此选项,则返回值将使用 0
    • noLink 布尔值(可选) - 在 Windows 上,Electron 会尝试识别 buttons 中哪些是常用按钮(例如“取消”或“是”),并将其他按钮显示为对话框中的命令链接。这样可以让对话框看起来像现代 Windows 应用的风格。如果你不希望有这种行为,可以将 noLink 设置为 true
    • normalizeAccessKeys 布尔值(可选)- 跨平台规范化键盘访问键。默认值是 false。启用此选项假设在按钮标签中使用 & 来放置键盘快捷键访问键,并且标签将被转换以在每个平台上正确工作,macOS 上会删除 & 字符,Linux 上会转换为 _,Windows 上保持不变。例如,按钮标签 Vie&w 在 Linux 上会转换为 Vie_w,在 macOS 上会转换为 View,并且可以在 Windows 和 Linux 上通过 Alt-W 进行选择。

返回 Integer —— 点击按钮的索引。

🌐 Returns Integer - the index of the clicked button.

显示一个消息框,它会阻塞进程直到消息框被关闭。它返回被点击按钮的索引。

🌐 Shows a message box, it will block the process until the message box is closed. It returns the index of the clicked button.

window 参数允许对话框附加到父窗口,使其成为模态窗口。如果未显示 window,对话框将不会附加到它。在这种情况下,它将作为独立窗口显示。

🌐 The window argument allows the dialog to attach itself to a parent window, making it modal. If window is not shown dialog will not be attached to it. In such case it will be displayed as an independent window.

dialog.showMessageBox([window, ]options)

  • window BaseWindow(可选)
  • options 对象
    • message 字符串 - 消息框的内容。
    • type 字符串(可选)- 可以是 noneinfoerrorquestionwarning。在 Windows 上,question 显示的图标与 info 相同,除非你使用 icon 选项设置图标。在 macOS 上,warningerror 都显示相同的警告图标。
    • buttons string[](可选)- 按钮文本的数组。在 Windows 上,如果数组为空,将显示一个标记为“确定”的按钮。
    • defaultId 整数(可选)- 消息框打开时默认选中的按钮在按钮数组中的索引。
    • signal AbortSignal(可选)- 传入一个 中止信号 实例以可选地关闭消息框,消息框将表现得像是被用户取消。在 macOS 上,signal 对没有父窗口的消息框无效,因为由于平台限制,这些消息框是同步运行的。
    • title 字符串(可选)- 消息框的标题,某些平台可能不会显示它。
    • detail 字符串(可选)- 消息的额外信息。
    • checkboxLabel 字符串(可选)- 如果提供,消息框将包含一个带有指定标签的复选框。
    • checkboxChecked 布尔值(可选)- 复选框的初始选中状态。默认值为 false
    • icon (NativeImage | 字符串)(可选)
    • textWidth 整数(可选) macOS - 消息框中文本的自定义宽度。
    • cancelId 整数(可选)- 用于取消对话框的按钮索引,通过 Esc 键触发。默认情况下,会分配给第一个标签为“取消”或“否”的按钮。如果不存在此类标签的按钮且未设置此选项,则返回值将使用 0
    • noLink 布尔值(可选) - 在 Windows 上,Electron 会尝试识别 buttons 中哪些是常用按钮(例如“取消”或“是”),并将其他按钮显示为对话框中的命令链接。这样可以让对话框看起来像现代 Windows 应用的风格。如果你不希望有这种行为,可以将 noLink 设置为 true
    • normalizeAccessKeys 布尔值(可选)- 跨平台规范化键盘访问键。默认值是 false。启用此选项假设在按钮标签中使用 & 来放置键盘快捷键访问键,并且标签将被转换以在每个平台上正确工作,macOS 上会删除 & 字符,Linux 上会转换为 _,Windows 上保持不变。例如,按钮标签 Vie&w 在 Linux 上会转换为 Vie_w,在 macOS 上会转换为 View,并且可以在 Windows 和 Linux 上通过 Alt-W 进行选择。

返回 Promise<Object> - 解析为包含以下属性的 Promise:

🌐 Returns Promise<Object> - resolves with a promise containing the following properties:

  • response 编号 - 被点击按钮的索引。
  • checkboxChecked 布尔值 - 如果设置了 checkboxLabel,则表示复选框的选中状态。否则为 false

显示消息框。

🌐 Shows a message box.

window 参数允许对话框附加到父窗口,使其成为模态窗口。

🌐 The window argument allows the dialog to attach itself to a parent window, making it modal.

dialog.showErrorBox(title, content)

  • title 字符串 - 在错误框中显示的标题。
  • content 字符串 - 要在错误框中显示的文本内容。

显示一个显示错误消息的模式对话框。

🌐 Displays a modal dialog that shows an error message.

app 模块触发 ready 事件之前,可以安全调用此 API,通常用于报告启动早期的错误。如果在 Linux 上应用触发 ready 事件之前调用,则消息将输出到 stderr,并且不会出现 GUI 对话框。

🌐 This API can be called safely before the ready event the app module emits, it is usually used to report errors in early stage of startup. If called before the app ready event on Linux, the message will be emitted to stderr, and no GUI dialog will appear.

dialog.showCertificateTrustDialog([window, ]options) macOS Windows

  • window BaseWindow(可选)
  • options 对象
    • certificate 证书 - 要信任/导入的证书。
    • message 字符串 - 要显示给用户的消息。

返回 Promise<void> - 当显示证书信任对话框时解析。

🌐 Returns Promise<void> - resolves when the certificate trust dialog is shown.

在 macOS 上,这会显示一个模态对话框,显示信息和证书信息,并给用户选择信任/导入证书的选项。如果提供 window 参数,对话框将附加到父窗口,使其成为模态窗口。

🌐 On macOS, this displays a modal dialog that shows a message and certificate information, and gives the user the option of trusting/importing the certificate. If you provide a window argument the dialog will be attached to the parent window, making it modal.

在 Windows 上,由于使用了 Win32 API,选项更加有限:

🌐 On Windows the options are more limited, due to the Win32 APIs used:

  • message 参数未使用,因为操作系统提供了自己的确认对话框。
  • window 参数会被忽略,因为无法将此确认对话框设置为模态。

书签数组

🌐 Bookmarks array

showOpenDialogshowSaveDialog 解析为一个包含 bookmarks 字段的对象。该字段是一个 Base64 编码字符串数组,包含已保存文件的 安全范围书签 数据。必须启用 securityScopedBookmarks 选项才能存在此字段。

构建类型securityScopedBookmarks 布尔值返回类型返回值
macOS masTrue成功['LONGBOOKMARKSTRING']
macOS masTrue错误[''](空字符串数组)
macOS masFalse不适用[](空数组)
非 mas任意不适用[](空数组)

床单

🌐 Sheets

在 macOS 上,如果你在 window 参数中提供了 BaseWindow 引用,对话框将作为附加在窗口上的面板显示;如果没有提供窗口,则显示为模态窗口。

🌐 On macOS, dialogs are presented as sheets attached to a window if you provide a BaseWindow reference in the window parameter, or modals if no window is provided.

你可以调用 BaseWindow.getCurrentWindow().setSheetOffset(offset) 来更改工作表附着在窗口框架上的偏移量。

🌐 You can call BaseWindow.getCurrentWindow().setSheetOffset(offset) to change the offset from the window frame where sheets are attached.