No internet connection
  1. Home
  2. How to

stop script when using sf.soundflow.runCommand

By samuel henriques @samuel_henriques
    2021-05-14 22:50:20.365Z

    Hello everyone,

    I'm using a script that calls other scripts using sf.soundflow.runCommand. But one of them need to be able to stop everything. I tried throw 0, it will stop the running script, but will continue the "parent script".
    Then I thought I would just use sf.DUMMY.stopAll(); but it's meant to run with a trigger.

    I could do a globalState.cancel = true and read it on the parent, but I'm wondering there could be something smarter than this.
    Any ideas?

    // scipt 1 does stuff
    
    
    
    //  run script 2 if I do throw 0 on this one, it comes back to script 1
    sf.soundflow.runCommand({
        commandId: 'id of script 2',
    });
    
    
    
    // script 1 continues
    

    Thank you

    • 13 replies

    There are 13 replies. Estimated reading time: 11 minutes

    1. Hi Samuel,

      In the child script, you could set its return value like this, to some special value, for example -1 to indicate an error:

      event.setResult(-1);
      

      This return value should be readable from the runCommand's "result" output property like this:

      if (sf.soundflow.runCommand({ ... }).result === -1)
          throw 0;
      
      1. samuel henriques @samuel_henriques
          2021-05-15 11:38:09.898Z

          Thank you Christian, this works.

          My problem now is that the child script can run independently as well, and I need to be able to stop it for the same reasons.

          at this time, I can stop child from returning to parent
          or
          stop child when it runs by it self with a throw 0. I can read the return event.setResult(-1); as well, but at some time I need to throw 0.

          Can't have both.

          I'm thinking that if I throw 0 on the child at any time, it doesn't send the event.result back to the parent script.

          Thank you so much.

          1. Just do this in the child script:

            if (.....) {
                event.setResult(-1);
                throw 0;
            }
            
            1. In reply tochrscheuer:
              samuel henriques @samuel_henriques
                2021-05-15 13:11:27.297Z

                I must be missing something,

                with

                if (.....) {
                    event.setResult(-1);
                }
                

                It sends the event back to the parent. But If I add the throw 0 it doesn't.

                1. samuel henriques @samuel_henriques
                    2021-05-15 16:02:30.301Z2021-05-15 16:54:40.037Z

                    Here's what I'm trying.

                    I have a fairly complex workflow that does everything from commit tracks all the way to export clips.

                    (the button names look better on the surface)

                    This is working pretty well, for some time now, I keep tweeting it but it's really stable. And I want to use the scripts alone as well.

                    So Track Commit, organises the resulting clips on the correct tracks, selects the track/tracks that need LUFS and,

                    calls Luffs script, once luffs script is done, it comes back to the Track Commit that calls the Track Name to Clip Name witch renames the clips, goes back to Track Commit,
                    and it calls Export Clips.

                    This might look complicated ( and probably it could be simplified. But I'm doing this as I learn and, anyway, its stable.

                    Now...
                    There are two places on the workflow that I can cancel pro tools. Either during the commit or during the LUFS process.

                    The confirmationDialog is the same for both.

                    It's really annoying when I cancel pro tools and naturaly, soundFlow doesn't know. I never remember that and the scripts continue moving clips about and renaming stuff...yadayaddayadda I keep a STOP ALL COMMANDS handy, but would be cool to get a better solution.

                    So i devised a function I run on both Track commit and LUFS to "know" if I canceled the confirmationDialog.

                    function commitCheckCancelation() {
                        while (true) {
                    
                            // Get Cancel Button position
                            const confirmationDCancelBtn = sf.ui.proTools.confirmationDialog.invalidate().buttons.whoseTitle.is('Cancel').first
                            const cancelButtonPos = confirmationDCancelBtn.position
                    
                            if (sf.ui.proTools.isActive) {
                                if (confirmationDCancelBtn.exists) {
                                    /// Get mouse possition
                                    const mousePosition = sf.mouse.getPosition().position
                    
                                    /// If mouse stops in Cancel btn , presume cancel button was pressed so, throw 0
                                    if (mousePosition.x > cancelButtonPos.x && mousePosition.x < cancelButtonPos.x + 77
                                        && mousePosition.y > cancelButtonPos.y && mousePosition.y < cancelButtonPos.y + 15) {
                    
                                        log("SoundFlow Canceled!")
                                        event.setResult(-1);
                                        //throw 0
                                    }
                                } else {
                                    break;
                                }
                            }
                        }
                    }
                    
                    
                    /// Here is my ok and wait for window to disappear.
                    
                        //Give it a wait to get the confirmation dialog
                        sf.wait({ intervalMs: 1000 });
                    
                        commitCheckCancelation()
                    
                        //Wait for bounce to finish (Wait for bounce dialog to appear, then to disappear)
                        sf.ui.proTools.confirmationDialog.elementWaitFor({ waitType: 'Disappear', timeout: -1 }); //-1 is endless timeout (cancel by Ctrl+Shift+Esc)
                    

                    Witch works, apart from the problem I described on the previous post.

                    Any idea how I could make this work?

                    Thank you so much

                    1. samuel henriques @samuel_henriques
                        2021-05-15 18:04:50.937Z2021-05-26 21:53:05.117Z

                        This works, but I feel like I should try to avoid globalState

                        function buttonCheckCancelation() {
                            while (true) {
                        
                                const foremostApp = sf.ui.frontmostApp.invalidate()
                                //  wake from screensaver if needed
                                if (foremostApp.title.value == 'ScreenSaverEngine') { sf.mouse.scroll() }
                        
                                globalState.commitCheckCancelation = false
                        
                                const dialogCancelBtn = sf.ui.proTools.confirmationDialog.invalidate().buttons.whoseTitle.is('Cancel').first
                        
                                if (dialogCancelBtn.exists) {
                        
                                    if (sf.ui.proTools.isActive) {
                                        // Get mouse position
                                        const mousePosition = sf.mouse.getPosition().position
                                        // Get cancel button position
                                        const cancelButtonPos = dialogCancelBtn.position
                        
                                        //  Cancel btn position must have a value. It can become null if dialogWin closes before this step 
                                        //     and script would get error and fail on cancelButtonPos.x
                                        if (cancelButtonPos &&
                        
                                            /// If mouse stops in Cancel btn , presume cancel button was pressed so, throw 0
                                            mousePosition.x > cancelButtonPos.x && mousePosition.x < cancelButtonPos.x + 77 &&
                                            mousePosition.y > cancelButtonPos.y && mousePosition.y < cancelButtonPos.y + 15) {
                        
                                            log("SoundFlow aborted!")
                                            // this is so script using sf.soundflow.runCommand can be stoped
                                            globalState.commitCheckCancelation = true;
                        
                                            throw 0;
                                        }
                                    }
                                } else {
                                    break;
                                }
                            }
                        }
                        
                        1. There really is no need to avoid globalState - your use of it is fine.

                          1. samuel henriques @samuel_henriques
                              2021-05-15 23:26:22.752Z

                              Cool, Chris, thank you!

                              1. I use and abuse globalState on the regular :)

                                1. On another note - since you're wanting to re-use code across multiple scripts, and that you want the ability for scripts to both be run as individual scripts but also as part of other scripts, which in this case complicates the code paths / control flow - I'd highly recommend that you consider switching to re-using functions instead, and then export them using module.exports:

                                  This would help you control the flow of your code and potentially save a lot of trouble in terms of error handling.

                                  1. samuel henriques @samuel_henriques
                                      2021-05-17 21:30:19.804Z

                                      Nice one, this is pretty cool.

                                      Would it make sense to have a script in the same package with all the reusable functions, and then import from that one?

                                      Thank you so much.

                                      1. Yep.
                                        That's how it's typically done.

                              2. samuel henriques @samuel_henriques
                                  2021-05-26 21:55:26.834Z

                                  For anyone interested in the future.
                                  The original function I posted here had a few problems that I managed to fix.
                                  So far this seems to work well.