跳到主要内容

shell

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

进程:渲染器(仅非沙盒模式)

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

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

const { shell } = require('electron')

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

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

方法

shell 模块有以下方法

shell.showItemInFolder(fullPath)

  • fullPath 字符串

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

shell.openPath(path)

  • path 字符串

返回 Promise<string> - 如果发生失败,则返回包含与失败对应的错误消息的字符串,否则返回 ""。

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

shell.openExternal(url[, options])

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

返回 Promise<void>

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

shell.trashItem(path)

  • path 字符串 - 要移动到垃圾箱的项目的路径。

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

此操作将路径移动到操作系统特定的垃圾箱位置 (macOS 上为 Trash,Windows 上为 Recycle Bin,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 的快捷方式链接。

发生任何错误时将抛出异常。