fs.mkdirSync(dir); Node child process quit, will restart
Hi,
I try to make a dir by using node.js, but if I start the script and trigger the function, there is node.script message in the console: Node child process quit, will restart.
The code looks like this:
const Max = require('max-api');
const fs = require('fs');
Max.addHandler("bang", () => {
// directory to check if exists
const dir = '/Library/Application Support/MyApp/';
// check if directory exists
if (fs.existsSync(dir)) {
Max.post('Directory exists!');
} else {
Max.post('Directory not found.');
fs.mkdirSync(dir);
}
});
The fs.existsSync(dir) works as expected.
Any idea, please?
Thank you
nothing to do with node.js, but do you have rights to write into
/Library at all ?
I am pretty sure not.
Then there is working mkdir example n4m.max-fs.js in node package.
even better use shell object and avoid using node at all which also means no need for CEF framework, in case you build standalone.
Would drastically reduce app size.
Hi,
Yes, I have rights to write on the the /Library.
I have just installed the shell extension. This is a great solution, thank you!
So then if I want to test, the folder exists, the message would look like this?
echo -d "/Library/Application Support/MyApp/"
Or am I wrong?
that won't do.
You don't need to check if folder exists,
mkdir will throw error if it exists and won't overwrite it.
but if you want to know if it exists, no need for shell,
you can use absolutepath and sel notfound to bang if
not found, or output full path if found.
similar find message to shell would output path or
"No such file or directory"
Remember - shell needs slash based path.
Great, thank you very much for the respond.
Hopefully the last question (sorry)... How could I do the mkdir as sudo, please?
option 1
echo 'yourpass' | sudo -S mkdir ~/Desktop/AAA
( that | is vertical bar, ascii 124)
option 2
sudo rm -rf ~/Desktop/AAA
stdout posts "Password:"
you send
penter 'yourpass'
Please be carefull with sudo and rm ...
Sometimes one can misstype something, or by accident
sudo rm -rf / bla bla
that space after slash would cause deletion of all files on all volumes
Not sure what's your intent here but you might consider using the user's Application Support directory, rather than the OS global one, which would avoid the permission issues you are seeing.
You can use the corresponding process.env values for that or just use os.homedir(), which wraps the functionality in a cross-platform fashion for you:
const { homedir } = require("os");
const { existsSync, promises: fsPromises } = require("fs");
const { join } = require("path");
const { mkdir } = fsPromises;
Max.addHandler("bang", async () => {
// directory to check if exists
// might need a slightly different path when running on Windows
const appSupportDir = join(homedir(), "Library", "Application Support", "MyApp");
// check if directory exists
if (existsSync(appSupportDir)) {
await Max.post('Directory exists!');
} else {
await Max.post('Directory not found.');
await mkdir(appSupportDir);
}
});
Hi,
Thank you very much for your reply.
More from the background, my application is going to talk to 3rd party app. Actually, I don't mind, if I will read/write from/to the system Library/Application Support/MyApp or user ~/Library/Application Support/MyApp, because the 3rd party app reads the data from both of them.
My goal is:
- Test if the folder exists already. If not, create the folder in question.
- Test if the file (I'm going to store) exists already.
- Store the file to the folder.
I don't mind, if I will use the node.js or the shell object.
Btw, the application is going to be Mac only.
Thanks a lot!
Hi Martin,
sure, happy to help. The benefit of using the user's App Support directory is that you will be able to avoid the permissions issue, the sudo usage and any passwords entirely. Furthermore in case of multiples users on the machine each of them would / could have separate data in their distinct App Support directory rather than the shared system library one.
Whether you use the built-in [node.script] or add the [shell] extension might just be a question of personal preference. Not sure if you intend to do more than this relatively small setup task as part of your application - just personally speaking I prefer the x-platform utilities in Node.JS when dealing with filepaths and when interacting with the filesystem but again that's a personal choice.
Hope any of this helps.
Cheers
Florian
Mac only or Win/Mac, path to write is easy to detemine and form,
Max was originally developed as Mac application, and we profit from that also on Windows,
for example message write ~/Desktop/bla.txt to text object
writes the file to users desktop folder no matter on Win or Mac.
~/ to absolutepath returns homedir
On Mac, Max and created Standalones use ~/Library/Application Support to write preferences etc.
On windows it is also hidden in
~/AppData/Roaming/Cycling '74/
One can only get rid of that location by hacking EXE file
(if one wants own Standalone Preferences somewhere else)
So far about user path.
My few cents - using node.js means also need to use CEF framework.
Which adds to app size (600 MB more) and slows the things down if it is for Standalone App.
All but folder creation can be done with vanilla max objects.
If you want to check if "~/Library/Application Support/MyApp/myfile.txt"
exists, simply send that message to absolutepath object, and if not found,
proceed with creating folder and file.
for fun try message to shell :
mkdir ~/Library/'Application Support'/MyApp, echo 'Bungalow on the Sunny Beach' > ~/Library/'Application Support'/MyApp/myfile.txt
and check the destination folder and file.
-------
Without any 3rd party stuff, one can also use script to create folder.
write into plain text file :mkdir ~/Library/Application\ Support/MyApp ;
killall Terminal
save as whatever.command
make executable using chmod +x
from Max run it using message
;
max launchbrowser file:///path to the command
---
or make applescript app.
do shell script "mkdir ~/Library/Application\ Support/MyApp"
same thing on windows using cmd, batch files, etc