lua script for documents manger
Moderators: Víctor Paredes, Belgarath, slowtiger
lua script for documents manger
Hi to all Moho experts and users. I need your help in writing a script with Lua for Moho. I have an open file first. I want when I run the code. Open another file and make changes to that document and save it. But all my changes apply to the first file. And the second file will be saved. Thank you
- synthsin75
- Posts: 10280
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: lua script for documents manger
Can you describe what you're wanting in more detail?
- 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: lua script for documents manger
I want to open my files one by one and create a comps for them and then save it. thanks
Of course. I want to change 20 files in one folder. With one button. I do not know how to manage the namespace files.
for k,v in pairs(listPathfiles) do
moho:FileOpen(v)
CreateComp(moho)
moho:FileSave()
moho:FileClose()
end
I do not know how to update moho namespace for each file.
Of course. I want to change 20 files in one folder. With one button. I do not know how to manage the namespace files.
for k,v in pairs(listPathfiles) do
moho:FileOpen(v)
CreateComp(moho)
moho:FileSave()
moho:FileClose()
end
I do not know how to update moho namespace for each file.
- synthsin75
- Posts: 10280
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: lua script for documents manger
Very rough example of the code you'll need:morteza wrote: ↑Thu Jan 13, 2022 8:13 am I want to open my files one by one and create a comps for them and then save it. thanks
Of course. I want to change 20 files in one folder. With one button. I do not know how to manage the namespace files.
for k,v in pairs(listPathfiles) do
moho:FileOpen(v)
CreateComp(moho)
moho:FileSave()
moho:FileClose()
end
I do not know how to update moho namespace for each file.
Code: Select all
moho:BeginFileListing(directory_path)
local file = moho:GetNextFile()
while (file) do
moho:FileOpen(directory_path..file)
moho.document:AddLayerComp(comp)
moho:FileSave()
moho:FileClose()
file = moho:GetNextFile()
end
- 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: lua script for documents manger
Thank you very much. I will try this. Thank you.
Re: lua script for documents manger
Hello. Thank you for your help.
I used your algorithm. But here is the problem.
If the file that was opened first is closed, Moho will crash and the code will not continue.
And if the first file is left open and not closed, the comp is created only on the first file.
And the next files are only opened but do not change.
I used your algorithm. But here is the problem.
If the file that was opened first is closed, Moho will crash and the code will not continue.
And if the first file is left open and not closed, the comp is created only on the first file.
And the next files are only opened but do not change.
- synthsin75
- Posts: 10280
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: lua script for documents manger
If you have an extra file opened in Moho beforehand (just an unsaved file you aren't manipulating with the script), so there's always one open file after you close the ones you're opening, it doesn't seem to crash Moho.morteza wrote: ↑Fri Jan 14, 2022 12:12 am Hello. Thank you for your help.
I used your algorithm. But here is the problem.
If the file that was opened first is closed, Moho will crash and the code will not continue.
And if the first file is left open and not closed, the comp is created only on the first file.
And the next files are only opened but do not change.
If the opened files still aren't changing, you might try adding LM.Snooze(msec) between opening a file and manipulating it.
Also, if you're assigning moho.document to a variable, you'll need to update that for each opened 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/
Re: lua script for documents manger
Hello. Thank you for your help.
I tried in many ways.
Unfortunately, the moho.document is not updated.
And the same changes are applied to the first file.
But the last file, which is opened, is saved.
test for open file ==> file(1)
test for open file ==> file(1)
test for open file ==> file(1)
test for open file ==> file(1)
test for open file ==> file(1)
test for open file ==> file(1)
................
Of course. I do this with the click of a button for each file.
The work is well done.
But I can not update the namespace(moho.document) to do it automatically.
I used this {moho:LoadDocument(path)}. But in this. I can only read them. But I can not change.
Moho seems to do this easily in its user interface, and it easily switches its open tabs, but I do not know what function it uses to update the document.
Thank you very much for your time for me.
I tried in many ways.
Unfortunately, the moho.document is not updated.
And the same changes are applied to the first file.
But the last file, which is opened, is saved.
Code: Select all
ScriptName = "MJ_run_submit"
MJ_run_submit = {}
function MJ_run_submit:Run(moho)
local dir = (moho.document:Path()):match("(.*[/\\])")
MJ_run_submit:CreateComp(moho)
moho:FileSave()
moho:BeginFileListing(dir)
local file = moho:GetNextFile()
while file do
if file:match "[^.]+$" == "moho" then
moho:FileOpen(dir .. "\\" .. file)
LM.Snooze(5)
MJ_run_submit:CreateComp(moho)
moho:FileSave()
end
--moho:FileClose()
file = moho:GetNextFile()
end
end
function MJ_run_submit:CreateComp(moho)
local doc = moho.document
print("test for open file ==> " .. doc:Name())
end
test for open file ==> file(1)
test for open file ==> file(1)
test for open file ==> file(1)
test for open file ==> file(1)
test for open file ==> file(1)
................
Of course. I do this with the click of a button for each file.
The work is well done.
But I can not update the namespace(moho.document) to do it automatically.
I used this {moho:LoadDocument(path)}. But in this. I can only read them. But I can not change.
Moho seems to do this easily in its user interface, and it easily switches its open tabs, but I do not know what function it uses to update the document.
Thank you very much for your time for me.
- synthsin75
- Posts: 10280
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: lua script for documents manger
You're right. moho.document doesn't update until after the script thread ends.
That being the case, there doesn't appear to be a good way for Moho to process multiple files in one go. Using an external macro to change document tabs might make it possible, but that's pretty convoluted.
The only other option that I can think of would involve parsing and editing the file JSON directly, which you could do from a Moho Lua script but without opening them in Moho. Maybe even more convoluted.
The only comp scripting I've done was an import script: http://www.lostmarble.com/forum/viewtop ... 0&p=184169
Wouldn't help you though.
That being the case, there doesn't appear to be a good way for Moho to process multiple files in one go. Using an external macro to change document tabs might make it possible, but that's pretty convoluted.
The only other option that I can think of would involve parsing and editing the file JSON directly, which you could do from a Moho Lua script but without opening them in Moho. Maybe even more convoluted.
The only comp scripting I've done was an import script: http://www.lostmarble.com/forum/viewtop ... 0&p=184169
Wouldn't help you though.
- 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: lua script for documents manger
Thank you for your response.
Yes, I have already written code by C # to change the JSON of each file.
Maybe it's better to use this method.
Of course, it is very difficult to build a comp with this method and the desired settings.
But there is no choice.
Thanks again for your help.
I will use your help again, dear friend.
Yes, I have already written code by C # to change the JSON of each file.
Maybe it's better to use this method.
Of course, it is very difficult to build a comp with this method and the desired settings.
But there is no choice.
Thanks again for your help.
I will use your help again, dear friend.
- synthsin75
- Posts: 10280
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: lua script for documents manger
No problem. Wish I could have been more helpful.
Maybe next time.
Maybe next time.

- 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: 3857
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: lua script for documents manger
I'm in total guesswork here...
I'm wondering if AHK can help.
along the lines of (in AHK)
I'm wondering if AHK can help.
along the lines of (in AHK)
Code: Select all
for all .moho files in directory
open file
run 'make layer comp' tool
save
close
end
- synthsin75
- Posts: 10280
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: lua script for documents manger
It could. You'd just have to keep bouncing between Lua and AHK.
1. Have the Lua script open all the files in Moho and launch the AHK script.
2. Have the AHK script repeatedly change document tabs and launch the Lua script (with an assigned shortcut in Moho) that makes the changes and saves the files.
That way each run of the Lua script has an updated moho.document.
Probably more straight forward to edit the JSON though.
- 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: lua script for documents manger
Hello Dears.
Yes. Changes in JSON file seem better and more optimal. To automate tasks.
But using a runner after opening each file is also very interesting.
Is there an example of running a script in Moho?
I have not used it yet.
Thank you very much.
Yes. Changes in JSON file seem better and more optimal. To automate tasks.
But using a runner after opening each file is also very interesting.
Is there an example of running a script in Moho?
I have not used it yet.
Thank you very much.
- synthsin75
- Posts: 10280
- Joined: Mon Jan 14, 2008 11:20 pm
- Location: Oklahoma
- Contact:
Re: lua script for documents manger
This script of mine has an example of launching an AHK script and the AHK script invoking a Lua script shortcut in Moho: http://www.lostmarble.com/forum/viewtopic.php?t=32740
- 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/