Rather than have the macro in an external .JS file, it's better to just add the code to your story's JavaScript section. That will solve all of the above problems.
However, if you still want/need to load an external .JS file, then the following code should help:
if (window.hasOwnProperty("storyFormat")) {
// Change the following line to the path where the HTML file
// is located if you want to run this from inside Twine 2.
setup.Path = "C:/Games/MyDirectory/"; // Running inside Twine 2
} else {
setup.Path = ""; // Running in a browser
}
setup.JSLoaded = false;
importStyles(setup.Path + "jquery-ui.css");
importScripts(setup.Path + "jquery-ui.js")
.then(function() {
setup.JSLoaded = true;
}).catch(function(error) {
alert("Error: Could not find file 'jquery-ui.js'.");
}
);
IMPORTANT NOTES: 1.) You will need to change "C:/Games/MyDirectory/" to the directory where the HTML file should be. 2.) The importScripts() function doesn't trigger immediately, so you may want to check the setup.JSLoaded variable to determine if the JavaScript has loaded successfully yet or not. 3.) Due to browser security reasons, the CSS and JavaScript files should be in the same directory as the HTML file, otherwise it may not work in some browsers. 4.) External files will need to refer to the SugarCube object to work (e.g. "SugarCube.Macro.add(...);").
The above code loads in the CSS and JavaScript for jQuery UI, but you can just change the filenames to load other files. The first part of the code makes sure that it looks for the files in the correct path, depending on whether you're running it inside Twine 2 or not.
Hope that helps! :-)