Save script data?
Moderators: Víctor Paredes, Belgarath, slowtiger
Save script data?
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?
- synthsin75
- Posts: 9982
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: Save script data?
Yes, you can write to a plain text file, like this: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?
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()
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)
- Wes
Donations: https://www.paypal.com/paypalme/synthsin75 (Thx, everyone.)
https://www.youtube.com/user/synthsin75
Scripting reference: https://mohoscripting.com/
Donations: https://www.paypal.com/paypalme/synthsin75 (Thx, everyone.)
https://www.youtube.com/user/synthsin75
Scripting reference: https://mohoscripting.com/
- hayasidist
- Posts: 3536
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: Save script data?
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.
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.
- synthsin75
- Posts: 9982
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: Save script data?
Yeah, you could share the values through Moho files, like Paul says.
- Wes
Donations: https://www.paypal.com/paypalme/synthsin75 (Thx, everyone.)
https://www.youtube.com/user/synthsin75
Scripting reference: https://mohoscripting.com/
Donations: https://www.paypal.com/paypalme/synthsin75 (Thx, everyone.)
https://www.youtube.com/user/synthsin75
Scripting reference: https://mohoscripting.com/
Re: Save script data?
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.