Page 1 of 2

lua script for documents manger

Posted: Wed Jan 12, 2022 4:46 pm
by morteza
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

Re: lua script for documents manger

Posted: Wed Jan 12, 2022 4:53 pm
by synthsin75
Can you describe what you're wanting in more detail?

Re: lua script for documents manger

Posted: Thu Jan 13, 2022 8:13 am
by morteza
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.

Re: lua script for documents manger

Posted: Thu Jan 13, 2022 5:17 pm
by synthsin75
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.
Very rough example of the code you'll need:

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

Re: lua script for documents manger

Posted: Thu Jan 13, 2022 8:50 pm
by morteza
Thank you very much. I will try this. Thank you.

Re: lua script for documents manger

Posted: Fri Jan 14, 2022 12:12 am
by morteza
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.

Re: lua script for documents manger

Posted: Fri Jan 14, 2022 1:10 am
by synthsin75
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 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.

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.

Re: lua script for documents manger

Posted: Fri Jan 14, 2022 3:55 pm
by morteza
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.

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)
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.

Re: lua script for documents manger

Posted: Fri Jan 14, 2022 10:22 pm
by synthsin75
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.

Re: lua script for documents manger

Posted: Fri Jan 14, 2022 10:53 pm
by morteza
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.

Re: lua script for documents manger

Posted: Fri Jan 14, 2022 11:46 pm
by synthsin75
No problem. Wish I could have been more helpful.

Maybe next time. :wink:

Re: lua script for documents manger

Posted: Sat Jan 15, 2022 12:19 pm
by hayasidist
I'm in total guesswork here...

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

Re: lua script for documents manger

Posted: Sat Jan 15, 2022 8:51 pm
by synthsin75
hayasidist wrote: Sat Jan 15, 2022 12:19 pm I'm wondering if AHK can help.
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.

Re: lua script for documents manger

Posted: Sat Jan 15, 2022 10:50 pm
by morteza
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.

Re: lua script for documents manger

Posted: Sat Jan 15, 2022 11:47 pm
by synthsin75
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