Running MacOS shortcuts shell command from node script
Asked Answered
E

1

6

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!

Egret answered 26/1, 2022 at 22:22 Comment(0)
E
3

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!

Egret answered 13/2, 2022 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.