app·续3

On macOS, it shows on the dock icon. On Linux, it only works for Unity launcher.

注意: Unity 启动器依赖于 . desktop 文件, 获取更多信息, 请阅读 桌面环境集成 。

过时的

app.getBadgeCount() Linux macOS

Returns Integer - 获取计数器提醒(badge) 中显示的当前值

过时的

app.isUnityRunning() Linux

Returns Boolean - 当前桌面环境是否为 Unity 启动器

app.getLoginItemSettings([options]) macOS Windows

  • options Object (optional)

  • path String (optional) Windows - The executable path to compare against. Defaults to process.execPath.

  • args String[] (optional) Windows - The command-line arguments to compare against. Defaults to an empty array.

如果你为 app. setLoginItemSettings 提供path 和 args 选项,那么你需要在这里为 openAtLogin 设置相同的参数已确保正确的设置。

返回 Object:

  • openAtLogin Boolean - true 如果应用程序设置为在登录时打开, 则为 <0>true

  • openAsHidden Boolean macOS - true 表示应用在登录时以隐藏的方式启动。 该配置在 MAS 构建 时不可用。

  • wasOpenedAtLogin Boolean macOS - true 表示应用在自动登录后已经启动。 该配置在 MAS 构建 时不可用。

  • wasOpenedAsHiddenBoolean macOS - 如果应用在登录时已经隐藏启动, 则为 true。 这表示应用程序在启动时不应打开任何窗口。 该配置在 MAS 构建 时不可用。

  • restoreState Boolean macOS - true 表示应用作为登录启动项并且需要恢复之前的会话状态。 这表示程序应该还原上次关闭时打开的窗口。 该配置在 MAS 构建 时不可用。

app.setLoginItemSettings(settings) macOS Windows

  • settings Object

  • openAtLogin Boolean (optional) - true to open the app at login, false to remove the app as a login item. 默认值为 false.

  • openAsHidden Boolean (可选) macOS - true 表示以隐藏的方式启动应用。 默认为false。 用户可以从系统首选项中编辑此设置, 以便在打开应用程序时检查 app.getLoginItemSettings().wasOpenedAsHidden 以了解当前值。 该配置在 MAS 构建 时不可用。

  • path String (optional) Windows - The executable to launch at login. Defaults to process.execPath.

  • args String[] (optional) Windows - The command-line arguments to pass to the executable. Defaults to an empty array. Take care to wrap paths in quotes.

设置应用程序的登录项设置。

如果需要在使用Squirrel的 Windows 上使用 Electron 的 autoUpdater ,你需要将启动路径设置为 Update.exe,并传递指定应用程序名称的参数。 例如:

const appFolder = path.dirname(process.execPath)const updateExe = path.resolve(appFolder, '..', 'Update.exe')const exeName = path.basename(process.execPath)app.setLoginItemSettings({  openAtLogin: true,  path: updateExe,  args: [    '--processStart', `"${exeName}"`,    '--process-start-args', `"--hidden"`  ]})

app.isAccessibilitySupportEnabled() macOS Windows

Returns Boolean - 如果开启了Chrome的辅助功能, 则返回 true,其他情况返false。 如果使用了辅助技术(例如屏幕阅读),该 API 将返回 `true

过时的

app.setAccessibilitySupportEnabled(enabled)` macOS Windows

  • enable 逻辑值 - 启用或禁用访问权限树视图。

手动启用 Chrome 的辅助功能的支持, 允许在应用程序中设置是否开启辅助功能。 在Chromium’s accessibility docs查看更多的细节 默认为禁用

此 API 必须在 ready 事件触发后调用

注意: 渲染进程树会明显的影响应用的性能。默认情况下不应该启用。

过时的

app.showAboutPanel()

Show the app’s about panel options. These options can be overridden with app.setAboutPanelOptions(options).

app.setAboutPanelOptions(options)

  • options Object

  • applicationName String (可选) - 应用程序的名字

  • applicationVersion String (可选) - 应用程序版本

  • copyright String (可选) - 版权信息

  • version String (optional) macOS - The app’s build version number.

  • credits String (optional) macOS Windows - Credit information.

  • authors String[] (optional) Linux - List of app authors.

  • website String (optional) Linux - The app’s website.

  • iconPath String (optional) Linux Windows - Path to the app’s icon. On Linux, will be shown as 64x64 pixels while retaining aspect ratio.

设置 “关于” 面板选项。 This will override the values defined in the app’s .plist file on MacOS. 更多详细信息, 请查阅 Apple 文档 。 在 Linux 上,没有默认值,所以必须设置值才能显示。

If you do not set credits but still wish to surface them in your app, AppKit will look for a file named “Credits.html”, “Credits.rtf”, and “Credits.rtfd”, in that order, in the bundle returned by the NSBundle class method main. The first file found is used, and if none is found, the info area is left blank. See Apple documentation for more information.

app.isEmojiPanelSupported()

Returns Boolean - whether or not the current OS version allows for native emoji pickers.

app.showEmojiPanel() macOS Windows

Show the platform’s native emoji picker.

app.startAccessingSecurityScopedResource(bookmarkData) mas

  • bookmarkData String - base64 编码的安全作用域的书签数据(bookmark data) ,通过 dialog.showOpenDialog 或者 dialog.showSaveDialog 方法获取。

返回 Function - 该函数 必须 在你完成访问安全作用域文件后调用一次。 如果你忘记停止访问书签,内核资源将会泄漏,并且你的应用将失去完全到达沙盒之外的能力,直到应用重启。

//开始读取文件const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(data)// You can now access the file outside of the sandbox 🎉// Remember to stop accessing the file once you've finished with it.stopAccessingSecurityScopedResource()

开始访问安全范围内的资源。 通过这个方法,Electron 应用被打包为可到达Mac App Store沙箱之外访问用户选择的文件。 关于系统工作原理,请查阅Apple’s documentation

app.enableSandbox() 实验功能

在应用程序上启用完全沙盒模式。

这个方法只能在应用程序准备就绪(ready)之前调用。

app.isInApplicationsFolder() macOS

Returns Boolean - Whether the application is currently running from the systems Application folder. Use in combination with app.moveToApplicationsFolder()

app.moveToApplicationsFolder([options]) macOS

  • options Object (optional)

  • conflictHandler Function (optional) - A handler for potential conflict in move failure.

  • conflictType String - The type of move conflict encountered by the handler; can be exists or existsAndRunning, where exists means that an app of the same name is present in the Applications directory and existsAndRunning means both that it exists and that it’s presently running.

Returns Boolean - Whether the move was successful. Please note that if the move is successful, your application will quit and relaunch.

No confirmation dialog will be presented by default. If you wish to allow the user to confirm the operation, you may do so using the dialog API.

注意:如果并非是用户造成操作失败,这个方法会抛出错误。 例如,如果用户取消了授权会话,这个方法将返回false。 如果无法执行复制操作, 则此方法将抛出错误。 The message in the error should be informative and tell you exactly what went wrong.

By default, if an app of the same name as the one being moved exists in the Applications directory and is not running, the existing app will be trashed and the active app moved into its place. If it is running, the pre-existing running app will assume focus and the the previously active app will quit itself. This behavior can be changed by providing the optional conflict handler, where the boolean returned by the handler determines whether or not the move conflict is resolved with default behavior. i.e. returning false will ensure no further action is taken, returning true will result in the default behavior and the method continuing.

例如:

app.moveToApplicationsFolder({  conflictHandler: (conflictType) => {    if (conflictType === 'exists') {      return dialog.showMessageBoxSync({        type: 'question',        buttons: ['Halt Move', 'Continue Move'],        defaultId: 0,        message: 'An app of this name already exists'      }) === 1    }  }})

Would mean that if an app already exists in the user directory, if the user chooses to ‘Continue Move’ then the function would continue with its default behavior and the existing app will be trashed and the active app moved into its place.

属性

app.accessibilitySupportEnabled macOS Windows

A Boolean property that’s true if Chrome’s accessibility support is enabled, false otherwise. This property will be true if the use of assistive technologies, such as screen readers, has been detected. Setting this property to true manually enables Chrome’s accessibility support, allowing developers to expose accessibility switch to users in application settings.

See Chromium’s accessibility docs for more details. 默认为禁用

此 API 必须在 ready 事件触发后调用

注意: 渲染进程树会明显的影响应用的性能。默认情况下不应该启用。

app.applicationMenu

A Menu | null property that returns Menu if one has been set and null otherwise. Users can pass a Menu to set this property.

app.badgeCount Linux macOS

An Integer property that returns the badge count for current app. Setting the count to 0 will hide the badge.

On macOS, setting this with any nonzero integer shows on the dock icon. On Linux, this property only works for Unity launcher.