September 9, 2009

Including Often Used Library Files

Filed under: Scripting — Marcus Tettmar @ 3:49 pm

If you create lots of Macro Scheduler macros you probably have pieces of code you use frequently. You might have blocks of VBScript or subroutines you often use in your macros. Copying and pasting code like that into your scripts is time consuming and also means if you ever need to change that code you’ll end up having to edit all your macros. A better way to use such “library code” is to Include it using the, yes, Include function:

Include>%SCRIPT_DIR%\MyLib.scp

This function includes the content of MyLib.scp in your macro.

Some people seem confused about how this works. It’s simpler than you might realise. It literally sucks in the code in place of the Include line and then executes it. When the script runs that Include line is actually replaced with the code in MyLib.scp. So the code in MyLib.scp actually becomes part of the overall macro.

One way to visualise how it works is to step through an Inlude line in the debugger. You’ll see the debugger pull the code into the script. Here’s a video to show you what I mean:

In the above example I’ve kept the include file small so that the entire script still fits in the editor. You’ll see the code jump into the editor and then disappear when the script ends or is stopped.

With Include you can reference commonly used functions and code. Some projects may involve lots of scripts that all use some core functions which you would put into an Include file.

Include is different to the Macro command which just “calls” another script file. With the Macro command that other script is run in isolation, almost like another process. The code isn’t shared between the two scripts. Include doesn’t just run the code, it pulls the code into the current macro so that it becomes part of it and is executed at that point. It remains part of the script until the script terminates. So the code could be subroutines or dialog blocks or VBScript blocks or anything you need the main script to reference.