Neutralino.os
Neutralino.os namespace contains methods related to the user's operating system.
os.execCommand(command, options)
Executes a command and returns the output.
Parameters
commandString: The command that is to be executed.
Options
backgroundBoolean: Executes the command in background and resolve the Promise immediately if this is set totrue. This option makes the process detached from the API function call, so if you need to connect with the newly created process later, consider usingos.spawnProcess.stdInString: Standard input as a string.cwdString: Current working directory.envsObject: Key-value pairs of an environment variables set.
Return Object (awaited):
pidNumber: Process identifier.stdOutString: Standard output.stdErrString: Standard error.exitCodeNumber: Exit code of the process.
let info = await Neutralino.os.execCommand('python --version');
console.log(`Your Python version: ${info.stdOut}`);
await Neutralino.os.execCommand('npm start', { background: true });
await Neutralino.os.execCommand('node index.js', { envs: {
VAR1: 'Value1',
VAR2: 'Value2'
}});
os.spawnProcess(command, options)
Spawns a process based on a command in background and let developers control it.
Parameters
commandString: The command that is to be executed in a new process.
Options
cwdString (optional): Current working directory.envsObject: Key-value pairs of an environment variables set.
Return Object (awaited):
idNumber: A Neutralino-scoped process identifier. This value is used for controlling the process via the native API.pidNumber: Process identifier from the operating system.
let pingProc = await Neutralino.os.spawnProcess('ping neutralino.js.org');
Neutralino.events.on('spawnedProcess', (evt) => {
if(pingProc.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(`Ping process terminated with exit code: ${evt.detail.data}`);
break;
}
}
});
os.updateSpawnedProcess(id, action, data)
Updates a spawned process based on a provided action and data. Throws NE_OS_UNLTOUP if the process cannot be
updated.
Parameters
idNumber: Neutralino-scoped process identifier.actionString: An action to take. Accepts only the following values:stdIn: Sends data to the process via the standard input stream.stdInEnd: Closes the standard input stream file descriptor.exit: Terminates the process.
dataObject (optional): Additional data for theaction. Send stardard input string if theactionisstdIn.
let nodeProc = await Neutralino.os.spawnProcess('node');
Neutralino.events.on('spawnedProcess', (evt) => {
if(nodeProc.id == evt.detail.id) {
switch(evt.detail.action) {
case 'stdOut':
console.log(evt.detail.data); // 10
break;
case 'stdErr':
console.error(evt.detail.data);
break;
case 'exit':
console.log(`Node.js process terminated with exit code: ${evt.detail.data}`);
break;
}
}
});
await Neutralino.os.updateSpawnedProcess(nodeProc.id, 'stdIn', 'console.log(5 + 5);');
await Neutralino.os.updateSpawnedProcess(nodeProc.id, 'stdInEnd');
os.getSpawnedProcesses()
Returns all spawned processes.
Return Object (awaited):
An array of SpawnedProcess objects.
SpawnedProcess
idNumber: A Neutralino-scoped process identifier..pidNumber: Process identifier from the operating system.
await Neutralino.os.spawnProcess('ping neutralino.js.org');
await Neutralino.os.spawnProcess('ping codezri.org');
let processes = await Neutralino.os.getSpawnedProcesses();
console.log(processes);
os.getEnv(key)
Provides the value of a given environment variable.
Parameters
keyString: The name of the environment variable.
Return String (awaited):
Value of the given environment variable. Returns an empty string if the environment variable is not defined.
let value = await Neutralino.os.getEnv('USER');
console.log(`USER = ${value}`);
os.getEnvs()
Returns all environment variables and their values.
Return Object (awaited):
Environment variables details in key-value pairs.
let envs = await Neutralino.os.getEnvs();
console.log(envs);
os.showOpenDialog(title, options)
Shows the file open dialog. You can use this function to obtain paths of existing files.
Parameters
titleString (optional): Title of the dialog.
Options
filtersFilter[] (optional): An array of Filter objects to filter the files list.multiSelections(optional): Enables multi selections.defaultPathString (optional): Initial path/filename displayed by the dialog.
Filter
nameString: Filter name.extensionsString: Array of file extensions. Eg:['jpg', 'png']
Return Object (awaited):
An array of selected entries.
let entries = await Neutralino.os.showOpenDialog('Open a diagram', {
defaultPath: '/home/my/directory/',
filters: [
{name: 'Images', extensions: ['jpg', 'png']},
{name: 'All files', extensions: ['*']}
]
});
console.log('You have selected:', entries);
os.showSaveDialog(title, options)
Shows the file save dialog. You can use this function to obtain a path to create a new file.
Parameters
titleString (optional): Title of the dialog.
Options
filtersFilter[] (optional): An array of Filter objects to filter the files list.forceOverwriteBoolean (optional): Skips file overwrite warning message.defaultPathString (optional): Initial path/filename displayed by the dialog.
Filter
nameString: Filter name.extensionsString[]: Array of file extensions. Eg:['jpg', 'png']
Return String (awaited):
Selected filename.
let entry = await Neutralino.os.showSaveDialog('Save to file', {
defaultPath: 'untitled.jpg',
filters: [
{name: 'Images', extensions: ['jpg', 'png']},
{name: 'All files', extensions: ['*']}
]
});
console.log('You have selected:', entry);
os.showFolderDialog(title)
Shows the folder open dialog.
Parameters
titleString (optional): Title of the dialog.
Options
defaultPathString (optional): Initial path displayed by the dialog.
Return String (awaited):
Selected folder.
let entry = await Neutralino.os.showFolderDialog('Select installation directory', {
defaultPath: '/home/my/directory/'
});
console.log('You have selected:', entry);
os.showNotification(title, content, icon)
Displays a notification message.
Parameters
titleString: Notification title.contentString: Content of the notification.iconString (optional): Icon name. Accpeted values are:INFO,WARNING,ERROR, andQUESTION. The default value isINFO
await Neutralino.os.showNotification('Hello world', 'It works! Have a nice day');
await Neutralino.os.showNotification('Oops :/', 'Something went wrong', 'ERROR');
os.showMessageBox(title, content, choice, icon)
Displays a message box.
Parameters
titleString: Title of the message box.contentString: Content of the message box.choiceString (optional): Message box buttons. Accpeted values are:OK,OK_CANCEL,YES_NO,YES_NO_CANCEL,RETRY_CANCEL, andABORT_RETRY_IGNORE. The default value isOK.iconString (optional): Icon name. Accpeted values are:INFO,WARNING,ERROR, andQUESTION. The default value isINFO.
Return String (awaited):
User's choice.
await Neutralino.os.showMessageBox('Hello', 'Welcome');
let button = await Neutralino.os
.showMessageBox('Confirm',
'Are you sure you want to quit?',
'YES_NO', 'QUESTION');
if(button == 'YES') {
Neutralino.app.exit();
}
os.setTray(options)
Creates/updates the tray icon and menu.
Options
iconString: Tray icon path. Eg:/resources/icons/trayIcon.png. A20x20-sized PNG image file works fine on all supported operating systems.menuItemsTrayMenuItem[]: An array ofTrayMenuItemobjects.
TrayMenuItem
idString (optional): A unique identifier for each menu item.textString: Label of the menu item. This field is a mandatory field. Use-(hyphen) character for a menu separator.isDisabledBoolean (optional): A boolean flag to disable/enable a specific menu item.isCheckedBoolean (optional): A boolean flag to mark a specific menu item as selected.
let tray = {
icon: '/resources/icons/trayIcon.png',
menuItems: [
{id: "about", text: "About"},
{text: "-"},
{id: "quit", text: "Quit"}
]
};
await Neutralino.os.setTray(tray);
os.getPath(name)
Returns known platform-specific folders such as Downloads, Music, Videos, etc.
Parameters
titleString: Name of the folder. Accepted values are:config,data,cache,documents,pictures,music,video,downloads,saveGames1,saveGames2, andtemp. ThrowsNE_OS_INVKNPTfor invalid folder names.
Return String (awaited):
Path.
let downloadsPath = await Neutralino.os.getPath('downloads');
console.log(`Downloads folder: ${downloadsPath}`);
os.open(url)
Opens a URL with the default web browser.
If your application is running in the default web browser, this method will open a new tab.
Parameters
urlString: URL to be opened.
Neutralino.os.open('https://neutralino.js.org');