跳转到主要内容

shell

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

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

shell 模块提供与桌面集成相关的函数。

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

const { shell } = require('electron')

shell.openExternal('https://github.com')
警告

虽然 shell 模块可以在渲染进程中使用,但它在沙盒化的渲染进程中将无法工作。

方法

shell 模块具有以下方法

shell.showItemInFolder(fullPath)

  • fullPath 字符串

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

shell.openPath(path)

  • path string

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

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

shell.openExternal(url[, options])

  • url 字符串 - Windows 上最大长度为 2081 个字符。
  • options Object (可选)
    • activate 布尔值 (可选) macOS - true 将打开的应用程序带到前台。默认值为 true
    • workingDirectory 字符串 (可选) Windows - 工作目录。
    • logUsage 布尔值 (可选) Windows - 指示用户发起的启动,启用对常用程序和其他行为的跟踪。默认值为 false

返回 Promise<void>

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

shell.trashItem(path)

  • path 字符串 - 要移动到回收站的项目的路径。

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

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

shell.beep()

播放提示音。

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

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

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

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

shell.readShortcutLink(shortcutPath) Windows

  • shortcutPath 字符串

返回 ShortcutDetails

解析 shortcutPath 处的快捷方式链接。

如果发生任何错误,将抛出异常。