No internet connection
  1. Home
  2. How to

How to trigger a command whenever Pro Tools transport is stopped

From https://forum.soundflow.org/-795/how-to-make-commands-wait-to-trigger-until-pro-tools-transport-is-stopped#post-3
CC @Graham_Archer

Hey, this is really cool. I think it could be useful for an auto talkback. Can I ask what trigger did you use to have it running constantly? I tried:
type Application Trigger, When these apps Pro Tools, Event Activate
but it only seems to work when I switch apps and put Pro Tools in focus. I'd like to just have it running all the time if possible.

Solved in post #2, click to view
  • 1 replies
  1. To make the code run forever, let's take the code from here:
    https://forum.soundflow.org/-989#post-7

    In this script I'm checking whether or not Pro Tools is playing and if we discover it stopped then it displays "Pro Tools stopped". You should insert your own code there.

    You should add an Application Trigger to run this script when Pro Tools is activated.

    var lastIsPlayingValue;
    
    function main() {
        var isPlaying = sf.ui.proTools.isPlaying;
        if (isPlaying === lastIsPlayingValue) return;
        lastIsPlayingValue = isPlaying;
    
        if (!isPlaying) {
            //Insert your code to run here
            log('Pro Tools stopped');
        }
    }
    
    function runForever(name, action, interval, timeout) {
        var now = (new Date).valueOf();
        if (now - globalState[name] < timeout) throw 0; //Exit if we were invoked again inside the timeout
    
        globalState[name] = now;
        sf.engine.runInBackground(function () {
            try {
                while (true) {
                    sf.engine.checkForCancellation();
                    globalState[name] = (new Date).valueOf();
    
                    action();
    
                    sf.wait({ intervalMs: interval, executionMode: 'Background' });
                }
            } finally {
                globalState[name] = null;
            }
        });
    }
    
    runForever("isPTStoppedRunning", main, 500, 5000);
    
    ReplySolution