Not able to open file explorer ,on clicking a button - javascript

my code in node js side is-
const exec = require('child_process').exec;
var cmdString = 'start C:/myApp/dropbox';
const child = exec(cmdString,
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
})
if i start the application from command prompt, I am able to open windows file explore on clicking a button.
But, if i start the application from windows service. I am not able to open file explorer.
Any idea what mistake i am doing in my coding side.

Related

How can I access the crontab file with Node.js?

I am currently developing an Electron app and need it to look into the crontab file and parse it.
How can Node.js (or something else in Electron) access the main crontab file and read it?
I looked into the cron-parser library but it is only able to read cron-formatted strings, not access the file.
Thanks in advance.
Since the crontab file we see when we run is just a tmp file that shows whatever cron jobs the current user has created (which are stored in /var/spool/cron/crontabs) when whe don't normally have access to those files, I'd suggest you run a shell script to get these data:
const { exec } = require("child_process");
exec("crontab -l", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
//Do stuff with the read string on stdout
});
Hopefully this should give you the contents of the crontab under the user that's running the node script

Can you control a unit outside your home with JavaScript? For example, shut down a Raspberry Pi 3 with the button "Shut down" on a web page

A few years ago I made a UI using 7-inch touchscreen connected to Raspberry Pi 3.
I had a digital button "shut down" and it worked perfectly to send a message to the Raspberry with Java (see the code below).
try {
Runtime.getRuntime().exec("sudo shutdown -h now");
} catch (IOException e) {
e.printStackTrace();
}
Now I want to shut down a Raspberry Pi 3 with a "Shut down" button on a web page using JavaScript. So I assume I need to give privileges to the web server and after that send this message sudo shutdown -h now to the Raspberry. Any suggestions?
You need to use Node.js if you want to use server-side JavaScript.
Something like this:
var express = require('express')
var app = express()
const { exec } = require("child_process");
// when navigating to your http://<yourdomain>/shutdown
app.get('/shutdown', function (req, res) {
exec("sudo shutdown -h now", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
})

How to print a set of PDF Files in using node.js

Hi I want to print(using printer) list of PDF files using nodejs. But not able to find any proper way. I found one JavaScript library called print.js(http://printjs.crabbly.com)
But with that also I m not able to call it in loop.
Is there anything I can do for this.
var pdflist = [a.pdf,b.pdf] //(this is my PDF list)
Thanks for help.
You can use ghostscript command line tools from your node app by forking a child process to execute the commands and loop thru your pdfs.
// OS : windows 64bits (for other OSs : linux, macosx ...etc; it's almost the same thing)
//assuming here that pdf is the path string to your pdf file
//printer name : Apple LaserWriter II NT
pdflist.foreach( function (pdf,index){
require("child_process").exec('gswin64c.exe ... -sOutputFile="%printer%Apple LaserWriter II NT" ' + pdf,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
);
});

How to display a popup Window just when a browser is started running from nodejs

I want to popup a window on Internet explorer windows 10 from nodejs.
I did research on google but I found only the window-popup, popup packages but it is throwing me error something like this ReferenceError: window is not defined
at windowPopup
Even after installing the packages thrpugh npm.
I am doing this on WebStorm ide.
exec(`START iexplore.exe`, (err, stdout, stderr) => {
if (err) {
throw err
}
console.log('stdout', stdout)
console.log('stderr', err)
});
var popup = require('window-popup').windowPopup;
What is the correct way to do this, please help me out where I am going wrong.
Thanks.

Run shell command from JavaScript in node-webkit?

Is there any way to run shell command from JavaScript in node-webkit?
There is a lot of similar questions, but it didn't help me.
I'm trying to build simple desktop app for listing installed tools.
I've created node module 'tools-v' which is installed globally and works when I run it in command line.
This module run several commands: npm -v, node -v, git -v etc.
I'm on Windows 7.
//var sys = require('sys');
var exec = require('child_process').exec;
//var toolsv = (process.platform === "win32" ? "tools-v.cmd" : "tools-v");
$(document).ready(function() {
//myCmd = "C:\\Users\\win7\\AppData\\Roaming\\npm\\tools-v.cmd";
//myCmd = toolsv;
myCmd = 'tools-v';
//gui.Shell.openItem('firefox',function(error, stdout, stderr) { });
//opening Firefox works.
exec(myCmd, function (error, stdout, stderr) {
//detached: true;
console.log('stdout: ' + stdout);
$('#output').append('stdout: ' + stdout)
if (error !== null) {
console.log('exec error: ' + error);
}
});
});
I'm always getting error:
""exec error: Error: spawn ENOENT""
I tried spawn instead of exec. I also tried several other commands, beside node module.
Thanks.
Actually, this code works. I just didn't built full app, I tested it trough sublime build for node-webkit. Preforming full build with grunt solved every spawn issues.

Categories

Resources