2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Max/MSPで外部プロセスを実行する(Windows/Mac)

Posted at

aka.shellshellでもよいのですが、Windowsでも使用したい事があり、mxjで書くようにしました。

  • 標準出力はそのままmax consoleへ垂れ流しています。
  • 環境変数は特に設定していません。(必要であればProcessBuilderのenvironmentに設定すればよいかと)
  • 動けばいいで書いてるので何かあったらすみません。。 :bow:

mxjの実装

import com.cycling74.max.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class MxjExec extends MaxObject {
    private boolean mIsRunning;
    private Thread mRunThread;
    private Process mProcess;

    public MxjExec() {
        mIsRunning = false;
        declareIO(1, 0);
    }
    public void exec(Atom[] args) {
        if (mIsRunning) {
            post("Running");
            return;
        }

        String[] cmds = new String[args.length];
        for (int i = 0; i < args.length; i++) {
            cmds[i] = args[i].getString();
        }

        try {
            mIsRunning = true;
            ProcessBuilder pb = new ProcessBuilder(cmds);
            pb.redirectErrorStream(true);

            mProcess = pb.start();
            post("Execute Process");
            mRunThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    String str;
                    BufferedReader br = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
                    try {
                        while((str = br.readLine()) != null) {
                            post(str);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mIsRunning = false;
                    }
                }
            });
            mRunThread.start();
        } catch (Exception e) {
            mIsRunning = false;
            e.printStackTrace();
        }
    }

    public void stop() {
        try {
            if (mProcess != null)
                mProcess.destroy();
            mProcess = null;
            mIsRunning = false;
            post("Stop Process");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void notifyDeleted() {
        stop();
    }
}

ビルド後にclassファイルをcodeディレクトリに入れて下さい。

リポジトリ

#使い方
execから始めればプロセスが実行されます。
私がMacでよくやるのはパッチを起動しているときは、以下のようにcaffeinateコマンドでスリープにならないようにしています。
screenshot.png

Windowsでもexec echo helloみたいな感じで実行されます。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?