Notifications
Clear all

[Closed] macroscripts inside a structure

Of course, there was a missing line!

on roll_Main close ( g_myVeryGreatTool.closeTool(); )

Notice that “isWindowOpen” is public so that other tools can know if the tool is open or not (in case of interaction between 2 tools)


I didn’t know about the big global encapsulation.
It removes a global, yeah… is it so important ?


And what is your magical solution, Denis ?: )

2 Replies
(@denist)
Joined: 11 months ago

Posts: 0

the solution of what?

(@feranti)
Joined: 11 months ago

Posts: 0

I may have misunderstood the topic but it seems that you found a better way to organize your script, including the macro inside the struct.

so… let’s continue:


global HelloWorld 
(
	struct HelloWorldStruct
	(
	private
		pos = [800,200],
		size = [200,40],
		opened = off,
	public
		dialog = 
		(
			rollout dialog "Hello World"
			(
				on dialog open do HelloWorld.open()
				on dialog close do HelloWorld.close()
			)
		),
		fn isOpen = (iskindof dialog RolloutClass and opened),
		fn open = 
		(
			opened = on
			createdialog dialog pos:pos size:size
		),
		fn close = 
		(
			opened = off
			destroydialog dialog
		),
		fn toggle = if isOpen() then close() else open(),
		
		action =
		(
			macroScript Macro_HelloWorld 
				category:"HelloWorld" 
				tooltip:"Hello World"
				buttonText:"Hi WORLD"
				silentErrors:off
			(
				local d
				fn enabled = isstruct (d = HelloWorld)
				on execute do if enabled() do d.toggle()
			)
		),
		on create do
		(
			if isstruct HelloWorld do HelloWorld.close()
		)
	)
	HelloWorld = HelloWorldStruct()
	ok
)

try to execute the code. the code automatically defines the macro. you can go to Customize User Interface… and in category HelloWorld find “Hello World” macros. Make a button, try to press it… but try to restart the max…

… the bad news is the button doesn’t work anymore. because nothing loaded our structure.
the good new is it doesn’t break anything. because i check the structure existence before execute the macro.

let’s save the code as MS file in any auto-loading by max directory. let’s save it as …max\scripts\startup\HelloWorld\HelloWorld.ms
and restart max…

… now you see that button works.
the question is where is really our macros placed? the answer is simple. in the user’s macroscripts directory.
and it load before our structure, but our structure successfully redefine it on startup. (remember this fact. you will use it later).

our button works as toggle. how is about to make works as ON/OFF (if dialog open it’s checked, if not – not checked)

… to be continued

it’s so easy because i already have everything for that… we need just add to lines:


global HelloWorld 
(
	struct HelloWorldStruct
	(
	private
		pos = [800,200],
		size = [200,40],
		opened = off,
	public
		dialog = 
		(
			rollout dialog "Hello World"
			(
				on dialog open do HelloWorld.open()
				on dialog close do HelloWorld.close()
			)
		),
		fn isOpen = (iskindof dialog RolloutClass and opened),
		fn open = 
		(
			opened = on
			createdialog dialog pos:pos size:size
		),
		fn close = 
		(
			opened = off
			destroydialog dialog
		),
		fn toggle = if isOpen() then close() else open(),
		
		action =
		(
			macroScript Macro_HelloWorld 
				category:"HelloWorld" 
				tooltip:"Hello World"
				buttonText:"Hi WORLD"
				silentErrors:off
			(
				local d
				fn enabled = isstruct (d = HelloWorld)
				on isEnabled do enabled()
				on isChecked do enabled() and d.isOpen()
				on execute do if enabled() do d.toggle()
			)
		),
		on create do
		(
			if isstruct HelloWorld do HelloWorld.close()
		)
	)
	HelloWorld = HelloWorldStruct()
	ok
)

the button works but if we open the dialog from script or close the dialog manually the button state doesn’t change. hmm… what can we do? … be continued

to make it works right we only need to add updateToolbarButtons() to open and close functions…


global HelloWorld 
(
	struct HelloWorldStruct
	(
	private
		pos = [800,200],
		size = [200,40],
		opened = off,
	public
		dialog = 
		(
			rollout dialog "Hello World"
			(
				on dialog open do HelloWorld.open()
				on dialog close do HelloWorld.close()
			)
		),
		fn isOpen = (iskindof dialog RolloutClass and opened),
		fn open = 
		(
			opened = on
			createdialog dialog pos:pos size:size
			updateToolbarButtons()
		),
		fn close = 
		(
			opened = off
			destroydialog dialog
			updateToolbarButtons()
		),
		fn toggle = if isOpen() then close() else open(),
		
		action =
		(
			macroScript Macro_HelloWorld 
				category:"HelloWorld" 
				tooltip:"Hello World"
				buttonText:"Hi WORLD"
				silentErrors:off
			(
				local d
				fn enabled = isstruct (d = HelloWorld)
				on isEnabled do enabled()
				on isChecked do enabled() and d.isOpen()
				on execute do if enabled() do d.toggle()
			)
		),
		on create do
		(
			if isstruct HelloWorld do HelloWorld.close()
		)
	)
	HelloWorld = HelloWorldStruct()
	updateToolbarButtons()
	ok
)

now the question is … why do i put the macros definition inside the structure if i can leave it outside but still in the same file? … be continued

What a teasing!!

You lost me long ago Denis, I can see what you’re doing it and why, but I’ve got a setup here where all our scripts are in a central repository as script files not macros. Then we have an interface which allows you to quickly make a button on the toolbar which just fires a fileIn to the needed script file.

I’m finding the centralised database a much better system as I can update and edit scripts and everyone has the latest version.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

i don’t want to say that macros in structures is only right way to organize tool distribution.
i just want to show how to do it.

the centralised database is what i have. the user just has to add my root directory as a 3-rd Party Path to get everything (plugins, dll, scripts, startup, icons, etc.) automatically loaded with max. and of course user can place the root directory anywhere he wants.

Page 2 / 3