Help with "New Document" - Tool Script

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
SimplSam
Posts: 1241
Joined: Thu Mar 13, 2014 9:09 am
Location: London, UK
Contact:

Help with "New Document" - Tool Script

Post by SimplSam »

I have made a Tool script that creates a New Document - mimicking File New (or Ctrl-N).

I like clicking New on toolbars, so I am trying to replicate that functionality via the Tool panel.

The script function itself is as simple as can be, and it works:

moho:FileNew()

However... I have a couple queries:

1. Function Placement

I am not sure of the correct place to utilise this function call. Currently I have it in "DoLayout(...)". I tried putting it in 'new(...)', 'Run(...)' and 'Handle Message::self.Create', but they don't appear to be invoked for this.

2. Switching off / Resetting the tool state

I only want the tool to be momentary. I have no need for the Options toolbar, and once it has run there is no further functionality (until I need another new document).

The tool is left selected after the new document is created, and it will run every time I switch tabs (i.e. return to the 1st document) or try to close the application.

So I created a boolean state flag to say that the NewDoc tool is active, which prevents unwanted re-running.

Ideally the tool would be able to switch back to the previous/default tool (similar to how the Insert Text tool does it, but that seemed to do it by magic), so that I can use it again later, or simply eliminate the need for the state flag above. But I and can't figure a way to reset the state or detect when another tool has been selected.


So.... Just wondering whether you had any thoughts?

Thanks
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
User avatar
hayasidist
Posts: 3907
Joined: Wed Feb 16, 2011 11:12 am
Location: Kent, England

Re: Help with "New Document" - Tool Script

Post by hayasidist »

I think it needs to be a button not a tool -- edit the _tool_list in your custom folder to have a line such as "

button IT_new_file ...


and the "main" function is

function <your service name>:Run(moho)
...
end

Please note the standard for naming is xx_yyy where xx is a personal identifier such as your initials or equivalent (e.g. LM_ is lost marble, I use HS_ ..) and yyy is descriptive text.

You might also like to look at lm_automation.lua in the program files -- (e.g.) C:\Program Files\Smith Micro\Anime Studio Pro 10\Resources\Support\scripts\menu\Script Writing\ -- which does this sort of thing


===========


so, in more detail you'll have something such as

Code: Select all

-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "IT_new_file"

-- **************************************************
-- General information about this script
-- **************************************************

IT_new_file = {}


function IT_new_file:Name()
	return "IT_new_file"
end

function IT_new_file:Version()
	return "1.0"
end

function IT_new_file:Description()
	return ("Create a new file")
end

function IT_new_file:Creator()
	return "iTeque"
end

function IT_new_file:UILabel()
	return ("New file")
end

function IT_new_file:Run(moho)
  ...
  moho:FileNew()
  ...
end
User avatar
SimplSam
Posts: 1241
Joined: Thu Mar 13, 2014 9:09 am
Location: London, UK
Contact:

Re: Help with "New Document" - Tool Script

Post by SimplSam »

Viola!

And now I also see the magic of the Insert Text 'button'.

Excellent. Many thanks.

I think I have already through every AS script in the known universe trying to resolve this, but the missing piece of the pie was the 'button' invocation.

Thanks again.
Moho 14.3 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.3 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
User avatar
heyvern
Posts: 7045
Joined: Thu Sep 01, 2005 8:49 pm

Re: Help with "New Document" - Tool Script

Post by heyvern »

iteque wrote:Viola!

And now I also see the magic of the Insert Text 'button'.

Excellent. Many thanks.

I think I have already through every AS script in the known universe trying to resolve this, but the missing piece of the pie was the 'button' invocation.

Thanks again.
Yes, I always put in those extra instructions in my Menu Scripts installations for "button" tool use. This allows Debut users to use menu scripts as well. Any menu script can be used as a button tool by just dropping it in the tool script folder and making it a button.
Post Reply