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

前端 未结 2 957
清酒与你
清酒与你 2021-02-10 13:36

Question

Is it possible to do the following?

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

  • pass /

2条回答
  •  时光说笑
    2021-02-10 14:05

    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'

提交回复
热议问题