Notifications
Clear all

[Closed] im going bananas – get macros procedurally

Hi folks,

Is there a way to get a whole category of macros and iterate over them?

I saw in the docs that you can get a macro by category+name or index, but this is not really helpful – I want to populate a menu with macro.run commands automatically. Maybe I am going about this entirely the wrong way.

Anyone have any tips for me?

cheers,

cw

4 Replies

Here’s something I did a while ago. It’s a more or less direct copy-paste from old work, so it might be ugly/wrong. For one the nested if’s should be written nicer, hehe.
But it might get you started at least

(
	local macroScripts;
	local macroScriptNames;
	
	function loadmacroScripts = (
		local macroCategory = "cat";
		local mcrStream = stringstream "";
		macros.list to:mcrStream;
		seek mcrStream 0;
		
		macroScripts = #();
		macroScriptNames = #();
		
		while (not (eof mcrStream)) do (
			local line = (readDelimitedString mcrStream "
");
			local lineSplit = (filterString line "\"");
			
			if (lineSplit[6] == macroCategory) do (
				append macroScripts lineSplit[2];
				
				local mcr = openFile lineSplit[8];
				if (mcr != undefined) do (
					local newName = "";
					seek mcr 0;
					if not eof mcr do (
						skipToString mcr lineSplit[2];
						if not eof mcr do (
							skipToString mcr "toolTip:";
							if not eof mcr do (
								readDelimitedString mcr "\"";
								if not eof mcr do (
									local newName = readDelimitedString mcr "\"";
									if (newName == "") do (
										seek mcr 0;
										skipToString mcr lineSplit[2];
										skipToString mcr "buttonText:";
										readDelimitedString mcr "\"";
										newName = readDelimitedString mcr "\"";
									)
								)
							)
						)
					)
					if (newName != "") then (
						append macroScriptNames newName;
					) else (
						append macroScriptNames macroScripts[macroScripts.count];
					)
				)
			)
		)

	)
)

Thanks so much for a prompt reply!

I had a slight hair raising sensation that it might end up parsing that list as text – thanks so much for your example script, it will save me some time for sure!

Cheers,

cw

1 Reply
(@pjanssen)
Joined: 11 months ago

Posts: 0

Yeah it’s not great…but afaik its the only way. Good luck

Just loads up to 4 categories into a quad menu automatically, removes the old ones and seems to work ok!

Thanks again. much appreciated.


  --cwmisc_quad_macros
 --thanks to Pier Janssen for the idea of parsing the macros.list() string to get categories and macro names.
 
 function loadmacroScripts _category= (
 	local macroCategory = _category;
 	local mcrStream = stringstream "";
 	macros.list to:mcrStream;
 	seek mcrStream 0;
 	
 	macroScripts = #();
 	macroScriptNames = #();
 	
 	while (not (eof mcrStream)) do (
 		local line = (readDelimitedString mcrStream "
");
 		local lineSplit = (filterString line "\" ");
 		
 		--for b in lineSplit do (format "% " b)
 		--format"
"
 		
 		if (lineSplit[3] == macroCategory) then (
 			append macroScripts lineSplit[2];
 		)
 	)
 	return macroScripts
 )
 
 (
 	existing = menuMan.findQuadMenu "auto_mcr"	
 	if existing != undefined then automacro = existing else
 	(
 		automacro = menuMan.createQuadMenu "auto_mcr" "quad1" "quad2" "quad3" "quad4"
 	)
 	
 	categories = #("SGTools","CWtools","cwmisc","")
 
 	for _cat=1 to categories.count do
 	(
 		thecategory = categories[_cat]
 		
 		if thecategory !="" then
 		(
 			thescripts = loadmacroScripts thecategory
 			--for b in thescripts do print b
 			
 			--the docs are incorrect, stating 0 based indices when in fact only 1 based works properly.  :(
 			quadn = automacro.getMenu (_cat)
 			if quadn != undefined then
 			(
 				for em = quadn.numItems() to 1 by -1 do
 				(
 					
 					format "removing item: %
" em
 					quadn.removeItemByPosition em
 				)
 				
 				for mitem in thescripts do
 				(
 					mn = menuMan.createActionItem mitem thecategory
 					quadn.additem mn -1
 				)
 			)
 		)
 	)
 )