Using launchbrowser to run batch / .bat scripts on Windows

Chloe Alexandra Thompson's icon

The documented use case of ; max launchbrowser is to open URLs, you can also use this message to open folders or files from Max patch. This can prove helpful if you want to use Max as a means of control and trigger program scripts to run through buttons or other objects in your UI.

I am not a Windows expert, so when it came time to write some scripts to remotely access and control RaspberryPi’s and networked controlled power strips through a Max patch I went looking for answers as to why using the [ ; max launchbrowser ] message wasn’t working as I expected. Here’s an issue I found in troubleshooting / learning to integrate batch scripts with Max, but when I searched in the forum weren’t clarified.

You explicitly need to begin your .bat script with the method “start” to open a console menu. Even if you write the script and it works normally when you manually click it or schedule the script, to trigger the console window to open and actually run your working script through Max start needs to be the first word in the script. If you do not begin your script with start you will get an error that the program is not executable.

I found a post in the forum where people were using the tosymbol object, and assigning the file to open and program to open it with separately. This solution did not work for my use case, but once my script was written in a way that triggered the command line to open, I was able to use the launchbrowser command as expected, and avoid adding unneeded code to my patch.

The above was the issue I experienced, and below I’ll write some tips that might seem obvious if you have a programming background, but also may not be explicitly outlined for beginners.

If you are launching scripts, you will want to end your script with a command to close the command prompt / terminal window to avoid a pile up of windows. This is a great step to do after you have finished debugging.

If you are launching multiple scripts from Max you will want to add delays to allow enough time to run through the first script before opening the next.

Your console window will launch from the directory your Max patch is on (IE: if your patch is located on the desktop it will run on the desktop). While part of how you use launchbrowser is through setting your directory, if you are using a cascading / nested script (batch triggering a second script or passing keys / a command stored in another file), then you will want to pop all cascading files into the same directory folder as the Max patch for ease of use (you could try defining this in your file path preferences, but as the command line program is what is doing the work I have not tested this as this does not seem to apply).

Let me know if you have found this helpful, or if this sort of thing has come up for you!

11OLSEN's icon

Hi, I want to add:
I never used "start" at the beginning of the batch file and it was always executed by launchbrowser.?
For me, the console window opens and closes automatically. "start" forces it to stay open after execution.

If it's a small script, you don't need a premade file, you could create, save and execute from inside Max.
"del %0" at the end will tell the script to delete it's file at the end.
See this example to show a file in explorer with the help of a batch:

Max Patch
Copy patch and select New From Clipboard in Max.

Btw you can also run vb-scripts in the same way on Windows.

I prefer the [shell] object to run scripts. With it you can send a result back to Max. Working with delays is not reliable.

Chloe Alexandra Thompson's icon

All amazing information! I don’t regularly use Windows, would classify myself as more than a beginner, but not a super user or Windows based developer at all… I typically use shell on Mac, and would also more typically write a node / py program for this type of thing if I had a whole dev environment set up (weird use case).

You use shell on windows? I found information that led me to believe that this wouldn’t work on Windows… if this is so I will TOTALLY find those posts and we should correct them.

Have you ever successfully used plink / putty from a .bat file with launchbrowser?

Was definitely getting a “can not execute” error sending plink commands which seemingly only resolved through adding start to the beginning of my file… which defined which program to open the script in. Maybe my finding was plink specific, but also applied to another file in which I was using a different script… Could be a timing issue if the window is automatically closing before the process is complete (there are sleep commands in my scripts to ensure full execution before end…).

Which version of Max are you using btw?

Always so much to learn!

11OLSEN's icon

[Shell] was Mac only in the beginning but is x-platform now, thanks to Jeremy. I'm using latest Max.