neutralinojs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNeutralino.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:
- Always use for new projects unless explicitly asked to create from scratch.
neu create <path> - Import for frontend frameworks (React, Vue, etc.) instead of including
@neutralinojs/libdirectly.neutralino.js - Never use when exposing native APIs - always use
tokenSecurity: none(default) for security.one-time - Configure and
nativeAllowListto restrict API access and enhance security.nativeBlockList - Call before using any native API methods.
Neutralino.init() - Handle window close events properly using - never rely solely on the close button.
events.on('windowClose') - Initialize First: Call before any native API usage, see below:
Neutralino.init()
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代码都需遵循以下规则:
- 新项目请始终使用命令,除非明确要求从零开始创建。
neu create <path> - 前端框架(React、Vue等)请导入,而非直接引入
@neutralinojs/lib。neutralino.js - 暴露原生API时绝不要使用—— 为保障安全,请始终使用默认的
tokenSecurity: none选项。one-time - 配置和
nativeAllowList以限制API访问,提升安全性。nativeBlockList - 调用任何原生API方法前请先执行。
Neutralino.init() - 使用正确处理窗口关闭事件 —— 不要仅依赖关闭按钮。
events.on('windowClose') - 初始化优先:调用任何原生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/- - All API namespaces and architecture
overview.md - - Application lifecycle, config, broadcasting
app.md - - Window management (create, focus, size, tray)
window.md - - File operations, watchers, paths
filesystem.md - - OS operations, environment variables, spawning processes
os.md - - Key-value storage (SQLite-based)
storage.md - - System clipboard access
clipboard.md - - System info (memory, OS version)
computer.md - - Event system (on, off, dispatch, broadcast)
events.md - - Extension communication
extensions.md - - Custom method execution
custom.md - - Bundle resource access
resources.md - - Server info and configuration
server.md - - Auto-updater functionality
updater.md - - Initialization API
init.md - - Error code reference
error-codes.md - - Predefined global variables
global-variables.md
CLI Reference in :
./references/cli/- - All CLI commands (create, run, build, update, etc.)
neu-cli.md - - Framework binary CLI arguments
internal-cli-arguments.md
Configuration in :
./references/configuration/- - Complete config reference
neutralino.config.json.md - - Application modes (window, browser, cloud, chrome)
modes.md - - Project directory layout
project-structure.md
Getting Started in :
./references/getting-started/- - What is Neutralino.js, features, comparisons
introduction.md - - React, Vue, Angular integration
using-frontend-libraries.md
How-To Guides in :
./references/how-to/- - Building and using extensions
extensions-overview.md - - Implementing auto-updates
auto-updater.md
Distribution in :
./references/distribution/- - Building and distributing applications
overview.md
Neutralino.js参考文档采用层级结构,跨领域概念和操作指南单独归类。
API参考 位于:
./references/api/- - 所有API命名空间与架构
overview.md - - 应用生命周期、配置、广播功能
app.md - - 窗口管理(创建、聚焦、尺寸、托盘)
window.md - - 文件操作、监视器、路径处理
filesystem.md - - 操作系统操作、环境变量、进程启动
os.md - - 键值存储(基于SQLite)
storage.md - - 系统剪贴板访问
clipboard.md - - 系统信息(内存、OS版本)
computer.md - - 事件系统(绑定、解绑、分发、广播)
events.md - - 扩展通信
extensions.md - - 自定义方法执行
custom.md - - 打包资源访问
resources.md - - 服务器信息与配置
server.md - - 自动更新功能
updater.md - - 初始化API
init.md - - 错误代码参考
error-codes.md - - 预定义全局变量
global-variables.md
CLI参考 位于:
./references/cli/- - 所有CLI命令(create、run、build、update等)
neu-cli.md - - 框架二进制文件CLI参数
internal-cli-arguments.md
配置参考 位于:
./references/configuration/- - 完整配置参考
neutralino.config.json.md - - 应用模式(window、browser、cloud、chrome)
modes.md - - 项目目录结构
project-structure.md
入门指南 位于:
./references/getting-started/- - Neutralino.js是什么、特性、竞品对比
introduction.md - - React、Vue、Angular集成
using-frontend-libraries.md
操作指南 位于:
./references/how-to/- - 扩展的构建与使用
extensions-overview.md - - 实现自动更新
auto-updater.md
分发指南 位于:
./references/distribution/- - 应用构建与分发
overview.md
Reading Order
阅读顺序
- Start with for overview
getting-started/introduction.md - Then read based on your task:
- Creating new app -> (neu create)
cli/neu-cli.md - Using native APIs -> + specific namespace
api/overview.md - Window management -> +
api/window.mdconfiguration/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
- Creating new app ->
- 从开始 了解概览
getting-started/introduction.md - 根据任务选择阅读内容:
- 创建新应用 → (neu create命令)
cli/neu-cli.md - 使用原生API → + 对应命名空间文档
api/overview.md - 窗口管理 → +
api/window.mdconfiguration/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命名空间
| Namespace | Entry File | Description |
|---|---|---|
| app | | Application lifecycle, config, broadcasting |
| window | | Window creation, management, tray |
| filesystem | | File operations, watchers, paths |
| os | | OS operations, environment, processes |
| storage | | Key-value storage |
| clipboard | | Clipboard access |
| computer | | System info (memory, OS) |
| events | | Event system |
| extensions | | Extension communication |
| custom | | Custom method execution |
| resources | | Bundle resources |
| server | | Server info |
| updater | | Auto-updater |
| init | | Initialization |
| error-codes | | Error codes |
| global-variables | | Predefined globals |
| 命名空间 | 入口文件 | 描述 |
|---|---|---|
| app | | 应用生命周期、配置、广播 |
| window | | 窗口创建、管理、托盘 |
| filesystem | | 文件操作、监视器、路径 |
| os | | 操作系统操作、环境、进程 |
| storage | | 键值存储 |
| clipboard | | 剪贴板访问 |
| computer | | 系统信息(内存、OS) |
| events | | 事件系统 |
| extensions | | 扩展通信 |
| custom | | 自定义方法执行 |
| resources | | 打包资源 |
| server | | 服务器信息 |
| updater | | 自动更新 |
| init | | 初始化 |
| error-codes | | 错误代码 |
| global-variables | | 预定义全局变量 |
Configuration
配置项
| Concept | Entry File | Description |
|---|---|---|
| Config Reference | | Complete configuration options |
| Modes | | Application modes (window, browser, cloud, chrome) |
| Project Structure | | Directory layout |
| 概念 | 入口文件 | 描述 |
|---|---|---|
| 配置参考 | | 完整配置选项 |
| 应用模式 | | 应用模式(window、browser、cloud、chrome) |
| 项目结构 | | 目录布局 |
CLI
CLI工具
| Concept | Entry File | Description |
|---|---|---|
| neu CLI | | All CLI commands |
| Internal Arguments | | Framework binary arguments |
| 概念 | 入口文件 | 描述 |
|---|---|---|
| neu CLI | | 所有CLI命令 |
| 内部参数 | | 框架二进制文件参数 |
Getting Started
入门指南
| Concept | Entry File | Description |
|---|---|---|
| Introduction | | Overview, features, comparisons |
| Frontend Libraries | | React, Vue, Angular integration |
| 概念 | 入口文件 | 描述 |
|---|---|---|
| 介绍 | | 概览、特性、对比 |
| 前端库集成 | | React、Vue、Angular集成 |
How-To
操作指南
| Concept | Entry File | Description |
|---|---|---|
| Extensions | | Building and using extensions |
| Auto Updater | | Implementing updates |
| 概念 | 入口文件 | 描述 |
|---|---|---|
| 扩展 | | 扩展构建与使用 |
| 自动更新 | | 实现更新功能 |
Distribution
分发指南
| Concept | Entry File | Description |
|---|---|---|
| Distribution | | Building and distributing apps |
| 概念 | 入口文件 | 描述 |
|---|---|---|
| 应用分发 | | 应用构建与分发 |
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, QUESTIONjavascript
// 基础通知
await Neutralino.os.showNotification('标题', '消息内容');
// 带图标类型的通知
await Neutralino.os.showNotification('错误', '发生了一些问题', 'ERROR');
// 图标类型:INFO、WARNING、ERROR、QUESTIONCommon Mistakes
常见错误
- Missing on async calls — All native APIs return promises
await - Not checking file existence — Use to verify files exist before operations
getStats() - Forgetting process cleanup — Always handle spawned process exit events
- Invalid tray icon paths — Use prefix for bundled assets
/resources/ - Missing error handling — Wrap native API calls in try-catch blocks
- 异步调用遗漏—— 所有原生API都返回Promise
await - 未检查文件存在性 —— 操作前使用验证文件是否存在
getStats() - 忘记清理进程 —— 始终处理启动进程的退出事件
- 托盘图标路径无效 —— 打包资源请使用前缀
/resources/ - 缺少错误处理 —— 将原生API调用包裹在try-catch块中
Resources
资源链接
Repository: https://github.com/neutralinojs/neutralinojs
Documentation: https://neutralino.js.org
Client Library (NPM): https://www.npmjs.com/package/@neutralinojs/lib
CLI (NPM): https://www.npmjs.com/package/@neutralinojs/neu
Discussions: https://github.com/neutralinojs/neutralinojs/discussions