Page 1 of 1

Save script data?

Posted: Tue May 08, 2018 4:59 pm
by dkwroot
Is there a way to save script data directly to a utility file? I'm working on a script where the user can generate new values and I'm trying to think of a nice way for the values to be available cross-project and even be shareable. Right now, all I can think to do is to call to the io and throw up a prompt to push saves to a selected file, but that's not very eloquent and I don't want the user to have to manually find their file every time they start the tool. Any ideas?

Re: Save script data?

Posted: Tue May 08, 2018 7:56 pm
by synthsin75
dkwroot wrote:Is there a way to save script data directly to a utility file? I'm working on a script where the user can generate new values and I'm trying to think of a nice way for the values to be available cross-project and even be shareable. Right now, all I can think to do is to call to the io and throw up a prompt to push saves to a selected file, but that's not very eloquent and I don't want the user to have to manually find their file every time they start the tool. Any ideas?
Yes, you can write to a plain text file, like this:

Code: Select all

	local dir = string.gsub(moho:UserAppDir(), "\\", "/")
	local output = dir.."/scripts/utility/test.txt"
	local file = io.open(output, "w")
	file:write("hello world")
	file:close()
Here I'm getting the user content folder (moho:UserAppDir()) and adding (concatenating "..") the path to the scripts>utility folder and desired file name.
I open the file I named "test.txt" in write mode ("w"), which will create it if it doesn't exist, write to it, and close it.

When you go to alter that file, you can either rewrite the whole file, by repeating the above steps, or you can, for example, append to the end of the file ("a+"), like this:

Code: Select all

	local dir = string.gsub(moho:UserAppDir(), "\\", "/")
	local output = dir.."/scripts/utility/test.txt"
	local file = io.open(output, "a+")
	file:write("\n", "reply")
	file:close(file)
Generally, if it doesn't have to be sharable, a script would just use script preferences to store such values.

Re: Save script data?

Posted: Tue May 08, 2018 8:44 pm
by hayasidist
Scriptdata? http://mohoscripting.com/index.php?show ... er&id=1122

Obviously depends on exactly what you're trying to do, but this keeps the info embedded in the .moho that your script can create / read / update / delete even across different systems where script prefs are not guaranteed to be consistent.

Re: Save script data?

Posted: Tue May 08, 2018 9:28 pm
by synthsin75
Yeah, you could share the values through Moho files, like Paul says.

Re: Save script data?

Posted: Wed May 09, 2018 12:10 am
by dkwroot
Thanks guys! I already looked into the scriptdata, but it wasn't quite what I was looking for. The method Wes pointed out looks like it'll work perfectly.