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!
- 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.
- 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:
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 IDconst { test1, test2 } = require('./RequireModule'); test1("hi ", 4);
In this instance, the script that imports functions would look like this:
Note that using "require" from another package will show a red underline code highlight, but it will still run just fine.const { test1, test2 } = require('user:adfjklaf0lkjaflk:adfafkjhafkjhaf'); test1("hi ", 4);
Chad Wahlbrink @Chad2024-06-10 20:11:24.340Z
Lol, @Kitch beat me to it! šŖšŖ
- 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. - In reply toJack_Greenā¬:Jack Green @Jack_Green
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!
Jack Green @Jack_Green
Figured it out it was a small error in the script I was requiring
Chris Shaw @Chris_Shaw2024-06-11 14:15:29.240Z
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
Jack Green @Jack_Green
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 :)