No internet connection
  1. Home
  2. How to

Running a script within another script.

By Jack Green @Jack_Green
    2024-06-10 15:59:53.402Z

    So I have a family of scripts that all work together to setup/manage my sessions.

    They all share lots of the same functions. It's becoming a bit of a headache to keep them all updated when I have to change something.

    Im wondering if there is a way to either. Run a script from within another script or run a script from a locally stored file?

    Thanks in advance!

    Solved in post #2, click to view
    • 9 replies
    1. Kitch Membery @Kitch2024-06-10 20:06:39.558Z

      Hi Jack,

      Yes, this can be done using the Export/Require Syntax.

      You can read more about it here.

      Reply2 LikesSolution
      1. In reply toJack_Green⬆:
        Chad Wahlbrink @Chad2024-06-10 20:10:40.087Z

        Hey @Jack_Green!

        Require Syntax #post-7
        ↑ Require syntax is the best way to share functions across scripts and update them from a central place.

        As Christian explains, you can have a script called "RequireModule":

        
        /**
        * @param {string} str
        * @param {number} num
        */
        function test1(str, num) {
            log(str + num)
        }
        
        /**
        * @param {number} num1
        * @param {number} num2
        */
        function test2(num1, num2) {
            return num1 + num2;
        }
        
        module.exports = {
            test1,
            test2,
        };
        

        Then, in a new script within the same package, you can call the functions from the other RequireModule Script like this:

        const { test1, test2 } = require('./RequireModule');
        
        test1("hi ", 4);
        
        Note, that you can also use a script from another package if you use a command ID reference instead of the written path. To do this, select the script with the functions you'd like to export and select Commands > Copy Command ID In this instance, the script that imports functions would look like this:
        const { test1, test2 } = require('user:adfjklaf0lkjaflk:adfafkjhafkjhaf');
        
        test1("hi ", 4);
        
        Note that using "require" from another package will show a red underline code highlight, but it will still run just fine.
        1. Chad Wahlbrink @Chad2024-06-10 20:11:24.340Z

          Lol, @Kitch beat me to it! šŸ’ŖšŸ’Ŗ

          1. In reply toChad⬆:
            Kitch Membery @Kitch2024-06-10 20:18:30.738Z

            Nice one Chad! :-)

          2. In reply toJack_Green⬆:
            Chad Wahlbrink @Chad2024-06-10 20:15:48.135Z

            Also probably worth noting that if you want to call a full script and not just export pieces, you can always use:

            sf.soundflow.runCommand({
                commandId: 'user:adfjklaf0lkjaflk:adfafkjhafkjhaf,
                props: {}
            });
            

            And just replace the commandId prop accordingly.

            1. In reply toJack_Green⬆:
              Jack Green @Jack_Green
                2024-06-11 12:13:00.581Z

                So I tried this out with a test and it worked perfectly but now I seem to be stuck...

                Started trying to move various scripts I have across into this system as it will be so much better for me in the long run. (recently updated protools and they changed Save As to Save Session As and it messed up so many of my scripts!)

                So I have my master package with a sub folder to store all the repeated functions.

                I have this in my functions script linking to various functions inside the script (nothing is red lined so looks ok)

                module.exports = {
                    sessionNameNewVersion,
                    sessionNameTimecode,
                    txtFileContentBuild,
                    saveAndMoveSession,
                    textFileUpdate,
                }
                

                then I have this in the script that wants to call these...

                const { sessionNameNewVersion,
                    sessionNameTimecode,
                    txtFileContentBuild,
                    saveAndMoveSession,
                    textFileUpdate, } = require("./Functions/Save Session Functions");
                

                I get the error

                Command './Functions/Save Session Functions' not found (Save Version: Line 3)

                What silly thing am I missing...

                thanks in advance!

                1. Jack Green @Jack_Green
                    2024-06-11 12:39:51.282Z

                    Figured it out it was a small error in the script I was requiring

                    1. That's something you have to watch for - if there is an error in any of the scripts being required you will get errors
                      Also if you use the command ID for requiring, to get rid of the red underline you just need to add a /* @ts-ignore **/ just above it like this:

                      /* @ts-ignore **/
                      const { getPreferences } = require(`package:cldnudy0h0000hb10nvf28k7i`)
                      

                      This is purely cosmetic

                      1. Jack Green @Jack_Green
                          2024-06-11 16:32:31.523Z

                          Ahh thats useful to know!

                          I missed the (err) on a try/catch block took me ages to spot gradually removing bits of code to figure out what bit was broken!

                          Thanks :)