How to read/write to a text file in a Standalone Application

jpenrose's icon

Hello,

I'm building a standalone application. I need to be able to read and write to a text file for saving information between sessions.

I've tried adding these text files with the "include in build" command, with no luck. I've also tried manually copying/pasting these text files into the Contents folders as well as the Contents > Resources folder. In both cases, the Standalone App is not able to locate these files.

In my code, I'm simply asking the Text object to read and write to these files as if they are local, i.e. I'm not including a filepath (i.e. "write config.txt"). Is the solution that I need a more specific filepath argument for the read/write commands? If so, how should I format this for the Text object inside a Standalone?

Any insights and pointers or links to existing documentation would be most appreciated...!

Thanks,

Joshua

Source Audio's icon

depending on OS, you could get permissions problems when attempting to write into

applications folder.

I would use absolute path to user documents.

Oni Shogun's icon

You can use the method

; max sendapppath [receive port]

to query the directory where the standalone was opened from in order to format absolute paths (instantiate a corresponding receive port). Depending on OS and name of standalone the string concatenation varies, of course, which makes it a bit cumbersome. The name of the standalone, for example, can be queried using the env method on [gestalt] and parsing the dictionary accordingly.

One thing to note, is that imho any files included in the stadnalone upon build are included in the collective and can therefore not be changed, which is why [pattrstorage] offers a fileusagemode.

I have built abstractions to handle these circumstances as wrappers, including preference file handling, exporting standalones with file-transfer-scripting support, etc., to simplify and automatize these steps; that being said, not every function listed in Controlling Max with Messages actually works as expected, but hey. They aren't perhaps compatible with public use yet, but if you fancy, you can give them try… let me know.