Hello, I have used the page to create scripts. http://mohoscripting.com/new_script.php
I want to create a button, that when I click on it I import or load a .moho file directly. I have tried many ways but I do not solve it, could someone help me please, this is the code:
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "upload"
-- **************************************************
-- General information about this script
-- **************************************************
upload = {}
function upload:Name()
return self:Localize('UILabel')
end
function upload:Version()
return '1.0'
end
function upload:UILabel()
return self:Localize('UILabel')
end
function upload:Creator()
return 'Brayan'
end
function upload:Description()
return self:Localize('Description')
end
function upload:ColorizeIcon()
return true
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function upload:IsRelevant(moho)
return true
end
function upload:IsEnabled(moho)
return true
end
-- **************************************************
-- Keyboard/Mouse Control
-- **************************************************
function upload:OnMouseDown(moho, mouseEvent)
end
function upload:OnMouseMoved(moho, mouseEvent)
end
function upload:OnMouseUp(moho, mouseEvent)
end
function upload:OnKeyDown(moho, keyEvent)
end
function upload:OnKeyUp(moho, keyEvent)
end
-- **************************************************
-- Tool Panel Layout
-- **************************************************
upload.UPLOAD = MOHO.MSG_BASE
function upload:FileImport(path, mode)
end
function upload:DoLayout(moho, layout)
self.uploadButton = LM.GUI.Button(self:Localize('upload'), self.UPLOAD)
layout:AddChild(self.uploadButton, LM.GUI.ALIGN_LEFT, 0)
end
function upload:HandleMessage(moho, view, msg)
if msg == self.CARGAR then
self FileImport("D:\\El Aus Juego\\Personajes\\Auron personajes\\VioletaG.moho",1)
end
end
-- **************************************************
-- Localization
-- **************************************************
function upload:Localize(text)
local phrase = {}
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'upload'
phrase['upload'] = 'upload'
local fileWord = MOHO.Localize("/Menus/File/File=File")
if fileWord == "Файл" then
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'upload'
phrase['upload'] = 'upload'
end
return phrase[text]
end
First, it's best practice to always prefix your script function names with your initials, like "bb_upload" instead of just "upload". This makes sure scripts do not conflict with one another.
If you're making a button/menu script, it cannot have the mouse functions in it, as this tells Moho that it is a tool. You need: function bb_upload:Run(moho)
You do not need any GUI functions (DoLayout, HandleMessage, etc.) for a button that just does one thing. You just need to do that stuff in the Run function.
If you're calling another function in your script, you need "self:FileImport()" instead of "self FileImport()". And you use the Moho API file import function like: moho:FileImport(path, mode)
No need to create or call a new function for it yourself.
If you go back MohoScripting and create a new Button/Menu script - using just the options below - you will get a basic button script, and then you just need to add your (corrected) code in the Run function.
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
Thanks synthsin75 , for the line moho:Fileimport(path,mode)
I have something like this, but I want the button (upload) to appear and when I click on it, it will load directly without pressing accept. thanks anyway, if you have an idea please correct me
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "bb_upload"
-- **************************************************
-- General information about this script
-- **************************************************
bb_upload = {}
function bb_upload:Name()
return self:Localize('UILabel')
end
function bb_upload:Version()
return '1.0'
end
function bb_upload:UILabel()
return self:Localize('UILabel')
end
function bb_upload:Creator()
return 'brayan'
end
function bb_upload:Description()
return self:Localize('Description')
end
function bb_upload:ColorizeIcon()
return true
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function bb_upload:IsRelevant(moho)
return true
end
function bb_upload:IsEnabled(moho)
return true
end
-- **************************************************
-- bb_uploadDialog
-- **************************************************
local bb_uploadDialog = {}
bb_uploadDialog.UPLOAD = MOHO.MSG_BASE
function bb_uploadDialog:new()
local d = LM.GUI.SimpleDialog(bb_upload:Localize('UILabel'), bb_uploadDialog)
local l = d:GetLayout()
d.uploadButton = LM.GUI.Button(bb_upload:Localize('upload'), d.UPLOAD)
l:AddChild(d.uploadButton, LM.GUI.ALIGN_LEFT, 0)
return d
end
function bb_uploadDialog:HandleMessage(msg)
if msg == self.UPLOAD then
else
end
end
-- **************************************************
-- The guts of this script
-- **************************************************
function bb_upload:Run(moho)
moho:FileImport("D:\\El Aus Juego\\Personajes\\Auron personajes\\VioletaG.moho",1)
return
end
-- **************************************************
-- Localization
-- **************************************************
function bb_upload:Localize(text)
local phrase = {}
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
local fileWord = MOHO.Localize("/Menus/File/File=File")
if fileWord == "Файл" then
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
end
return phrase[text]
end
Last edited by bbrraayyaann on Sun Feb 21, 2021 9:22 pm, edited 1 time in total.
Thanks SimplSam, you also helped me I had not seen that option and I corrected it I have something like this :
but I want the button (upload) to appear and when I click on it, it will load directly without pressing accept. thanks anyway, if you have an idea please correct me.
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "bb_upload"
-- **************************************************
-- General information about this script
-- **************************************************
bb_upload = {}
function bb_upload:Name()
return self:Localize('UILabel')
end
function bb_upload:Version()
return '1.0'
end
function bb_upload:UILabel()
return self:Localize('UILabel')
end
function bb_upload:Creator()
return 'brayan'
end
function bb_upload:Description()
return self:Localize('Description')
end
function bb_upload:ColorizeIcon()
return true
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function bb_upload:IsRelevant(moho)
return true
end
function bb_upload:IsEnabled(moho)
return true
end
-- **************************************************
-- bb_uploadDialog
-- **************************************************
local bb_uploadDialog = {}
bb_uploadDialog.UPLOAD = MOHO.MSG_BASE
function bb_uploadDialog:new()
local d = LM.GUI.SimpleDialog(bb_upload:Localize('UILabel'), bb_uploadDialog)
local l = d:GetLayout()
d.uploadButton = LM.GUI.Button(bb_upload:Localize('upload'), d.UPLOAD)
l:AddChild(d.uploadButton, LM.GUI.ALIGN_LEFT, 0)
return d
end
function bb_uploadDialog:HandleMessage(msg)
if msg == self.UPLOAD then
else
end
end
-- **************************************************
-- The guts of this script
-- **************************************************
function bb_upload:Run(moho)
moho:FileImport("D:\\El Aus Juego\\Personajes\\Auron personajes\\VioletaG.moho",1)
return
end
-- **************************************************
-- Localization
-- **************************************************
function bb_upload:Localize(text)
local phrase = {}
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
local fileWord = MOHO.Localize("/Menus/File/File=File")
if fileWord == "Файл" then
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
end
return phrase[text]
end
there are a couple of comments I've added to help especially with localisation.
if/as you're hard-coding the file name you won't need a dialogue ... obviously, if you're planning on adding to this you will.
and you should aim to give yourself an icon that will appear in the tools panel if this is more than a quick one-off... ask if you need info on the format / conventions for icons.
(tested ok - except ofc I don't have your file, so Moho threw the expected "can't find it" error)
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "bb_upload"
-- **************************************************
-- General information about this script
-- **************************************************
bb_upload = {}
function bb_upload:Name()
return self:Localize('UILabel')
end
function bb_upload:Version()
return '1.0'
end
function bb_upload:UILabel()
return self:Localize('UILabel')
end
function bb_upload:Creator()
return 'brayan'
end
function bb_upload:Description()
return self:Localize('Description')
end
function bb_upload:ColorizeIcon()
return true
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function bb_upload:IsRelevant(moho)
return true
end
function bb_upload:IsEnabled(moho)
return true
end
-- **************************************************
-- bb_uploadDialog
-- **************************************************
--[[dialogue not needed??
local bb_uploadDialog = {}
bb_uploadDialog.UPLOAD = MOHO.MSG_BASE
function bb_uploadDialog:new()
local d = LM.GUI.SimpleDialog(bb_upload:Localize('UILabel'), bb_uploadDialog)
local l = d:GetLayout()
d.uploadButton = LM.GUI.Button(bb_upload:Localize('upload'), d.UPLOAD)
l:AddChild(d.uploadButton, LM.GUI.ALIGN_LEFT, 0)
return d
end
function bb_uploadDialog:HandleMessage(msg)
if msg == self.UPLOAD then
else
end
end
]]
-- **************************************************
-- The guts of this script
-- **************************************************
function bb_upload:Run(moho)
moho:FileImport("D:\\El Aus Juego\\Personajes\\Auron personajes\\VioletaG.moho",1)
return
end
-- **************************************************
-- Localization
-- **************************************************
function bb_upload:Localize(text)
local phrase = {}
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
--[[
-- this is for the Russian localisation; if you want to provide localisation to Spanish change Файл to Archivo
-- and provide Spanish text for the given phrases.
local fileWord = MOHO.Localize("/Menus/File/File=File")
if fileWord == "Файл" then
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
end
]]
return phrase[text]
end
Hello hayasidista, yes I tried it and it works the same as before. What I want is that the upload button also appears to press it and I just load the file.
For example here it says only "Message UPLOAD received".
And I want the file to be uploaded
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "bb_upload"
-- **************************************************
-- General information about this script
-- **************************************************
bb_upload = {}
function bb_upload:Name()
return self:Localize('UILabel')
end
function bb_upload:Version()
return '1.0'
end
function bb_upload:UILabel()
return self:Localize('UILabel')
end
function bb_upload:Creator()
return 'Brayan'
end
function bb_upload:Description()
return self:Localize('Description')
end
function bb_upload:ColorizeIcon()
return true
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function bb_upload:IsRelevant(moho)
return true
end
function bb_upload:IsEnabled(moho)
return true
end
-- **************************************************
-- bb_uploadDialog
-- **************************************************
local bb_uploadDialog = {}
bb_uploadDialog.UPLOAD = MOHO.MSG_BASE
function bb_uploadDialog:new()
local d = LM.GUI.SimpleDialog(bb_upload:Localize('UILabel'), bb_uploadDialog)
local l = d:GetLayout()
d.uploadButton = LM.GUI.Button(bb_upload:Localize('upload'), d.UPLOAD)
l:AddChild(d.uploadButton, LM.GUI.ALIGN_LEFT, 0)
return d
end
function bb_uploadDialog:HandleMessage(msg)
if msg == self.UPLOAD then
print('Message UPLOAD received')
else
end
end
-- **************************************************
-- The guts of this script
-- **************************************************
function bb_upload:Run(moho)
local dlog = bb_uploadDialog:new(moho)
if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then
return
end
moho.document:SetDirty()
moho.document:PrepUndo(nil)
-- Your code here:
end
-- **************************************************
-- Localization
-- **************************************************
function bb_upload:Localize(text)
local phrase = {}
phrase['Description'] = 'ggg'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
local fileWord = MOHO.Localize("/Menus/File/File=File")
if fileWord == "Файл" then
phrase['Description'] = 'ggg'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
end
return phrase[text]
end
bbrraayyaann wrote: ↑Sun Feb 21, 2021 9:19 pm
Thanks synthsin75 , for the line moho:Fileimport(path,mode)
I have something like this, but I want the button (upload) to appear and when I click on it, it will load directly without pressing accept. thanks anyway, if you have an idea please correct me
It should only ask to accept if you use 2 as the second argument in FileImport (if doing it in the Run function).
If you really want a dialog with a button, there's no way to get rid of the OK and Cancel buttons, as those come automatically with a SimpleDialog.
EDIT: If you really, really want a dialog, you could do this:
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "bb_upload"
-- **************************************************
-- General information about this script
-- **************************************************
bb_upload = {}
function bb_upload:Name()
return self:Localize('UILabel')
end
function bb_upload:Version()
return '1.0'
end
function bb_upload:UILabel()
return self:Localize('UILabel')
end
function bb_upload:Creator()
return 'brayan'
end
function bb_upload:Description()
return self:Localize('Description')
end
function bb_upload:ColorizeIcon()
return true
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function bb_upload:IsRelevant(moho)
return true
end
function bb_upload:IsEnabled(moho)
return true
end
-- **************************************************
-- bb_uploadDialog
-- **************************************************
local bb_uploadDialog = {}
bb_uploadDialog.UPLOAD = MOHO.MSG_BASE
function bb_uploadDialog:new(moho)
local d = LM.GUI.SimpleDialog(bb_upload:Localize('UILabel'), bb_uploadDialog)
local l = d:GetLayout()
d.uploadText = LM.GUI.StaticText("Do you want to import this file?")
l:AddChild(d.uploadText, LM.GUI.ALIGN_LEFT, 0)
return d
end
-- **************************************************
-- The guts of this script
-- **************************************************
function bb_upload:Run(moho)
local dlog = bb_uploadDialog:new(moho)
if (dlog:DoModal() == LM.GUI.MSG_OK) then
moho:FileImport("D:\\El Aus Juego\\Personajes\\Auron personajes\\VioletaG.moho",1)
elseif (dlog:DoModal() == LM.GUI.MSG_CANCEL) then
return
end
end
-- **************************************************
-- Localization
-- **************************************************
function bb_upload:Localize(text)
local phrase = {}
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
local fileWord = MOHO.Localize("/Menus/File/File=File")
if fileWord == "????" then
phrase['Description'] = 'this is the description'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
end
return phrase[text]
end
When the dialog comes up, you just have to hit OK or hit enter (same thing).
bbrraayyaann wrote: ↑Sun Feb 21, 2021 10:24 pm
Hello hayasidista, yes I tried it and it works the same as before. What I want is that the upload button also appears to press it and I just load the file.
For example here it says only "Message UPLOAD received".
And I want the file to be uploaded
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************
ScriptName = "bb_upload"
-- **************************************************
-- General information about this script
-- **************************************************
bb_upload = {}
function bb_upload:Name()
return self:Localize('UILabel')
end
function bb_upload:Version()
return '1.0'
end
function bb_upload:UILabel()
return self:Localize('UILabel')
end
function bb_upload:Creator()
return 'Brayan'
end
function bb_upload:Description()
return self:Localize('Description')
end
function bb_upload:ColorizeIcon()
return true
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function bb_upload:IsRelevant(moho)
return true
end
function bb_upload:IsEnabled(moho)
return true
end
-- **************************************************
-- bb_uploadDialog
-- **************************************************
local bb_uploadDialog = {}
bb_uploadDialog.UPLOAD = MOHO.MSG_BASE
function bb_uploadDialog:new()
local d = LM.GUI.SimpleDialog(bb_upload:Localize('UILabel'), bb_uploadDialog)
local l = d:GetLayout()
d.uploadButton = LM.GUI.Button(bb_upload:Localize('upload'), d.UPLOAD)
l:AddChild(d.uploadButton, LM.GUI.ALIGN_LEFT, 0)
return d
end
function bb_uploadDialog:HandleMessage(msg)
if msg == self.UPLOAD then
print('Message UPLOAD received')
else
end
end
-- **************************************************
-- The guts of this script
-- **************************************************
function bb_upload:Run(moho)
local dlog = bb_uploadDialog:new(moho)
if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then
return
end
moho.document:SetDirty()
moho.document:PrepUndo(nil)
-- Your code here:
end
-- **************************************************
-- Localization
-- **************************************************
function bb_upload:Localize(text)
local phrase = {}
phrase['Description'] = 'ggg'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
local fileWord = MOHO.Localize("/Menus/File/File=File")
if fileWord == "Файл" then
phrase['Description'] = 'ggg'
phrase['UILabel'] = 'bb_upload'
phrase['upload'] = 'upload'
end
return phrase[text]
end
the version you have here is not the one I gave you -- I had commented out the dialogue (as synthsin said - if all you want to do is to press the button in the tool window, you don't need it); this version doesn't include Run function along with the moho:FileImport() call; and the the localisation you have is looking for the Russian language version - and, judging by the file name you used in the first version, you're likely to have a Spanish UI? so looking for the Russian word for "file" will always make the test (if fileWord == "Файл" ) evaluate to false.
I am now of the opinion that Brayan might want a Pop Up Dialog in addition to a Button on the dialog that loads the File (not necessarily the OK button). I suspect that this may be the start of something bigger.
Brayan can you please try and clarify even just by listing the user interaction steps - as another user would see it.
i.e.
1) Click Tool / Button
2) Review Dialog message
3a) Option: Click X to do Y
3b) Option: Click OK to do Z
3c) Option: Click CANCEL to Quit
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam
SimplSam wrote: ↑Mon Feb 22, 2021 9:41 am
I am now of the opinion that Brayan might want a Pop Up Dialog in addition to a Button on the dialog that loads the File (not necessarily the OK button). I suspect that this may be the start of something bigger.
haha no, all the answers helped me a lot, what happens is that I am very new to lua ; I just learned how the language works and there is not much information about it. That's why I'm also learning python to better understand the LUA language, about the SCRIPT I solved it, it was something simple that I didn't understand at the beginning.