跳转到主要内容

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 的快捷方式链接。

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