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
模块可以在渲染器进程中使用,但它无法在沙盒渲染器中运行。
¥Note: While the shell
module 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
字符串¥
fullPath
string
在文件管理器中显示给定的文件。如果可能,请选择该文件。
¥Show the given file in a file manager. If possible, select the file.
shell.openPath(path)
-
path
字符串¥
path
string
返回 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 个字符。¥
url
string - 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
字符串 - 要移至垃圾箱的项目的路径。¥
path
string - 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
字符串¥
shortcutPath
string -
operation
字符串(可选) - 默认为create
,可以是以下之一:¥
operation
string (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
字符串¥
shortcutPath
string
¥Returns ShortcutDetails
解析 shortcutPath
处的快捷方式链接。
¥Resolves the shortcut link at shortcutPath
.
当任何错误发生时都会抛出异常。
¥An exception will be thrown when any error happens.