Max4Live + Javascript + Reading/Writing files
Hey,
I've a javascript running in a Max4Live device that reads/writes to a JSON file. Obviously for portability I want to keep the json filename and path relative to the current device path so I open it with:-
f = new File("data.json", "write", "TEXT");
Unfortunately it gets seems to get a bit confused and starts reading and writing to the Application folder (/Applications/Max 6.1)...
Is there anyway to restrict it relative to the Max4Live device, or get the absolute path properly for the current Max4Live device?
Thanks,
Cárthach
To add further, the problem seems to be inconsistency with the relative path between the Max Device in the Ableton window compared to when it's opened via the Edit Button. Is there any way to enforce some common relative path space between the two I wonder (specifically for Javascript file resources).
Cheers,
C
In theory you can get the path of your patcher with this.pathcher.filepath
. Within M4L though, when your device is open in the editor it will output "/" instead of the absolute path. When not open in editor it outputs the full pat name of your device.
I observed though that it doesn't work when the device is frozen. I believe this might be a bug. (Also I find the behavior of sending the root directory when opened in the editor not coherent... ).
What does work is putting a [thispathcher] in your device and getting the path of your device using the "path" message. Same behavior inside the editor as this.pathcher.filepath
, but it will give you the correct path even if the device is frozen.
It's more a workaround but you can send the absolute path from [thispatcher] to jour [js] when the device is loaded [live.thisdevice] and store it in a variable for later.
Cheers, Jan
Cheers Jan,
No luck either, I get the "/" with thispatcher as well. GRRRR!
C
Even when the device is not opened in the editor? Just for curiosity: which platform and Max/M4L version you are working on?
My sincere apologies, it does work I hadn't it set properly...
Unfortunately it still doesn't solve the issue when the patch opens in the editor view it creates a new data.json file in the Application folder and now there are two competing files,... Very frustrating.
C
Yupp it is! I'd consider it a bug (also in regard to the inconstant behavior between JS and thispatcher.) I just have submitted a bug report.
Well ,as a workaround, while developing your device, you can check in JS if the patcher is locked (this.patcher.locked) and replace the path with a hardcoded one if it not locked. It will still create the file in the wrong location when when the device is opened and locked, but might help nevertheless.
When not open in the editor the state of your patch will be always locked (even if you saved and closed it unlocked).
J
Nice one!
Thanks for your help Jan, and thanks for submitting the bug.