Unable to do real simple stuff in Javascript
Hi,
I am just beginning with Javascript (js object) in Max 8 but I cannot find out how to make real simple stuff. I want to create a txt file (myTest.txt) and I want to write a text line to that file ("This is a teststring") and then close it. I've been looking at the documentation (e.g., file object) but am still not able to solve it. Any help greatly appreciated.
Kind regards,
Jan
Have you checked out the File object in the docs?
Yes. So far no success (but I will check again later today).
Kind regards,
Jan
You're always going to get more answers on this forum if you post your patch (copy-compressed) and/or js code as relevant.
Creating a file is not trivial on modern OS because of security settings. Your question might be related.
Many folks, myself included, are finding the best way to deal with file creation is with the Node Script object because the node APIs for filesystem access are standardized and quite easy to use. It does wind up being an async request (you message the nodescript object, then make it reply back with some kind of answer message when it's done), but other than that it's very straightforward. I used this recently in an installation that involved a bunch of file system i/o and it worked great. The main scripting was in scheme (which works basically like the normal embedded JS object) and then I handed file access off to the node script.
HTH!
As Audio Matt said, another other way would be to use JS to send messages to the file object, which I have not done, because the Node method seemed a lot more convenient
Simpler than node (though less portable ;) ) : Use Javascript to script a [text] object, send messages to it and erase it? You may... (I haven't tried this in a while) be able to create an invisible patcher in your javascript and script the text object there.
Yeah that makes sense. The node one is nice if you need to things like check directories or whatever. But it does introduce more patch complexity for sure.
Hi again,
I had a look at node.js and it does look very good (and I plan to take a deeper look into it later). As I am using Ableton it would be good if they include the LOM in n4m too. Anyway, after googling the internet I've come up with a simple js object solution that appears to be working. I guess there are better solutions but just in case any other beginner has the same problem here it is (for M4L):
var patcherDir = this.patcher.filepath.replace(patcher.name + ".amxd", "");
var f = new File(patcherDir + "testFile.txt","write","TEXT");
f.writeline("This is a teststring written from filemgr.js");
f.close();
Kind regards,
Jan
PS Sorry couldn't get the fancy blue code block to work
Here's something you'll want to understand: the LOM object can't be in Node the way it is in Max or JS because it works fundamentally differently from the JS object. It's running a separate process, and under the hood, messages to it are serialized and sent back and forth asynchronously. This is what I meant by the additional patch complexity. It's great for giving us access to Node libraries, but it really is a compliment, not a replacement, for the JS object.
Thanks Iain,
although not understanding the details or consequences right now it was mentioned in one of the tutorial videos. If I understand it correctly this is, for example, also why the script start/stop has to be used. Hope to understand more of the details as I slowly progress.
Kind regards,
Jan
yes correct, the start and stop fire up the node process, and the node script object takes care of serializing the input messages and sending them over the network.