shell
使用默认应用管理文件和 URL。
¥Manage files and URLs using their default applications.
¥Process: Main, Renderer (non-sandboxed only)
shell 模块提供与桌面集成相关的功能。
¥The shell module provides functions related to desktop integration.
在用户的默认浏览器中打开 URL 的示例:
¥An example of opening a URL in the user's default browser:
const { shell } = require('electron')
shell.openExternal('https://github.com')
[!警告] 虽然
shell模块可以在渲染器进程中使用,但它无法在沙盒渲染器中运行。¥[!WARNING] While the
shellmodule can be used in the renderer process, it will not function in a sandboxed renderer.
方法
¥Methods
shell 模块有以下方法:
¥The shell module has the following methods:
shell.showItemInFolder(fullPath)
-
fullPath字符串¥
fullPathstring
在文件管理器中显示给定的文件。如果可能,请选择该文件。
¥Show the given file in a file manager. If possible, select the file.
shell.openPath(path)
-
path字符串¥
pathstring
返回 Promise<string> - 如果发生故障,则解析为包含与故障对应的错误消息的字符串,否则为 ""。
¥Returns Promise<string> - Resolves with a string containing the error message corresponding to the failure if a failure occurred, otherwise "".
以桌面的默认方式打开给定文件。
¥Open the given file in the desktop's default manner.
shell.openExternal(url[, options])
-
url字符串 - Windows 上最多 2081 个字符。¥
urlstring - Max 2081 characters on Windows.
返回 Promise<void>
¥Returns Promise<void>
以桌面默认方式打开给定的外部协议 URL。(例如,mailto:用户默认邮件代理中的 URL)。
¥Open the given external protocol URL in the desktop's default manner. (For example, mailto: URLs in the user's default mail agent).
shell.trashItem(path)
-
path字符串 - 要移至垃圾箱的项目的路径。¥
pathstring - path to the item to be moved to the trash.
返回 Promise<void> - 当操作完成时解决。如果删除请求的项目时出现错误,则拒绝。
¥Returns Promise<void> - Resolves when the operation has been completed.
Rejects if there was an error while deleting the requested item.
这会将路径移动到特定于操作系统的垃圾位置(macOS 上的垃圾箱、Windows 上的回收站以及 Linux 上的桌面环境特定位置)。
¥This moves a path to the OS-specific trash location (Trash on macOS, Recycle Bin on Windows, and a desktop-environment-specific location on Linux).
shell.beep()
播放嘟嘟声。
¥Play the beep sound.
shell.writeShortcutLink(shortcutPath[, operation], options) Windows
-
shortcutPath字符串¥
shortcutPathstring -
operation字符串(可选) - 默认为create,可以是以下之一:¥
operationstring (optional) - Default iscreate, can be one of following:-
create- 创建新的快捷方式,必要时进行覆盖。¥
create- Creates a new shortcut, overwriting if necessary. -
update- 仅更新现有快捷方式的指定属性。¥
update- Updates specified properties only on an existing shortcut. -
replace- 覆盖现有快捷方式,如果快捷方式不存在则失败。¥
replace- Overwrites an existing shortcut, fails if the shortcut doesn't exist.
-
返回 boolean - 快捷方式是否创建成功。
¥Returns boolean - Whether the shortcut was created successfully.
在 shortcutPath 创建或更新快捷链接。
¥Creates or updates a shortcut link at shortcutPath.
shell.readShortcutLink(shortcutPath) Windows
-
shortcutPath字符串¥
shortcutPathstring
¥Returns ShortcutDetails
解析 shortcutPath 处的快捷方式链接。
¥Resolves the shortcut link at shortcutPath.
当任何错误发生时都会抛出异常。
¥An exception will be thrown when any error happens.