Is there a way to launch a terminal window (or cmd on Windows) and pass/run a command?

江枫思渺然 提交于 2020-01-12 08:39:07

问题


Question

Is it possible to do the following?

  • open a new cmd.exe or terminal (on MacOS / Linux) window

  • pass / run a command, e.g. cd <path>

Problem

I can open cmd by running this command:

"$electron.shell.openItem('cmd.exe')"

But shell.openItem doesn't allow to pass the arguments / commands.


I tried using child_process but I couldn't make it work at all, it doesn't open a new terminal window:

const { spawn, exec } = require('child_process');
spawn('C:/Windows/System32/cmd.exe');

I also tried running the following command, but still nothing:

spawn( 'cmd.exe', [ '/c', 'echo ASDASD' ], { stdio: [0, 1, 2] } )

The only possible solution that I see is to create a command.bat:

start cmd.exe /K "cd /D C:\test"

And then use openItem:

"$electron.shell.openItem('command.bat')"

But that would only work on Windows


回答1:


Here is a working example showing how to open a Terminal window at a specific path (~/Desktop for instance) on macOS, from a renderer script:

const { app } = require ('electron').remote;
const atPath = app.getPath ('desktop');
const { spawn } = require ('child_process');
let openTerminalAtPath = spawn ('open', [ '-a', 'Terminal', atPath ]);
openTerminalAtPath.on ('error', (err) => { console.log (err); });

It should be easy to adapt it to any selected atPath... As for running other commands, I haven't found a way yet...

And here is the equivalent working code for Linux Mint Cinnamon or Ubuntu:

const { app } = require ('electron').remote;
const terminal = 'gnome-terminal';
const atPath = app.getPath ('desktop');
const { spawn } = require ('child_process');
let openTerminalAtPath = spawn (terminal, { cwd: atPath });
openTerminalAtPath.on ('error', (err) => { console.log (err); });

Please note that the name of the terminal application may be different, depending on the Linux flavor (for instance 'mate-terminal' on Linux Mint MATE), and also that the full path to the application can be explicitly defined, to be on the safe side:

const terminal = '/usr/bin/gnome-terminal';

HTH...




回答2:


Solution

I finally found a way to do it on Windows:

var child_process = require('child_process');
child_process.exec("start cmd.exe /K cd /D C:/test");

Notes

  • You have to add the word start to open a new command window
  • Instead of cd /D C:/test you can specify any other command, e.g. python
  • /D is to make sure it will change the current drive automatically, depending on the path specified

  • /K removes the initial message

  • Don't use execSync it will lock the app until the terminal (command prompt) window is closed

As for MacOS, looks like it's possible to do with osascript

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'



来源:https://stackoverflow.com/questions/50507531/is-there-a-way-to-launch-a-terminal-window-or-cmd-on-windows-and-pass-run-a-co

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!