跳转到主要内容

shell

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

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

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

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

const { shell } = require('electron')

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

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

方法

shell 模块具有以下方法

shell.showItemInFolder(fullPath)

  • fullPath string

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

shell.openPath(path)

  • path string

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

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

shell.openExternal(url[, options])

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

返回 Promise<void>

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

shell.trashItem(path)

  • path string - 要移至回收站的项目路径。

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

此操作会将一个路径移至操作系统特定的回收站位置(macOS 上的 Trash,Windows 上的 Recycle Bin,Linux 上的桌面环境特定位置)。

shell.beep()

播放哔声。

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

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

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

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

shell.readShortcutLink(shortcutPath) Windows

  • shortcutPath string

返回 ShortcutDetails

解析 shortcutPath 的快捷方式链接。

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