Running Patch/Standalone in Background
I recently was looking through forum posts about this topic, and since there were quite a few threads, I wasn't sure which one to post on, so I figured I'd start a new one and share what I found.
There are at least three parts to doing this correctly:
1. Create a toplevel patch with the standalone object that will open your actual patch using the 'shroud ' message to pcontrol, and then close itself; such as this one here:
2. When you build your application and collective from the toplevel patch, make sure to include your actual patch and all its dependencies in the build script, because they will not be included automatically.
3. After you have built your application, use the technique found here
or use the program that article refrences here
to prevent the menu bar and application icon on the dock from showing up.
What results is a program that is invisible to the user (aka does not have a menu bar, does not show up on the dock, does not show up as a process under force quit, and does not have a window). It is also impossible for the anything to end the process outside of a program, so you better have a way to stop it when you want it to. For instance, I am using this method to run max as an audio engine for Unity, and here is the C# code I use to start the program and end it (using System.Diagnostics and System.IO from the .Net library, included with Unity).
using UnityEngine;
using System.Diagnostics;
using System.IO;
public class OpenFile : MonoBehaviour {
FileInfo info = new FileInfo("/Users/JohnCassidy/Documents/MaxPatches/BGPatchOpenerApp.app/Contents/MacOS/BGPatchOpenerApp");
Process maxPatch;
// Opens standalone when Unity app starts
void Start () {
maxPatch = Process.Start(info.FullName);
}
// Closes standalone when Unity app stops
void OnApplicationQuit () {
if (!maxPatch.HasExited) {
maxPatch.CloseMainWindow();
}
}
}
It is important that the file path points to the executable, otherwise the app will not close properly.
For windows, all you would have to do is find some Windows specific way to accomplish step 3 (hiding the app from the taskbar), and make appropriate changes to the file path in your program.
If you really want to go one step further, and not even have a visible loading screen for your patch, you can use the techniques referenced in this forum post:
and the included example patch (uploaded as an attachment to preserve patcher inspector properties)
I hope this helps anyone interested in this topic.
Cheers!
-John Cassidy
Apologies about that unity code. Let me see if this works better.
using UnityEngine;
using System.Diagnostics;
using System.IO;
public class OpenFile : MonoBehaviour {
FileInfo info = new FileInfo("/Users/JohnCassidy/Documents/MaxPatches/BGPatchOpenerApp.app/Contents/MacOS/BGPatchOpenerApp");
Process maxPatch;
// Opens standalone when Unity app starts
void Start () {
maxPatch = Process.Start(info.FullName);
}
// Closes standalone when Unity app stops
void OnApplicationQuit () {
if (!maxPatch.HasExited) {
maxPatch.CloseMainWindow();
}
}
}
Hi John
I ran into your post looking for solutions in order to integrate a MaxMSP patch in an app developed within Unity. I am not a programmer, so forgive the naiveness of my question, but is it possible, and would your code work in order to do that?
I iniitally thought of having Max and Unity communicate via OSC, and read about Myu, a interoperability tookit for Max and Unity. But I really have to make sure that we can get a single application at the end of the road.
Here is a link to my post: https://cycling74.com/forums/unity-maxmsp-embedded-in-app/
Thank you.
Coralie