neutralinojs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Neutralino.js Platform Skill

Neutralino.js 平台技能

Consolidated skill for building cross-platform desktop applications with Neutralino.js. Use decision trees below to find the right APIs and configuration options, then load detailed references.
使用Neutralino.js构建跨平台桌面应用的综合技能指南。使用下方的决策树找到合适的API和配置选项,然后查看详细参考文档。

When to Apply

适用场景

  • Building desktop utilities with file system operations
  • Executing system commands or managing processes
  • Creating apps with tray icons, notifications, or clipboard integration
  • Accessing system information (CPU, memory, displays)
  • 构建包含文件系统操作的桌面工具
  • 执行系统命令或管理进程
  • 创建包含托盘图标、通知或剪贴板集成的应用
  • 访问系统信息(CPU、内存、显示器)

Critical Rules

核心规则

Follow these rules in all Neutralino.js code:
  1. Always use
    neu create <path>
    for new projects
    unless explicitly asked to create from scratch.
  2. Import
    @neutralinojs/lib
    for frontend frameworks
    (React, Vue, etc.) instead of including
    neutralino.js
    directly.
  3. Never use
    tokenSecurity: none
    when exposing native APIs - always use
    one-time
    (default) for security.
  4. Configure
    nativeAllowList
    and
    nativeBlockList
    to restrict API access and enhance security.
  5. Call
    Neutralino.init()
    before using any native API methods.
  6. Handle window close events properly using
    events.on('windowClose')
    - never rely solely on the close button.
  7. Initialize First: Call
    Neutralino.init()
    before any native API usage, see below:
javascript
// WRONG - calling APIs before initialization
await Neutralino.os.getEnv('USER');

// RIGHT - initialize then use APIs
Neutralino.init();
await Neutralino.os.getEnv('USER');
Handle Window Close: Always implement windowClose handler to prevent hanging processes
javascript
// WRONG - missing exit handler
Neutralino.init();

// RIGHT - proper cleanup on window close
Neutralino.init();
Neutralino.events.on("windowClose", () => {
  Neutralino.app.exit();
});
Error Code Patterns: Native API errors use specific codes (
NE_FS_*
,
NE_OS_*
,
NE_RT_*
)
javascript
// Check for specific filesystem errors
try {
  await Neutralino.filesystem.readFile('./config.txt');
} catch(err) {
  if (err.code === 'NE_FS_NOPATHE') {
    // File doesn't exist
  } else if (err.code === 'NE_FS_FILRDER') {
    // File read error
  }
}
所有Neutralino.js代码都需遵循以下规则:
  1. 新项目请始终使用
    neu create <path>
    命令
    ,除非明确要求从零开始创建。
  2. 前端框架(React、Vue等)请导入
    @neutralinojs/lib
    ,而非直接引入
    neutralino.js
  3. 暴露原生API时绝不要使用
    tokenSecurity: none
    —— 为保障安全,请始终使用默认的
    one-time
    选项。
  4. 配置
    nativeAllowList
    nativeBlockList
    以限制API访问,提升安全性。
  5. 调用任何原生API方法前请先执行
    Neutralino.init()
  6. 使用
    events.on('windowClose')
    正确处理窗口关闭事件
    —— 不要仅依赖关闭按钮。
  7. 初始化优先:调用任何原生API前先执行
    Neutralino.init()
    ,示例如下:
javascript
// 错误示例 - 初始化前调用API
await Neutralino.os.getEnv('USER');

// 正确示例 - 先初始化再使用API
Neutralino.init();
await Neutralino.os.getEnv('USER');
处理窗口关闭:始终实现windowClose处理器以避免进程挂起
javascript
// 错误示例 - 缺少退出处理器
Neutralino.init();

// 正确示例 - 窗口关闭时正确清理
Neutralino.init();
Neutralino.events.on("windowClose", () => {
  Neutralino.app.exit();
});
错误代码模式:原生API错误使用特定代码(
NE_FS_*
NE_OS_*
NE_RT_*
javascript
// 检查特定文件系统错误
try {
  await Neutralino.filesystem.readFile('./config.txt');
} catch(err) {
  if (err.code === 'NE_FS_NOPATHE') {
    // 文件不存在
  } else if (err.code === 'NE_FS_FILRDER') {
    // 文件读取错误
  }
}

How to Use This Skill

如何使用本技能指南

Reference File Structure

参考文档结构

Neutralino.js references follow a hierarchical structure. Cross-cutting concepts and how-to guides are separate.
API References in
./references/api/
:
  • overview.md
    - All API namespaces and architecture
  • app.md
    - Application lifecycle, config, broadcasting
  • window.md
    - Window management (create, focus, size, tray)
  • filesystem.md
    - File operations, watchers, paths
  • os.md
    - OS operations, environment variables, spawning processes
  • storage.md
    - Key-value storage (SQLite-based)
  • clipboard.md
    - System clipboard access
  • computer.md
    - System info (memory, OS version)
  • events.md
    - Event system (on, off, dispatch, broadcast)
  • extensions.md
    - Extension communication
  • custom.md
    - Custom method execution
  • resources.md
    - Bundle resource access
  • server.md
    - Server info and configuration
  • updater.md
    - Auto-updater functionality
  • init.md
    - Initialization API
  • error-codes.md
    - Error code reference
  • global-variables.md
    - Predefined global variables
CLI Reference in
./references/cli/
:
  • neu-cli.md
    - All CLI commands (create, run, build, update, etc.)
  • internal-cli-arguments.md
    - Framework binary CLI arguments
Configuration in
./references/configuration/
:
  • neutralino.config.json.md
    - Complete config reference
  • modes.md
    - Application modes (window, browser, cloud, chrome)
  • project-structure.md
    - Project directory layout
Getting Started in
./references/getting-started/
:
  • introduction.md
    - What is Neutralino.js, features, comparisons
  • using-frontend-libraries.md
    - React, Vue, Angular integration
How-To Guides in
./references/how-to/
:
  • extensions-overview.md
    - Building and using extensions
  • auto-updater.md
    - Implementing auto-updates
Distribution in
./references/distribution/
:
  • overview.md
    - Building and distributing applications
Neutralino.js参考文档采用层级结构,跨领域概念和操作指南单独归类。
API参考 位于
./references/api/
  • overview.md
    - 所有API命名空间与架构
  • app.md
    - 应用生命周期、配置、广播功能
  • window.md
    - 窗口管理(创建、聚焦、尺寸、托盘)
  • filesystem.md
    - 文件操作、监视器、路径处理
  • os.md
    - 操作系统操作、环境变量、进程启动
  • storage.md
    - 键值存储(基于SQLite)
  • clipboard.md
    - 系统剪贴板访问
  • computer.md
    - 系统信息(内存、OS版本)
  • events.md
    - 事件系统(绑定、解绑、分发、广播)
  • extensions.md
    - 扩展通信
  • custom.md
    - 自定义方法执行
  • resources.md
    - 打包资源访问
  • server.md
    - 服务器信息与配置
  • updater.md
    - 自动更新功能
  • init.md
    - 初始化API
  • error-codes.md
    - 错误代码参考
  • global-variables.md
    - 预定义全局变量
CLI参考 位于
./references/cli/
  • neu-cli.md
    - 所有CLI命令(create、run、build、update等)
  • internal-cli-arguments.md
    - 框架二进制文件CLI参数
配置参考 位于
./references/configuration/
  • neutralino.config.json.md
    - 完整配置参考
  • modes.md
    - 应用模式(window、browser、cloud、chrome)
  • project-structure.md
    - 项目目录结构
入门指南 位于
./references/getting-started/
  • introduction.md
    - Neutralino.js是什么、特性、竞品对比
  • using-frontend-libraries.md
    - React、Vue、Angular集成
操作指南 位于
./references/how-to/
  • extensions-overview.md
    - 扩展的构建与使用
  • auto-updater.md
    - 实现自动更新
分发指南 位于
./references/distribution/
  • overview.md
    - 应用构建与分发

Reading Order

阅读顺序

  1. Start with
    getting-started/introduction.md
    for overview
  2. Then read based on your task:
    • Creating new app ->
      cli/neu-cli.md
      (neu create)
    • Using native APIs ->
      api/overview.md
      + specific namespace
    • Window management ->
      api/window.md
      +
      configuration/modes.md
    • File operations ->
      api/filesystem.md
    • Configuration ->
      configuration/neutralino.config.json.md
    • Building extensions ->
      how-to/extensions-overview.md
    • Auto-updater ->
      how-to/auto-updater.md
    • Frontend integration ->
      getting-started/using-frontend-libraries.md
    • Distribution ->
      distribution/overview.md
  1. getting-started/introduction.md
    开始
    了解概览
  2. 根据任务选择阅读内容
    • 创建新应用 →
      cli/neu-cli.md
      (neu create命令)
    • 使用原生API →
      api/overview.md
      + 对应命名空间文档
    • 窗口管理 →
      api/window.md
      +
      configuration/modes.md
    • 文件操作 →
      api/filesystem.md
    • 配置 →
      configuration/neutralino.config.json.md
    • 构建扩展 →
      how-to/extensions-overview.md
    • 自动更新 →
      how-to/auto-updater.md
    • 前端框架集成 →
      getting-started/using-frontend-libraries.md
    • 应用分发 →
      distribution/overview.md

Example Paths

示例路径

./references/getting-started/introduction.md       # Start here
./references/cli/neu-cli.md                         # CLI commands
./references/api/overview.md                        # All APIs overview
./references/api/filesystem.md                       # File operations
./references/configuration/neutralino.config.json.md # Config reference
./references/how-to/extensions-overview.md          # Extensions
./references/api/window.md                          # Window management
./references/getting-started/introduction.md       # 入门起点
./references/cli/neu-cli.md                         # CLI命令参考
./references/api/overview.md                        # API总览
./references/api/filesystem.md                       # 文件操作
./references/configuration/neutralino.config.json.md # 配置参考
./references/how-to/extensions-overview.md          # 扩展指南
./references/api/window.md                          # 窗口管理

Quick Decision Trees

快速决策树

"I want to create a new Neutralino.js app"

"我想创建一个新的Neutralino.js应用"

Create app?
├─ Quick start with default template -> neu create myapp
├─ Use custom template -> neu create myapp --template=<account>/<repo>
├─ Integrate with frontend framework -> getting-started/using-frontend-libraries.md
└─ From scratch (expert) -> configuration/neutralino.config.json.md
创建应用?
├─ 使用默认模板快速开始 -> neu create myapp
├─ 使用自定义模板 -> neu create myapp --template=<account>/<repo>
├─ 集成前端框架 -> getting-started/using-frontend-libraries.md
└─ 从零开始(进阶用户)-> configuration/neutralino.config.json.md

"I need to manage windows"

"我需要管理窗口"

Window management?
├─ Create/show window -> api/window.md (window.create, window.show)
├─ Set window size/position -> api/window.md (window.setSize, window.setPosition)
├─ Window focus/maximize/minimize -> api/window.md (window.focus, window.maximize, window.minimize)
├─ System tray icon -> api/window.md (window.setTrayOptions)
├─ Transparent window -> configuration/neutralino.config.json.md (modes.window.transparent)
├─ Borderless window -> configuration/neutralino.config.json.md (modes.window.borderless)
└─ Fullscreen mode -> api/window.md (window.enterFullScreen)
窗口管理?
├─ 创建/显示窗口 -> api/window.md (window.create, window.show)
├─ 设置窗口尺寸/位置 -> api/window.md (window.setSize, window.setPosition)
├─ 窗口聚焦/最大化/最小化 -> api/window.md (window.focus, window.maximize, window.minimize)
├─ 系统托盘图标 -> api/window.md (window.setTrayOptions)
├─ 透明窗口 -> configuration/neutralino.config.json.md (modes.window.transparent)
├─ 无边框窗口 -> configuration/neutralino.config.json.md (modes.window.borderless)
└─ 全屏模式 -> api/window.md (window.enterFullScreen)

"I need to work with files"

"我需要处理文件"

File operations?
├─ Read text file -> api/filesystem.md (readFile)
├─ Write text file -> api/filesystem.md (writeFile)
├─ Read directory -> api/filesystem.md (readDirectory)
├─ Create/remove directory -> api/filesystem.md (createDirectory, remove)
├─ Copy/move files -> api/filesystem.md (copy, move)
├─ Watch file changes -> api/filesystem.md (createWatcher)
├─ File stats (exists, size, modified) -> api/filesystem.md (getStats)
├─ Path operations -> api/filesystem.md (getAbsolutePath, getRelativePath)
└─ File permissions -> api/filesystem.md (getPermissions, setPermissions)
文件操作?
├─ 读取文本文件 -> api/filesystem.md (readFile)
├─ 写入文本文件 -> api/filesystem.md (writeFile)
├─ 读取目录 -> api/filesystem.md (readDirectory)
├─ 创建/删除目录 -> api/filesystem.md (createDirectory, remove)
├─ 复制/移动文件 -> api/filesystem.md (copy, move)
├─ 监听文件变化 -> api/filesystem.md (createWatcher)
├─ 文件状态(存在性、大小、修改时间)-> api/filesystem.md (getStats)
├─ 路径操作 -> api/filesystem.md (getAbsolutePath, getRelativePath)
└─ 文件权限 -> api/filesystem.md (getPermissions, setPermissions)

"I need OS-level operations"

"我需要执行操作系统级操作"

OS operations?
├─ Execute system command -> api/os.md (execCommand)
├─ Get environment variable -> api/os.md (getEnv)
├─ Set environment variable -> api/os.md (setEnv)
├─ Spawn process with I/O -> api/os.md (spawnProcess)
├─ Get OS info -> api/computer.md (getOsInfo)
├─ Get memory info -> api/computer.md (getMemoryInfo)
├─ Open external URL -> api/os.md (open)
└─ Path constants (NL_PATH, NL_CWD) -> api/global-variables.md
操作系统操作?
├─ 执行系统命令 -> api/os.md (execCommand)
├─ 获取环境变量 -> api/os.md (getEnv)
├─ 设置环境变量 -> api/os.md (setEnv)
├─ 启动带I/O的进程 -> api/os.md (spawnProcess)
├─ 获取操作系统信息 -> api/computer.md (getOsInfo)
├─ 获取内存信息 -> api/computer.md (getMemoryInfo)
├─ 打开外部URL -> api/os.md (open)
└─ 路径常量(NL_PATH、NL_CWD)-> api/global-variables.md

"I need data storage"

"我需要数据存储"

Storage?
├─ Simple key-value storage -> api/storage.md (getData, setData, putData)
├─ Persist data across app restarts -> api/storage.md
└─ Custom database -> how-to/extensions-overview.md (build extension)
存储功能?
├─ 简单键值存储 -> api/storage.md (getData, setData, putData)
├─ 跨应用重启持久化数据 -> api/storage.md
└─ 自定义数据库 -> how-to/extensions-overview.md(构建扩展)

"I need to handle events"

"我需要处理事件"

Events?
├─ Window close event -> api/events.md (windowClose)
├─ Window focus/blur -> api/events.md (windowFocus, windowBlur)
├─ Tray menu click -> api/events.md (trayMenuItemClicked)
├─ App instance events -> api/events.md (appClientConnect, appClientDisconnect)
├─ Extension events -> api/events.md (extensionReady, extClientConnect)
└─ Custom events -> api/events.md (dispatch, broadcast)
事件处理?
├─ 窗口关闭事件 -> api/events.md (windowClose)
├─ 窗口聚焦/失焦 -> api/events.md (windowFocus, windowBlur)
├─ 托盘菜单点击 -> api/events.md (trayMenuItemClicked)
├─ 应用实例事件 -> api/events.md (appClientConnect, appClientDisconnect)
├─ 扩展事件 -> api/events.md (extensionReady, extClientConnect)
└─ 自定义事件 -> api/events.md (dispatch, broadcast)

"I need to build and distribute"

"我需要构建和分发应用"

Build & distribute?
├─ Build for current platform -> cli/neu-cli.md (neu build)
├─ Create portable ZIP -> cli/neu-cli.md (--release)
├─ Single-file executable -> cli/neu-cli.md (--embed-resources)
├─ Auto-updater -> how-to/auto-updater.md
└─ Distribution overview -> distribution/overview.md
构建与分发?
├─ 为当前平台构建 -> cli/neu-cli.md (neu build)
├─ 创建便携ZIP包 -> cli/neu-cli.md (--release)
├─ 单文件可执行程序 -> cli/neu-cli.md (--embed-resources)
├─ 自动更新 -> how-to/auto-updater.md
└─ 分发总览 -> distribution/overview.md

"I need to use extensions"

"我需要使用扩展"

Extensions?
├─ Overview and concepts -> how-to/extensions-overview.md
├─ Define extensions in config -> configuration/neutralino.config.json.md (extensions)
├─ Enable extensions -> configuration/neutralino.config.json.md (enableExtensions)
├─ Dispatch to extension -> api/extensions.md (dispatch)
├─ Extension receives messages -> how-to/extensions-overview.md (WebSocket protocol)
└─ Extension calls native API -> how-to/extensions-overview.md (app.broadcast)
扩展功能?
├─ 概览与概念 -> how-to/extensions-overview.md
├─ 在配置中定义扩展 -> configuration/neutralino.config.json.md (extensions)
├─ 启用扩展 -> configuration/neutralino.config.json.md (enableExtensions)
├─ 向扩展分发消息 -> api/extensions.md (dispatch)
├─ 扩展接收消息 -> how-to/extensions-overview.md (WebSocket协议)
└─ 扩展调用原生API -> how-to/extensions-overview.md (app.broadcast)

"I need app lifecycle control"

"我需要控制应用生命周期"

Lifecycle?
├─ Exit app -> api/app.md (exit)
├─ Kill process (force) -> api/app.md (killProcess)
├─ Restart app -> api/app.md (restartProcess)
├─ Get app config -> api/app.md (getConfig)
├─ Broadcast to all instances -> api/app.md (broadcast)
├─ Read/write stdin/stdout -> api/app.md (readProcessInput, writeProcessOutput)
└─ Initialize client -> api/init.md (init)
生命周期管理?
├─ 退出应用 -> api/app.md (exit)
├─ 强制终止进程 -> api/app.md (killProcess)
├─ 重启应用 -> api/app.md (restartProcess)
├─ 获取应用配置 -> api/app.md (getConfig)
├─ 向所有实例广播消息 -> api/app.md (broadcast)
├─ 读取/写入标准输入输出 -> api/app.md (readProcessInput, writeProcessOutput)
└─ 初始化客户端 -> api/init.md (init)

"Which mode should I use?"

"我应该使用哪种模式?"

Application mode?
├─ Desktop app with native window -> window mode (default)
├─ Web app with native features -> browser mode
├─ Background server/API -> cloud mode
├─ Chrome app style -> chrome mode
└─ Details -> configuration/modes.md
应用模式?
├─ 带原生窗口的桌面应用 -> window模式(默认)
├─ 带原生功能的Web应用 -> browser模式
├─ 后台服务器/API -> cloud模式
├─ Chrome应用风格 -> chrome模式
└─ 详细说明 -> configuration/modes.md

"I need security configuration"

"我需要配置安全选项"

Security?
├─ Restrict API access -> configuration/neutralino.config.json.md (nativeAllowList, nativeBlockList)
├─ Token security -> configuration/neutralino.config.json.md (tokenSecurity)
├─ Export auth info -> configuration/neutralino.config.json.md (exportAuthInfo)
└─ Global variables -> api/global-variables.md
安全配置?
├─ 限制API访问 -> configuration/neutralino.config.json.md (nativeAllowList, nativeBlockList)
├─ Token安全 -> configuration/neutralino.config.json.md (tokenSecurity)
├─ 导出认证信息 -> configuration/neutralino.config.json.md (exportAuthInfo)
└─ 全局变量 -> api/global-variables.md

"I need to integrate with frontend frameworks"

"我需要集成前端框架"

Frontend integration?
├─ React/Vue/Angular -> getting-started/using-frontend-libraries.md
├─ Import client lib -> npm: @neutralinojs/lib
├─ CLI frontend support -> configuration/neutralino.config.json.md (cli.frontendLibrary)
└─ HMR/DevTools -> cli/neu-cli.md (neu run with dev server)
前端框架集成?
├─ React/Vue/Angular -> getting-started/using-frontend-libraries.md
├─ 导入客户端库 -> npm: @neutralinojs/lib
├─ CLI前端支持 -> configuration/neutralino.config.json.md (cli.frontendLibrary)
└─ HMR/开发者工具 -> cli/neu-cli.md (配合开发服务器使用neu run)

Product Index

产品索引

API Namespaces

API命名空间

NamespaceEntry FileDescription
app
./references/api/app.md
Application lifecycle, config, broadcasting
window
./references/api/window.md
Window creation, management, tray
filesystem
./references/api/filesystem.md
File operations, watchers, paths
os
./references/api/os.md
OS operations, environment, processes
storage
./references/api/storage.md
Key-value storage
clipboard
./references/api/clipboard.md
Clipboard access
computer
./references/api/computer.md
System info (memory, OS)
events
./references/api/events.md
Event system
extensions
./references/api/extensions.md
Extension communication
custom
./references/api/custom.md
Custom method execution
resources
./references/api/resources.md
Bundle resources
server
./references/api/server.md
Server info
updater
./references/api/updater.md
Auto-updater
init
./references/api/init.md
Initialization
error-codes
./references/api/error-codes.md
Error codes
global-variables
./references/api/global-variables.md
Predefined globals
命名空间入口文件描述
app
./references/api/app.md
应用生命周期、配置、广播
window
./references/api/window.md
窗口创建、管理、托盘
filesystem
./references/api/filesystem.md
文件操作、监视器、路径
os
./references/api/os.md
操作系统操作、环境、进程
storage
./references/api/storage.md
键值存储
clipboard
./references/api/clipboard.md
剪贴板访问
computer
./references/api/computer.md
系统信息(内存、OS)
events
./references/api/events.md
事件系统
extensions
./references/api/extensions.md
扩展通信
custom
./references/api/custom.md
自定义方法执行
resources
./references/api/resources.md
打包资源
server
./references/api/server.md
服务器信息
updater
./references/api/updater.md
自动更新
init
./references/api/init.md
初始化
error-codes
./references/api/error-codes.md
错误代码
global-variables
./references/api/global-variables.md
预定义全局变量

Configuration

配置项

ConceptEntry FileDescription
Config Reference
./references/configuration/neutralino.config.json.md
Complete configuration options
Modes
./references/configuration/modes.md
Application modes (window, browser, cloud, chrome)
Project Structure
./references/configuration/project-structure.md
Directory layout
概念入口文件描述
配置参考
./references/configuration/neutralino.config.json.md
完整配置选项
应用模式
./references/configuration/modes.md
应用模式(window、browser、cloud、chrome)
项目结构
./references/configuration/project-structure.md
目录布局

CLI

CLI工具

ConceptEntry FileDescription
neu CLI
./references/cli/neu-cli.md
All CLI commands
Internal Arguments
./references/cli/internal-cli-arguments.md
Framework binary arguments
概念入口文件描述
neu CLI
./references/cli/neu-cli.md
所有CLI命令
内部参数
./references/cli/internal-cli-arguments.md
框架二进制文件参数

Getting Started

入门指南

ConceptEntry FileDescription
Introduction
./references/getting-started/introduction.md
Overview, features, comparisons
Frontend Libraries
./references/getting-started/using-frontend-libraries.md
React, Vue, Angular integration
概念入口文件描述
介绍
./references/getting-started/introduction.md
概览、特性、对比
前端库集成
./references/getting-started/using-frontend-libraries.md
React、Vue、Angular集成

How-To

操作指南

ConceptEntry FileDescription
Extensions
./references/how-to/extensions-overview.md
Building and using extensions
Auto Updater
./references/how-to/auto-updater.md
Implementing updates
概念入口文件描述
扩展
./references/how-to/extensions-overview.md
扩展构建与使用
自动更新
./references/how-to/auto-updater.md
实现更新功能

Distribution

分发指南

ConceptEntry FileDescription
Distribution
./references/distribution/overview.md
Building and distributing apps
概念入口文件描述
应用分发
./references/distribution/overview.md
应用构建与分发

Key Patterns

核心代码示例

File Operations

文件操作

javascript
// Read/write text files
let content = await Neutralino.filesystem.readFile('./data.txt');
await Neutralino.filesystem.writeFile('./output.txt', 'Hello World');

// Binary file operations
let buffer = await Neutralino.filesystem.readBinaryFile('./image.png');
let rawBin = new ArrayBuffer(1);
let view = new Uint8Array(rawBin);
view[0] = 64;
await Neutralino.filesystem.writeBinaryFile('./data.bin', rawBin);

// File stats and existence check
try {
  let stats = await Neutralino.filesystem.getStats('./file.txt');
  console.log(`Size: ${stats.size}, IsFile: ${stats.isFile}`);
} catch(err) {
  if (err.code === 'NE_FS_NOPATHE') {
    console.log('File does not exist');
  }
}
javascript
// 读取/写入文本文件
let content = await Neutralino.filesystem.readFile('./data.txt');
await Neutralino.filesystem.writeFile('./output.txt', 'Hello World');

// 二进制文件操作
let buffer = await Neutralino.filesystem.readBinaryFile('./image.png');
let rawBin = new ArrayBuffer(1);
let view = new Uint8Array(rawBin);
view[0] = 64;
await Neutralino.filesystem.writeBinaryFile('./data.bin', rawBin);

// 文件状态与存在性检查
try {
  let stats = await Neutralino.filesystem.getStats('./file.txt');
  console.log(`大小: ${stats.size}, 是否为文件: ${stats.isFile}`);
} catch(err) {
  if (err.code === 'NE_FS_NOPATHE') {
    console.log('文件不存在');
  }
}

Process Management

进程管理

javascript
// Execute command and get output
let result = await Neutralino.os.execCommand('python --version');
console.log(result.stdOut);

// Spawn background process with event monitoring
let proc = await Neutralino.os.spawnProcess('ping google.com');

Neutralino.events.on('spawnedProcess', (evt) => {
  if (proc.id === evt.detail.id) {
    switch(evt.detail.action) {
      case 'stdOut':
        console.log(evt.detail.data);
        break;
      case 'stdErr':
        console.error(evt.detail.data);
        break;
      case 'exit':
        console.log(`Exit code: ${evt.detail.data}`);
        break;
    }
  }
});

// Send input to spawned process
await Neutralino.os.updateSpawnedProcess(proc.id, 'stdIn', 'input data');
await Neutralino.os.updateSpawnedProcess(proc.id, 'stdInEnd');
javascript
// 执行命令并获取输出
let result = await Neutralino.os.execCommand('python --version');
console.log(result.stdOut);

// 启动后台进程并监听事件
let proc = await Neutralino.os.spawnProcess('ping google.com');

Neutralino.events.on('spawnedProcess', (evt) => {
  if (proc.id === evt.detail.id) {
    switch(evt.detail.action) {
      case 'stdOut':
        console.log(evt.detail.data);
        break;
      case 'stdErr':
        console.error(evt.detail.data);
        break;
      case 'exit':
        console.log(`退出码: ${evt.detail.data}`);
        break;
    }
  }
});

// 向启动的进程发送输入
await Neutralino.os.updateSpawnedProcess(proc.id, 'stdIn', 'input data');
await Neutralino.os.updateSpawnedProcess(proc.id, 'stdInEnd');

System Tray Integration

系统托盘集成

javascript
// Set up tray with menu
let tray = {
  icon: '/resources/icons/tray.png',
  menuItems: [
    {id: "show", text: "Show Window"},
    {text: "-"}, // Separator
    {id: "quit", text: "Quit"}
  ]
};

await Neutralino.os.setTray(tray);

// Handle tray menu clicks
Neutralino.events.on('trayMenuItemClicked', (evt) => {
  switch(evt.detail.id) {
    case 'show':
      // Show window logic
      break;
    case 'quit':
      Neutralino.app.exit();
      break;
  }
});
javascript
// 设置带菜单的托盘
let tray = {
  icon: '/resources/icons/tray.png',
  menuItems: [
    {id: "show", text: "显示窗口"},
    {text: "-"}, // 分隔符
    {id: "quit", text: "退出"}
  ]
};

await Neutralino.os.setTray(tray);

// 处理托盘菜单点击
Neutralino.events.on('trayMenuItemClicked', (evt) => {
  switch(evt.detail.id) {
    case 'show':
      // 显示窗口逻辑
      break;
    case 'quit':
      Neutralino.app.exit();
      break;
  }
});

Clipboard Operations

剪贴板操作

javascript
// Text clipboard
await Neutralino.clipboard.writeText('Hello World');
let text = await Neutralino.clipboard.readText();

// HTML clipboard
await Neutralino.clipboard.writeHTML('<p style="color:red;">Formatted</p>');
let html = await Neutralino.clipboard.readHTML();

// Check clipboard format
let format = await Neutralino.clipboard.getFormat();
javascript
// 文本剪贴板
await Neutralino.clipboard.writeText('Hello World');
let text = await Neutralino.clipboard.readText();

// HTML剪贴板
await Neutralino.clipboard.writeHTML('<p style="color:red;">格式化文本</p>');
let html = await Neutralino.clipboard.readHTML();

// 检查剪贴板格式
let format = await Neutralino.clipboard.getFormat();

System Information

系统信息

javascript
// CPU information
let cpu = await Neutralino.computer.getCPUInfo();
console.log(`CPU: ${cpu.model}, Cores: ${cpu.physicalCores}`);

// Display information
let displays = await Neutralino.computer.getDisplays();
displays.forEach(display => {
  console.log(`Display ${display.id}: ${display.resolution.width}x${display.resolution.height}`);
});

// Architecture and OS
let arch = await Neutralino.computer.getArch();
let username = await Neutralino.os.getEnv(NL_OS === 'Windows' ? 'USERNAME' : 'USER');
javascript
// CPU信息
let cpu = await Neutralino.computer.getCPUInfo();
console.log(`CPU: ${cpu.model}, 核心数: ${cpu.physicalCores}`);

// 显示器信息
let displays = await Neutralino.computer.getDisplays();
displays.forEach(display => {
  console.log(`显示器 ${display.id}: ${display.resolution.width}x${display.resolution.height}`);
});

// 架构与操作系统
let arch = await Neutralino.computer.getArch();
let username = await Neutralino.os.getEnv(NL_OS === 'Windows' ? 'USERNAME' : 'USER');

Notifications

通知功能

javascript
// Basic notification
await Neutralino.os.showNotification('Title', 'Message content');

// Notification with icon type
await Neutralino.os.showNotification('Error', 'Something went wrong', 'ERROR');
// Icon types: INFO, WARNING, ERROR, QUESTION
javascript
// 基础通知
await Neutralino.os.showNotification('标题', '消息内容');

// 带图标类型的通知
await Neutralino.os.showNotification('错误', '发生了一些问题', 'ERROR');
// 图标类型:INFO、WARNING、ERROR、QUESTION

Common Mistakes

常见错误

  • Missing
    await
    on async calls
    — All native APIs return promises
  • Not checking file existence — Use
    getStats()
    to verify files exist before operations
  • Forgetting process cleanup — Always handle spawned process exit events
  • Invalid tray icon paths — Use
    /resources/
    prefix for bundled assets
  • Missing error handling — Wrap native API calls in try-catch blocks
  • 异步调用遗漏
    await
    —— 所有原生API都返回Promise
  • 未检查文件存在性 —— 操作前使用
    getStats()
    验证文件是否存在
  • 忘记清理进程 —— 始终处理启动进程的退出事件
  • 托盘图标路径无效 —— 打包资源请使用
    /resources/
    前缀
  • 缺少错误处理 —— 将原生API调用包裹在try-catch块中

Resources

资源链接