Is there a way in a Supercollider source code file to include another source code file from disk, like the "include"-statement does in C++?

I assume there must be a way to do it, but I haven't managed to find any example by googling.

  • Welcome to our site Elias. I'm not sure this is on topic here. While we do allow music software questions it typically isn't this low level.
    – Dom
    Commented Jan 17, 2017 at 1:05

2 Answers 2


Supercollider appears to be able to execute code from external files, though whether this is an include-into-the-current-namespace or something-run-in-a-new-environment I don't know:


A recursive grep for "loadPath" should turn up examples:



// ===================================================================== // FILE: utilityFunctions.scd // =====================================================================

/*This file contains some utility functions.
You can 'import' these functions using executeFile.

*NONE* of the vars in the 'imported' file end up 
in the scope of the file doing the 'importing'

'import' this with something like:
var utils  = this.executeFile(path-to-this-file);

If BOTH FILES are in the same directory you can do this:
var utils  = this.executeFile(Document.current.dir ++ "/utilityFunctions.scd");

Then you can refer to the functions like this:
utils[\fmtInt].(1234567); // this returns "1,234,567"

You could also do this:
var fmtInt = utils[\fmtInt];
fmtInit.(7654321); // this returns "7,654,321"

var fmtInt = { arg num, sep=",", groupSize=3;
  var str = num.asInteger.asString;
  var len = str.size;
  var numSeps = ((len-1)/groupSize).floor.asInteger;
  var firstSep = len - (numSeps*groupSize);
  var stop = len - groupSize;
  var out=str;
  firstSep.forBy(stop, groupSize,
    {|pos, itr|
      var ins = pos + itr;
      out = out.insert(ins, sep);
  out; // return result

var readStr0 = {arg file, max=20;
  var zeroByte = 0.asAscii;  // zero byte
  var str = "";
  block {|break|
    max.do {|i|
      var char = file.getChar;
      if (char == zeroByte) { break.value(999); };
      str = str++char;
  str;  // return result

var functions = Dictionary.new(2);

functions.add(\fmtInt -> fmtInt);
functions.add(\readStr0 -> readStr0);

functions;  // return the dictionary of functions

// ===================================================================== // END OF FILE: utilityFunctions.scd // =====================================================================

// ===================================================================== // FILE: testUtilityFunctions.scd // =====================================================================

// this assumes the test file is in the same dir as the code file
var dir = Document.current.dir;
var name = "/utilityFunctions.scd";
var path = dir ++ name;
var utils  = this.executeFile(path);

var f1 = utils[\fmtInt];
var f2 = utils[\readStr0];
var d1 = f1.def;
var a1 = d1.argNames;

postf("\nTesting the (import of) the functions in file: %\n", name);
postf("utils is: %\n", utils);
postf("f1 should be a Function, it is: %\n", f1);
postf("f2 should be a Function, it is: %\n", f2);
postf("d1 should be a FunctionDef, it is: %\n", d1);
postf("a1 should be the args of f1, it is: %\n", a1);
postln("Check that names did not bleed in from imported file:");
postf("~functions should be nil, it is: %\n", ~functions);
postf("~fmtInt should be nil, it is: %\n", ~fmtInt);
postln("try one of the functions:");
postf("f1.(12345) is: %\n", f1.(12345));
postf("utils[\\fmtInt].(5678) is: %\n", utils[\fmtInt].(5678));

postln("'functions.postln;' should return: ERROR: Variable 'functions' not defined.");


// ======================================================================== // END OF FILE: testUtilityFunctions.scd // ========================================================================

  • Hi, welcome to Music: Practice & Theory. Can you please edit your answer to fix the formatting, and to add a paragraph or two of explanation about what this code is/does. Thanks! Commented Jan 28 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.