Skip to main content

dialog

显示用于打开和保存文件、警报等的原生系统对话框。

¥Display native system dialogs for opening and saving files, alerting, etc.

进程:主进程

¥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(可选)

    ¥window BaseWindow (optional)

  • options 对象

    ¥options Object

    • title 字符串(可选)

      ¥title string (optional)

    • defaultPath 字符串(可选)

      ¥defaultPath string (optional)

    • buttonLabel 字符串(可选) - 确认按钮的自定义标签,留空时将使用默认标签。

      ¥buttonLabel string (optional) - Custom label for the confirmation button, when left empty the default label will be used.

    • filters FileFilter[](可选)

      ¥filters FileFilter[] (optional)

    • properties 字符串[](可选) - 包含对话框应使用哪些功能。支持以下值:

      ¥properties string[] (optional) - Contains which features the dialog should use. The following values are supported:

      • openFile - 允许选择文件。

        ¥openFile - Allow files to be selected.

      • openDirectory - 允许选择目录。

        ¥openDirectory - Allow directories to be selected.

      • multiSelections - 允许选择多个路径。

        ¥multiSelections - Allow multiple paths to be selected.

      • showHiddenFiles - 在对话框中显示隐藏文件。

        ¥showHiddenFiles - Show hidden files in dialog.

      • createDirectory macOS - 允许从对话框创建新目录。

        ¥createDirectory macOS - Allow creating new directories from dialog.

      • promptToCreate Windows - 如果对话框中输入的文件路径不存在,则提示创建。这实际上并不在路径处创建文件,而是允许返回应由应用创建的不存在的路径。

        ¥promptToCreate Windows - Prompt for creation if the file path entered in the dialog does not exist. This does not actually create the file at the path but allows non-existent paths to be returned that should be created by the application.

      • noResolveAliases macOS - 禁用自动别名(符号链接)路径解析。选定的别名现在将返回别名路径而不是其目标路径。

        ¥noResolveAliases macOS - Disable the automatic alias (symlink) path resolution. Selected aliases will now return the alias path instead of their target path.

      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。

        ¥treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

      • dontAddToRecent Windows - 不要将正在打开的项目添加到最近的文档列表中。

        ¥dontAddToRecent Windows - Do not add the item being opened to the recent documents list.

    • message 字符串(可选)macOS - 显示在输入框上方的消息。

      ¥message string (optional) macOS - Message to display above input boxes.

    • securityScopedBookmarks 布尔值(可选) macOS mas - 为 Mac App Store 打包时创建 安全范围书签

      ¥securityScopedBookmarks boolean (optional) macOS MAS - Create security scoped bookmarks when packaged for the 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).

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

¥Note: On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set properties to ['openFile', 'openDirectory'] on these platforms, a directory selector will be shown.

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

dialog.showOpenDialog([window, ]options)

  • window BaseWindow(可选)

    ¥window BaseWindow (optional)

  • options 对象

    ¥options Object

    • title 字符串(可选)

      ¥title string (optional)

    • defaultPath 字符串(可选)

      ¥defaultPath string (optional)

    • buttonLabel 字符串(可选) - 确认按钮的自定义标签,留空时将使用默认标签。

      ¥buttonLabel string (optional) - Custom label for the confirmation button, when left empty the default label will be used.

    • filters FileFilter[](可选)

      ¥filters FileFilter[] (optional)

    • properties 字符串[](可选) - 包含对话框应使用哪些功能。支持以下值:

      ¥properties string[] (optional) - Contains which features the dialog should use. The following values are supported:

      • openFile - 允许选择文件。

        ¥openFile - Allow files to be selected.

      • openDirectory - 允许选择目录。

        ¥openDirectory - Allow directories to be selected.

      • multiSelections - 允许选择多个路径。

        ¥multiSelections - Allow multiple paths to be selected.

      • showHiddenFiles - 在对话框中显示隐藏文件。

        ¥showHiddenFiles - Show hidden files in dialog.

      • createDirectory macOS - 允许从对话框创建新目录。

        ¥createDirectory macOS - Allow creating new directories from dialog.

      • promptToCreate Windows - 如果对话框中输入的文件路径不存在,则提示创建。这实际上并不在路径处创建文件,而是允许返回应由应用创建的不存在的路径。

        ¥promptToCreate Windows - Prompt for creation if the file path entered in the dialog does not exist. This does not actually create the file at the path but allows non-existent paths to be returned that should be created by the application.

      • noResolveAliases macOS - 禁用自动别名(符号链接)路径解析。选定的别名现在将返回别名路径而不是其目标路径。

        ¥noResolveAliases macOS - Disable the automatic alias (symlink) path resolution. Selected aliases will now return the alias path instead of their target path.

      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。

        ¥treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

      • dontAddToRecent Windows - 不要将正在打开的项目添加到最近的文档列表中。

        ¥dontAddToRecent Windows - Do not add the item being opened to the recent documents list.

    • message 字符串(可选)macOS - 显示在输入框上方的消息。

      ¥message string (optional) macOS - Message to display above input boxes.

    • securityScopedBookmarks 布尔值(可选) macOS mas - 为 Mac App Store 打包时创建 安全范围书签

      ¥securityScopedBookmarks boolean (optional) macOS MAS - Create security scoped bookmarks when packaged for the Mac App Store.

返回 Promise<Object> - 使用包含以下内容的对象进行解析:

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

  • canceled 布尔值 - 对话框是否被取消。

    ¥canceled boolean - whether or not the dialog was canceled.

  • filePaths 字符串[] - 用户选择的文件路径数组。如果取消对话框,这将是一个空数组。

    ¥filePaths string[] - An array of file paths chosen by the user. If the dialog is cancelled this will be an empty array.

  • bookmarks string[] (可选) macOS mas - 与 Base64 编码字符串的 filePaths 数组匹配的数组,其中包含安全范围的书签数据。必须启用 securityScopedBookmarks 才能填充此内容。(有关返回值,请参阅 此处的表格。)

    ¥bookmarks string[] (optional) macOS MAS - An array matching the filePaths array of base64 encoded strings which contains security scoped bookmark data. securityScopedBookmarks must be enabled for this to be populated. (For return values, see table here.)

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).

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

¥Note: On Windows and Linux an open dialog can not be both a file selector and a directory selector, so if you set properties to ['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)
})

dialog.showSaveDialogSync([window, ]options)

  • window BaseWindow(可选)

    ¥window BaseWindow (optional)

  • options 对象

    ¥options Object

    • title 字符串(可选) - 对话框标题。在某些 Linux 桌面环境下无法显示。

      ¥title string (optional) - The dialog title. Cannot be displayed on some Linux desktop environments.

    • defaultPath 字符串(可选) - 默认使用的绝对目录路径、绝对文件路径或文件名。

      ¥defaultPath string (optional) - Absolute directory path, absolute file path, or file name to use by default.

    • buttonLabel 字符串(可选) - 确认按钮的自定义标签,留空时将使用默认标签。

      ¥buttonLabel string (optional) - Custom label for the confirmation button, when left empty the default label will be used.

    • filters FileFilter[](可选)

      ¥filters FileFilter[] (optional)

    • message 字符串(可选)macOS - 显示上方文本字段的消息。

      ¥message string (optional) macOS - Message to display above text fields.

    • nameFieldLabel 字符串(可选)macOS - 文件名文本字段前面显示的文本的自定义标签。

      ¥nameFieldLabel string (optional) macOS - Custom label for the text displayed in front of the filename text field.

    • showsTagField 布尔值(可选)macOS - 显示标签输入框,默认为 true

      ¥showsTagField boolean (optional) macOS - Show the tags input box, defaults to true.

    • properties 字符串[](可选)

      ¥properties string[] (optional)

      • showHiddenFiles - 在对话框中显示隐藏文件。

        ¥showHiddenFiles - Show hidden files in dialog.

      • createDirectory macOS - 允许从对话框创建新目录。

        ¥createDirectory macOS - Allow creating new directories from dialog.

      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。

        ¥treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

      • showOverwriteConfirmation Linux - 设置如果用户键入已存在的文件名,是否向用户显示确认对话框。

        ¥showOverwriteConfirmation Linux - Sets whether the user will be presented a confirmation dialog if the user types a file name that already exists.

      • dontAddToRecent Windows - 不要将正在保存的项目添加到最近的文档列表中。

        ¥dontAddToRecent Windows - Do not add the item being saved to the recent documents list.

    • securityScopedBookmarks 布尔值(可选) macOS mas - 为 Mac App Store 打包时创建 安全范围书签。如果启用此选项并且该文件尚不存在,则会在所选路径中创建一个空白文件。

      ¥securityScopedBookmarks boolean (optional) macOS MAS - Create a security scoped bookmark when packaged for the Mac App Store. If this option is enabled and the file doesn't already exist a blank file will be created at the chosen path.

返回 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(可选)

    ¥window BaseWindow (optional)

  • options 对象

    ¥options Object

    • title 字符串(可选) - 对话框标题。在某些 Linux 桌面环境下无法显示。

      ¥title string (optional) - The dialog title. Cannot be displayed on some Linux desktop environments.

    • defaultPath 字符串(可选) - 默认使用的绝对目录路径、绝对文件路径或文件名。

      ¥defaultPath string (optional) - Absolute directory path, absolute file path, or file name to use by default.

    • buttonLabel 字符串(可选) - 确认按钮的自定义标签,留空时将使用默认标签。

      ¥buttonLabel string (optional) - Custom label for the confirmation button, when left empty the default label will be used.

    • filters FileFilter[](可选)

      ¥filters FileFilter[] (optional)

    • message 字符串(可选)macOS - 显示上方文本字段的消息。

      ¥message string (optional) macOS - Message to display above text fields.

    • nameFieldLabel 字符串(可选)macOS - 文件名文本字段前面显示的文本的自定义标签。

      ¥nameFieldLabel string (optional) macOS - Custom label for the text displayed in front of the filename text field.

    • showsTagField 布尔值(可选)macOS - 显示标签输入框,默认为 true

      ¥showsTagField boolean (optional) macOS - Show the tags input box, defaults to true.

    • properties 字符串[](可选)

      ¥properties string[] (optional)

      • showHiddenFiles - 在对话框中显示隐藏文件。

        ¥showHiddenFiles - Show hidden files in dialog.

      • createDirectory macOS - 允许从对话框创建新目录。

        ¥createDirectory macOS - Allow creating new directories from dialog.

      • treatPackageAsDirectory macOS - 将包(例如 .app 文件夹)视为目录而不是文件。

        ¥treatPackageAsDirectory macOS - Treat packages, such as .app folders, as a directory instead of a file.

      • showOverwriteConfirmation Linux - 设置如果用户键入已存在的文件名,是否向用户显示确认对话框。

        ¥showOverwriteConfirmation Linux - Sets whether the user will be presented a confirmation dialog if the user types a file name that already exists.

      • dontAddToRecent Windows - 不要将正在保存的项目添加到最近的文档列表中。

        ¥dontAddToRecent Windows - Do not add the item being saved to the recent documents list.

    • securityScopedBookmarks 布尔值(可选) macOS mas - 为 Mac App Store 打包时创建 安全范围书签。如果启用此选项并且该文件尚不存在,则会在所选路径中创建一个空白文件。

      ¥securityScopedBookmarks boolean (optional) macOS MAS - Create a security scoped bookmark when packaged for the Mac App Store. If this option is enabled and the file doesn't already exist a blank file will be created at the chosen path.

返回 Promise<Object> - 使用包含以下内容的对象进行解析:

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

  • canceled 布尔值 - 对话框是否被取消。

    ¥canceled boolean - whether or not the dialog was canceled.

  • filePath 字符串 - 如果对话框被取消,这将是一个空字符串。

    ¥filePath string - If the dialog is canceled, this will be an empty string.

  • bookmark 字符串(可选)macOS mas - Base64 编码字符串,其中包含已保存文件的安全范围书签数据。必须启用 securityScopedBookmarks 才能出现此情况。(有关返回值,请参阅 此处的表格。)

    ¥bookmark string (optional) macOS MAS - Base64 encoded string which contains the security scoped bookmark data for the saved file. securityScopedBookmarks must be enabled for this to be present. (For return values, see table here.)

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.

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

¥Note: On macOS, using the asynchronous version is recommended to avoid issues when expanding and collapsing the dialog.

dialog.showMessageBoxSync([wndow, ]options)

  • window BaseWindow(可选)

    ¥window BaseWindow (optional)

  • options 对象

    ¥options Object

    • message 字符串 - 消息框的内容。

      ¥message string - Content of the message box.

    • type 字符串(可选) - 可以是 noneinfoerrorquestionwarning。在 Windows 上,question 显示与 info 相同的图标,除非你使用 icon 选项设置图标。在 macOS 上,warningerror 显示相同的警告图标。

      ¥type string (optional) - Can be none, info, error, question or warning. On Windows, question displays the same icon as info, unless you set an icon using the icon option. On macOS, both warning and error display the same warning icon.

    • buttons 字符串[](可选) - 按钮的文本数组。在 Windows 上,空数组将导致一个标记为 "OK" 的按钮。

      ¥buttons string[] (optional) - Array of texts for buttons. On Windows, an empty array will result in one button labeled "OK".

    • defaultId 整数(可选) - 按钮数组中按钮的索引,消息框打开时默认选择该按钮。

      ¥defaultId Integer (optional) - Index of the button in the buttons array which will be selected by default when the message box opens.

    • title 字符串(可选) - 消息框的标题,有些平台不会显示。

      ¥title string (optional) - Title of the message box, some platforms will not show it.

    • detail 字符串(可选) - 消息的额外信息。

      ¥detail string (optional) - Extra information of the message.

    • iconNativeImage | 字符串)(可选)

      ¥icon (NativeImage | string) (optional)

    • textWidth 整数(可选)macOS - 消息框中文本的自定义宽度。

      ¥textWidth Integer (optional) macOS - Custom width of the text in the message box.

    • cancelId 整数(可选) - 用于通过 Esc 键取消对话框的按钮索引。默认情况下,它被分配给第一个以 "cancel" 或 "no" 作为标签的按钮。如果不存在这样的标记按钮并且未设置此选项,则 0 将用作返回值。

      ¥cancelId Integer (optional) - The index of the button to be used to cancel the dialog, via the Esc key. By default this is assigned to the first button with "cancel" or "no" as the label. If no such labeled buttons exist and this option is not set, 0 will be used as the return value.

    • noLink 布尔值(可选) - 在 Windows 上,Electron 将尝试找出 buttons 中的哪一个是常用按钮(如 "取消" 或 "是的"),并在对话框中将其他按钮显示为命令链接。这可以使对话框以现代 Windows 应用的风格显示。如果你不喜欢这种行为,可以将 noLink 设置为 true

      ¥noLink boolean (optional) - On Windows Electron will try to figure out which one of the buttons are common buttons (like "Cancel" or "Yes"), and show the others as command links in the dialog. This can make the dialog appear in the style of modern Windows apps. If you don't like this behavior, you can set noLink to true.

    • normalizeAccessKeys 布尔值(可选) - 跨平台标准化键盘访问键。默认为 false。启用此功能假定按钮标签中使用 & 来放置键盘快捷键访问键,并且标签将被转换,以便它们在每个平台上正常工作,& 字符在 macOS 上被删除,在 Linux 上转换为 _,在 Windows 上保持不变 。例如,按钮标签 Vie&w 将在 Linux 上转换为 Vie_w,在 macOS 上转换为 View,并且可以在 Windows 和 Linux 上通过 Alt-W 进行选择。

      ¥normalizeAccessKeys boolean (optional) - Normalize the keyboard access keys across platforms. Default is false. Enabling this assumes & is used in the button labels for the placement of the keyboard shortcut access key and labels will be converted so they work correctly on each platform, & characters are removed on macOS, converted to _ on Linux, and left untouched on Windows. For example, a button label of Vie&w will be converted to Vie_w on Linux and View on macOS and can be selected via Alt-W on Windows and Linux.

返回 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(可选)

    ¥window BaseWindow (optional)

  • options 对象

    ¥options Object

    • message 字符串 - 消息框的内容。

      ¥message string - Content of the message box.

    • type 字符串(可选) - 可以是 noneinfoerrorquestionwarning。在 Windows 上,question 显示与 info 相同的图标,除非你使用 icon 选项设置图标。在 macOS 上,warningerror 显示相同的警告图标。

      ¥type string (optional) - Can be none, info, error, question or warning. On Windows, question displays the same icon as info, unless you set an icon using the icon option. On macOS, both warning and error display the same warning icon.

    • buttons 字符串[](可选) - 按钮的文本数组。在 Windows 上,空数组将导致一个标记为 "OK" 的按钮。

      ¥buttons string[] (optional) - Array of texts for buttons. On Windows, an empty array will result in one button labeled "OK".

    • defaultId 整数(可选) - 按钮数组中按钮的索引,消息框打开时默认选择该按钮。

      ¥defaultId Integer (optional) - Index of the button in the buttons array which will be selected by default when the message box opens.

    • signal 中止信号(可选) - 传递 AbortSignal 的实例以选择性地关闭消息框,消息框的行为就像被用户取消一样。在 macOS 上,signal 不适用于没有父窗口的消息框,因为这些消息框由于平台限制而同步运行。

      ¥signal AbortSignal (optional) - Pass an instance of AbortSignal to optionally close the message box, the message box will behave as if it was cancelled by the user. On macOS, signal does not work with message boxes that do not have a parent window, since those message boxes run synchronously due to platform limitations.

    • title 字符串(可选) - 消息框的标题,有些平台不会显示。

      ¥title string (optional) - Title of the message box, some platforms will not show it.

    • detail 字符串(可选) - 消息的额外信息。

      ¥detail string (optional) - Extra information of the message.

    • checkboxLabel 字符串(可选) - 如果提供,消息框将包含一个带有给定标签的复选框。

      ¥checkboxLabel string (optional) - If provided, the message box will include a checkbox with the given label.

    • checkboxChecked 布尔值(可选) - 复选框的初始选中状态。默认为 false

      ¥checkboxChecked boolean (optional) - Initial checked state of the checkbox. false by default.

    • iconNativeImage | 字符串)(可选)

      ¥icon (NativeImage | string) (optional)

    • textWidth 整数(可选)macOS - 消息框中文本的自定义宽度。

      ¥textWidth Integer (optional) macOS - Custom width of the text in the message box.

    • cancelId 整数(可选) - 用于通过 Esc 键取消对话框的按钮索引。默认情况下,它被分配给第一个以 "cancel" 或 "no" 作为标签的按钮。如果不存在这样的标记按钮并且未设置此选项,则 0 将用作返回值。

      ¥cancelId Integer (optional) - The index of the button to be used to cancel the dialog, via the Esc key. By default this is assigned to the first button with "cancel" or "no" as the label. If no such labeled buttons exist and this option is not set, 0 will be used as the return value.

    • noLink 布尔值(可选) - 在 Windows 上,Electron 将尝试找出 buttons 中的哪一个是常用按钮(如 "取消" 或 "是的"),并在对话框中将其他按钮显示为命令链接。这可以使对话框以现代 Windows 应用的风格显示。如果你不喜欢这种行为,可以将 noLink 设置为 true

      ¥noLink boolean (optional) - On Windows Electron will try to figure out which one of the buttons are common buttons (like "Cancel" or "Yes"), and show the others as command links in the dialog. This can make the dialog appear in the style of modern Windows apps. If you don't like this behavior, you can set noLink to true.

    • normalizeAccessKeys 布尔值(可选) - 跨平台标准化键盘访问键。默认为 false。启用此功能假定按钮标签中使用 & 来放置键盘快捷键访问键,并且标签将被转换,以便它们在每个平台上正常工作,& 字符在 macOS 上被删除,在 Linux 上转换为 _,在 Windows 上保持不变 。例如,按钮标签 Vie&w 将在 Linux 上转换为 Vie_w,在 macOS 上转换为 View,并且可以在 Windows 和 Linux 上通过 Alt-W 进行选择。

      ¥normalizeAccessKeys boolean (optional) - Normalize the keyboard access keys across platforms. Default is false. Enabling this assumes & is used in the button labels for the placement of the keyboard shortcut access key and labels will be converted so they work correctly on each platform, & characters are removed on macOS, converted to _ on Linux, and left untouched on Windows. For example, a button label of Vie&w will be converted to Vie_w on Linux and View on macOS and can be selected via Alt-W on Windows and Linux.

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

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

  • response 数字 - 单击的按钮的索引。

    ¥response number - The index of the clicked button.

  • checkboxChecked 布尔值 - 如果设置了 checkboxLabel,则复选框的选中状态。否则 false

    ¥checkboxChecked boolean - The checked state of the checkbox if checkboxLabel was set. Otherwise 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 字符串 - 错误框中显示的标题。

    ¥title string - The title to display in the error box.

  • content 字符串 - 错误框中显示的文本内容。

    ¥content string - The text content to display in the error box.

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

¥Displays a modal dialog that shows an error message.

该 API 可以在 app 模块发出 ready 事件之前安全地调用,通常用于在启动初期报告错误。如果在 Linux 上的应用 readyevent 之前调用,该消息将发送到 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 readyevent on Linux, the message will be emitted to stderr, and no GUI dialog will appear.

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

  • window BaseWindow(可选)

    ¥window BaseWindow (optional)

  • options 对象

    ¥options Object

    • certificate 证书 - 要信任/导入的证书。

      ¥certificate Certificate - The certificate to trust/import.

    • message 字符串 - 显示给用户的消息。

      ¥message string - The message to display to the user.

返回 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 参数,因为操作系统提供了自己的确认对话框。

    ¥The message argument is not used, as the OS provides its own confirmation dialog.

  • window 参数被忽略,因为不可能使此确认对话框成为模态。

    ¥The window argument is ignored since it is not possible to make this confirmation dialog modal.

书签数组

¥Bookmarks array

showOpenDialogshowOpenDialogSyncshowSaveDialogshowSaveDialogSync 将返回 bookmarks 数组。

¥showOpenDialog, showOpenDialogSync, showSaveDialog, and showSaveDialogSync will return a bookmarks array.

构建类型securityScopedBookmarks 布尔值返回类型返回值
macOS 系统真的成功['LONGBOOKMARKSTRING']
macOS 系统真的错误[''](空字符串数组)
macOS 系统错误的不适用[](空数组)
非马斯any不适用[](空数组)

床单

¥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.