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 个字符。
返回 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
stringoperation
string (可选) - 默认为create
,可以是以下之一create
- 创建新的快捷方式,如果需要则覆盖。update
- 仅更新现有快捷方式的指定属性。replace
- 覆盖现有快捷方式,如果快捷方式不存在则失败。
返回 boolean
- 是否成功创建了快捷方式。
在 shortcutPath
创建或更新快捷方式链接。
shell.readShortcutLink(shortcutPath)
Windows
shortcutPath
string
解析 shortcutPath
的快捷方式链接。
发生任何错误时将抛出异常。