No internet connection
  1. Home
  2. How to

get currently running Command Id

By samuel henriques @samuel_henriques
    2021-06-12 20:46:10.416Z

    I'm wondering if there is a way to get the information from soundFlow's log into a script.

    The idea would be to have a run forever script that knew what script has just started.

    I can see from the log, as soon as I run a script I get this information:

    12.06.2021 21:28:11.29 <info> [Backend]: >> Command: test script [user:ckjuddk090000j810cmjoyrw6:ckpkzyem30000lx10crz1xzrq]

    Any idea how to get this information?

    -Command: test script and
    -script Command Id

    Thank you so much

    • 13 replies
    1. Hi Samuel,

      You shouldn't make scripts relying on reading from the log file as the format of the log file can change without notice.

      Why would you need a runforever script to know which other scripts are running? It would be easier to help if you describe the overall workflow you're trying to achieve.

      1. samuel henriques @samuel_henriques
          2021-06-13 00:04:45.683Z

          Hey Christian,
          Yep, it makes sense.
          And I figured after more testing that it wouldn't help, since whatever I wanted to do with the information would pause while the script is running.
          I'm figuring out a way to have my surface change for whatever script is running in a way I don't need to change the scripts much.
          I'm using it for a few buttons only.

          So far I created a surface for each of the buttons I want to have this behaviour. 4 so far.
          I'm reusing a function that will open the surface relative to that button at the start of the script and it's working, even when one script is triggering the other.

          What I'll have to figure out now, is how to go back to the default/main surface if the scripts stop or are interrupted before the end of each script. It's not that hard to change because the scripts only have one start, but might have many ends.

          Thank you so much.

          1. Hi Samuel,

            You should use a try finally block for something like this.

            try {
                //Switch to new surface
            
                //Do the rest of the script
            
            } finally {
                //Switch back
            }
            
            1. samuel henriques @samuel_henriques
                2021-06-13 13:08:11.261Z

                Hi Christian,

                I ended up using this function, witch runs at the start of every script for this thing:

                function dynamicSurface(currentScript, event) {
                
                    if (event != null) {
                        globalState.scriptInitiator = event.fullInstanceId.split(":")[3].split(".").slice(0, -1).join(".")
                    }
                
                    if (globalState.aScriptIsRunning === "stop") {
                
                        sf.surfaces.get('user:ckdsx2kse0001rp10kif21fs8:ckizyrb6j00009a10tqylbf47').open({
                            device: sf.devices.mobile.getByDeviceId(globalState.scriptInitiator)
                        });
                        globalState.aScriptIsRunning = undefined
                
                    } else {
                        sf.surfaces.get(currentScript).open({
                            device: sf.devices.mobile.getByDeviceId(globalState.scriptInitiator),
                        });
                    }
                }
                
                
                module.exports.dynamicSurface = dynamicSurface;
                

                I'm calling it dynamic, but I know there is probably a proper way to to this. :)

                I call it like this:

                where 'user:ckdsx2kse0001rp10kif21fs8:ckpu5rivb0003oh10l7leoueg'is the surface relative to the script calling the function.

                // @ts-ignore
                const { dynamicSurface } = require('package:ckp2nn9bh0007jd10lmv2plbi');
                dynamicSurface('user:ckdsx2kse0001rp10kif21fs8:ckpu5rivb0003oh10l7leoueg', event.surface)
                
                

                and then at the end of each I run it again like this:

                
                globalState.aScriptIsRunning = "stop"
                dynamicSurface()
                

                I'm using the event to get the device that started it. I have two devices and i'm trying on every script that changes surfaces, to make it always know witch device is calling the change. That way it doesn't matter witch one I used to call, it will go back to the correct one.

                here's a video:

                https://we.tl/t-Qf1iWaLgFd

                1. samuel henriques @samuel_henriques
                    2021-06-13 13:36:04.844Z

                    Of course, there is no practical reason to do this other than being pretty cool to see it change!
                    šŸ˜‚šŸ˜‚

                    1. samuel henriques @samuel_henriques
                        2021-06-13 23:17:33.795Z

                        I didn't know you could use gifs as images on the buttons,

                        check this out:

                        https://we.tl/t-rxbPhu0GFJ

                        1. Pretty cool stuff you got going on, @samuel_henriques! I'm definitely bookmarking this post for future reference.

                          Also, love that you're using // @ts-ignore. I just discovered it a few days ago and can finally get rid of that pesky syntax error on imports.

                          1. @raphaelsepulveda, please note that while Samuel's code works, I wouldn't recommend the pattern he's using here for direct copying.

                            The dynamicSurface function has a number of issues:

                            • Its name doesn't clearly indicate what it's doing
                            • The 1st parameter is really the ID of a surface, not of a script
                            • The 2nd parameter is really a surfaceInstance, not an event
                            • It's using fullInstanceId to get the device instead of directly using the device property of the AxSurfaceInstance
                            • It's using a globalState variable that isn't strictly necessary (scriptInitiator)
                            • It's using another globalState variable that should be a normal parameter for the function (or rather, it should be using callback syntax via try/finally)
                            1. Something like this would be a better pattern (not tested):

                              
                              /**
                               * @callback Callback
                               */
                              
                              /**
                               * @param {Object} args
                               * @param {string} args.surfaceId
                               * @param {AxSurfaceInstance} args.currentInstance
                               * @param {Callback} args.callback
                               */
                              function withSurface({ surfaceId, currentInstance, callback }) {
                                  //Store the device we're being called from
                                  let originalDevice = currentInstance.device;
                              
                                  //Store the current surface
                                  let originalSurface = currentInstance.surface;
                              
                                  try {
                                      //Try opening the requested surface
                                      sf.surfaces.get(surfaceId).open({
                                          device: originalDevice,
                                      });
                              
                                      //Run the callback
                                      callback();
                                  }
                                  finally {
                                      //Restore the original surface on the device
                                      originalSurface.open({
                                          device: originalDevice,
                                      });
                                  }
                              }
                              
                              function main() {
                                  //Put the main code of your script here
                              
                                  alert('Something...');
                              }
                              
                              withSurface({
                                  surfaceId: 'user:ckdsx2kse0001rp10kif21fs8:ckpu5rivb0003oh10l7leoueg',
                                  currentInstance: event.surface,
                                  callback: () => main(),
                              });
                              
                              
                            2. In reply tochrscheuer⬆:
                              samuel henriques @samuel_henriques
                                2021-06-14 07:15:59.311Z

                                Yep, I agree.

                                1. samuel henriques @samuel_henriques
                                    2021-06-14 10:21:26.549Z

                                    Thank you for your time Christian,
                                    I've cleaned it more, fixing some of the issues you described, it certainly made it clearer, and reduced some of the confusion I created to get to a decent result.
                                    These scripts are already a bit complicated, and for something plainly aesthetical I'm not going to mess with them, maybe in the future when I re-write them with better coding, I'll change it.

                                    is there a way I can map what surface is running on what device?

                                    1. You can't currently read the state of devices and which surfaces are running, but you can get information about the surface instance that a button press came from (see my code above)