How execute bat from mac? Is it possible to rewrite it to python or bash script?

I have a problem with .bat file. There is a .bat file that starts the server. But on a Mac OS system, this is impossible. Are there any options to rewrite it to python or bash so that it would be possible to start from MacBook?

This is .bat file:

echo start web server..
start cmd /k node webServer.js
echo start chrome..
start chrome.exe /k http://localhost:8080

Thanks for helping!

2 answers

  • answered 2020-05-29 10:15 devfoFikiCar

    Ok, so it would be better to use bash scripts. They are much more powerful than bat and they work on all Unix like OS-s (Linux, Mac ..) and can work on windows with some modifications. This will show you how to run node:

    Running node from a bash script

    This will show you how to run the app:

    https://askubuntu.com/questions/682913/how-to-write-shell-script-to-start-some-programs

    Also, look at this link for an introduction to bash, it is a good thing to know it:

    https://linuxconfig.org/bash-scripting-tutorial-for-beginners

    Also on the https://www.mac-forums.com/forums/switcher-hangout/302162-execute-bat-file-mac.html you can see how to run it on mac but as they pointed out there it doesn't work 100%.

    Edit1: This is the code:

    #!/bin/bash
    
    echo "Star server .."
    node webServer.js
    echo "Open chrome"
    open http://localhost:8080
    

    For node just add the path to file, like you would usually run it. For last line it opens default browser with link.

  • answered 2020-05-29 11:52 hoefling

    Here's a Python example that is cross-platform (unless you don't have node in PATH) and using only the standard library:

    # client.py
    
    import subprocess
    import webbrowser
    
    
    if __name__ == '__main__':
        try:
            server_proc = subprocess.Popen(['node', 'webServer.js'])
            webbrowser.open('http://localhost:8080')
            server_proc.communicate()
        except KeyboardInterrupt:
            server_proc.terminate()
    

    Note, however, that webbrowser.open will open the browser that is set as default, so it could be Safari or whatever. If you want to specifically open Chrome, you will have to pass the full path to the executable (or modify your PATH env var). Example:

    # client.py
    
    import os
    import subprocess
    
    
    if __name__ == '__main__':
        try:
            server_proc = subprocess.Popen(['node', 'webServer.js'])
            chrome_exe = os.path.join('/', 'Applications', 'Google Chrome.app', 'Contents', 'MacOS', 'Google Chrome')
            subprocess.Popen([chrome_exe, 'http://localhost:8080'])
            server_proc.communicate()
        except KeyboardInterrupt:
            server_proc.terminate()