Skip to main content

shell

使用默认应用管理文件和 URL。

进程:主进程渲染进程(仅非沙盒)

🌐 Process: Main, Renderer (non-sandboxed only)

shell 模块提供与桌面集成相关的功能。

🌐 The shell module provides functions related to desktop integration.

在用户的默认浏览器中打开 URL 的示例:

🌐 An example of opening a URL in the user's default browser:

const { shell } = require('electron')

shell.openExternal('https://github.com')
warning

虽然 shell 模块可以在渲染进程中使用,但它在沙箱渲染器中无法运行。

方法

🌐 Methods

shell 模块具有以下方法:

🌐 The shell module has the following methods:

shell.showItemInFolder(fullPath)

  • fullPath 字符串

在文件管理器中显示指定文件。如果可能,选择该文件。

🌐 Show the given file in a file manager. If possible, select the file.

shell.openPath(path)

History
Version(s)Changes
None
API ADDED
  • path 字符串

返回 Promise<string> - 如果发生错误,则解析为包含对应错误信息的字符串,否则返回 ""。

🌐 Returns Promise<string> - Resolves with a string containing the error message corresponding to the failure if a failure occurred, otherwise "".

以桌面的默认方式打开给定文件。

🌐 Open the given file in the desktop's default manner.

shell.openExternal(url[, options])

History
Version(s)Changes
None

Added activate option.

None

Added workingDirectory option.

None

Added logUsage option.

  • url 字符串 - 在 Windows 上最多 2081 个字符。
  • options 对象(可选)
    • activate 布尔值(可选)macOS - true 用于将已打开的应用置于前台。默认值为 true
    • workingDirectory 字符串(可选)Windows - 工作目录。
    • logUsage 布尔值(可选) Windows - 表示用户发起的启动,它可以跟踪常用程序和其他行为。默认值为 false

返回 Promise<void>

🌐 Returns Promise<void>

以桌面默认方式打开给定的外部协议 URL。(例如,在用户的默认邮件客户端中打开 mailto: URL)。

🌐 Open the given external protocol URL in the desktop's default manner. (For example, mailto: URLs in the user's default mail agent).

shell.trashItem(path)

History
Version(s)Changes
None
API ADDED
  • path 字符串 - 要移动到回收站的项目路径。

返回 Promise<void> - 当操作完成时解析。如果在删除请求的项目时发生错误,则会被拒绝。

🌐 Returns Promise<void> - Resolves when the operation has been completed. Rejects if there was an error while deleting the requested item.

这会将路径移动到操作系统特定的垃圾箱位置(macOS 上的“废纸篓”,Windows 上的“回收站”,以及 Linux 上特定桌面环境的垃圾箱位置)。

🌐 This moves a path to the OS-specific trash location (Trash on macOS, Recycle Bin on Windows, and a desktop-environment-specific location on Linux).

路径必须使用平台的默认路径分隔符(Windows 上为反斜杠)。使用 node:path 模块中的 path.resolve() 来确保在所有文件系统上的正确处理。

🌐 The path must use the default path separator for the platform (backslash on Windows). Use path.resolve() from the node:path module to ensure correct handling on all filesystems.

shell.beep()

播放嘟嘟声。

🌐 Play the beep sound.

shell.writeShortcutLink(shortcutPath[, operation], options) Windows

  • shortcutPath 字符串
  • operation 字符串(可选)- 默认值是 create,可以是以下之一:
    • create - 创建新的快捷方式,必要时进行覆盖。
    • update - 仅更新现有快捷方式的指定属性。
    • replace - 覆盖现有的快捷方式,如果快捷方式不存在则失败。
  • options 快捷方式详情

返回 boolean - 快捷方式是否创建成功。

🌐 Returns boolean - Whether the shortcut was created successfully.

shortcutPath 创建或更新快捷方式链接。

🌐 Creates or updates a shortcut link at shortcutPath.

shell.readShortcutLink(shortcutPath) Windows

  • shortcutPath 字符串

返回 ShortcutDetails

🌐 Returns ShortcutDetails

解析位于 shortcutPath 的快捷方式链接。

🌐 Resolves the shortcut link at shortcutPath.

当任何错误发生时都会抛出异常。

🌐 An exception will be thrown when any error happens.