Running MacOS shortcuts shell command from node script - node.js

I am trying to run MacOS shortcuts via NodeJS script. To achieve that I created shortcuts in the MacOS Shortcuts app, which normally run in the terminal (ZSH) by typing shortcuts run shortcutname (Docs). They work fine when typing them directly into the terminal, but not when called from a NodeJS script.
My script looks like this:
exec('shortcuts run shortcutname', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
When starting the script from the terminal node script.js it does nothing. It does not return anything nor does it finish. I tried replacing the command shortcuts run shortcutname by other commands the system should know. For example ls, or git. They all work. Also when I leave out the shortcutname in the end, it complains about the missing parameter.
I was suspecting missing access rights, but apparently NodeJS runs as my normal user. Running whoami in script context returns my user name.
NODE_DEBUG=cluster,net,http,fs,tls,module,timers node script.js did not show anything unusual. I also tried using the full path of the binary. which shortcuts gave me /usr/bin/shortcuts. Which I used in my script /usr/bin/shortcuts run shortcutname.
What am I missing? Thanks in advance!

Though I thought I tested it, I found the solution:
execSync instead of exec works
It’d be cool if someone could explain why exec wouldn’t work.
Cheers!

Related

Run ffmpeg from its parent directory in node js application in its own directory

I have a node js application which needs to access ffmpeg. My approach to this has been to use {exec} from the child_processes module built into Node. The problem is that {exec} always starts the cmd line from the current directory and I can't figure out how to point the directory context to where ffmpeg.exe is located so that I can access the program. Is my approach flawed? How can I access a seperate CLI application from Node?
This code returns "'ffmpeg' is not recognized as an internal or external command" because I'm obviously in Node's execution context which is not where ffmpeg is located.
I also do not want to store the node application in the directory of ffmpeg.exe because that's just lazy and impractical.
exec(`ffmpeg -i ${filepathToMedia} --vf fps=1 ${outputdirectory}/out%d.png`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
if(stdout) {
console.log(`success: ${stdout}`)
} });
You could do one of two things here:
Use the absolute path to the ffmpeg executable so instead of just exec('ffmpeg ...') you'd do something likeexec('C:\Users\user\ffmpeg_dir\ffmpeg ...') This isn't very clean and will probably break if someone else tries to use your code.
Add your ffmpeg directory to your system's PATH environment variable. If you add ffmpeg to your PATH it becomes available as ffmpeg regardless of what folder you're in, allowing the script to work as-is. This'll also make it easier for other people to run your script.

How to execute a mongoDB shell script via Node.js?

I am working on my class project in which I want to demonstrate the use of mongoDB sharding. I am using mongoDB node.js native driver. I got to know there is no sharding functionality in this driver. So, I have to write shell script to do sharding. So, Is it possible to do this somehow like this:
node myfile.js (executes my shell script and run my code)
Given that you already have a shell script, why not execute that through the Child Process module. Just use the below function to run the script that you have.
child_process.execFileSync(file[, args][, options])
Note that the script should have run permissions(use chmod a+x script otherwise)
why don't you consider using npm run scripts?
if you want the script to run standalone, add scripts with test/start or both to your package json,
"scripts": {
"test": "node mytestfile.js",
"start": "node ./myfile --param1 --param2"
},
and run npm run test or npm run start which can execute the script file. this way you can even pass parameters to the script.
or the elegant child_process way,
const { exec } = require("child_process");
exec("node myfile.js", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
stderr and stdout will show the progress of the script as you build further.
hope this helps.

Run Linux command from Angular 4 component

Requirement is to fetch the output of a shell script's after running it from the Angular 4 component at the beginning during compilation i.e. just before the website is launched. I have already gone through the threads in stackoverflow i.e. 49700941 and 41637166.
From the first thread i tried to use the below code, but getting error:
Module not found: Error: Can't resolve 'child_process' in 'app/component ...'
const exec = require('child_process').exec; // Can't resolve 'child_process' error coming from this line
exec('/home/myDir/init_setup.sh', (err, stdout, stderr) => {
if (err){
console.error(err);
return;
};
console.log(stdout);
console.log(stderr);
/**
remaining logics
*/
});
Please let me know if I need to import some library explicitly or not to avoid this error.
The modern browsers opens the webpage in isolated sandbox so they have have no access to clients' computers.
Imagine the damage that could be done if a black hat could run batch script on computer that opens his webpage.
The only way to run the script is to run the desktop application on client's machine.
The example code you provided is Node.js code, the desktop framework that user have to install on his machine and run the code intentionally. There's (fortunately!) no way to run it remotely via webpage.

Run another Node JS file in command prompt without closing and restarting it

Being New in NodeJS Whenever I run my node JS file in command prompt. I have to close the console window and type the same procedure again. is there any way that i have to not run again and again command prompt.
Use ctrl+c in command prompt and you can use your same command prompt window to run the same program.
For example there are two node.js files.
nodeOne.js
nodeTwo.js
If, these nodes need a port to run, configure both on different port numbers.
e.g. 8080 for nodeOne.js and 8081 for nodeTwo.js
Now, open two instances of 'cmd' and execute both nodes separately.
I'd use "start node whatever.js".
That'll open it up in a separate window, which you can kill whenever and just hit Up arrow on the original cmd window to run the same command.
On the off chance, you're killing the original cmd just to restart node, ctrl-c a couple times should kill it, shouldn't it?
Then up arrow and you've got the last command again.
This could use a little more context, but I assume you are running something like this on the command line:
node my_file.js
Or just:
./my_file.js
Whenever you write command line scripts like this, it's good to call process.exit to tell Node when you're done. So, for example:
#!/usr/bin/env node
function myFunction () {
// Do some stuff that takes a while...
return Promise.resolve()
}
myFunction()
.then(result => {
console.log(result)
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
})
And if your script is just hanging and not stopping, you can always hit Ctrl + C to abort.

automatically input to terminal, from nodeJS?

This may sound rather crazy,
but I cannot find a solution to automate taking screenshots on a cordova application.
I know I can take and save a screenshot of the emulator using
adb shell /system/bin/screencap -p /sdcard/screenshot.png
adb pull /sdcard/screenshot.png screenshot.png
in the terminal, my crazy question is.
Can I trigger these commands from a nodeJS automation script? or is this too far fetched and a terrible idea?
Simplest way to execute terminal commands from node.js would be to use child_process.exec
const exec = require('child_process').exec;
exec('adb shell /system/bin/screencap -p /sdcard/screenshot.png', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
The built-in child_process module in node.js provides many functions such child_process.spawn, child_process.exec, child_process.execFile which can be used to execute terminal commands or other script files (.sh, .bat).

Resources