Save script data?

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Fahim, Distinct Sun, Víctor Paredes, erey, Belgarath, slowtiger

Post Reply
dkwroot
Posts: 659
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Save script data?

Post by dkwroot » Tue May 08, 2018 4:59 pm

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?
User avatar
synthsin75
Posts: 7114
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Save script data?

Post by synthsin75 » Tue May 08, 2018 7:56 pm

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.
hayasidist
Posts: 2123
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Save script data?

Post by hayasidist » Tue May 08, 2018 8:44 pm

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.
User avatar
synthsin75
Posts: 7114
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Save script data?

Post by synthsin75 » Tue May 08, 2018 9:28 pm

Yeah, you could share the values through Moho files, like Paul says.
dkwroot
Posts: 659
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Save script data?

Post by dkwroot » Wed May 09, 2018 12:10 am

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.
Post Reply