How to launch a macro >50 lines from the command line.

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information:CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
engeeaitch
Posts: 4
Joined: Sun Aug 14, 2016 3:54 pm

How to launch a macro >50 lines from the command line.

Post by engeeaitch » Tue Sep 11, 2018 9:27 pm

iMacros for Firefox 10.0.2.1450
Browser Firefox 62.0
OS: Windows 10

I am using the suggestion in the Wiki for Command Line Support here but needed to run a macro that was more than 50 lines long. I tried splitting the macro into two and then launching the macros one after the other, but that didn’t work, because they ran in parallel with each other, not sequentially. So I have modified the solution in the Wiki to launch an event as the last line of the first macro, and used that event to launch the second macro. If anyone is interested, here is my code:

Code: Select all

<html>
	<meta charset="UTF-8">
    <body onload="window.setTimeout('document.getElementById(\'criimlaunch\').click();', 1000);">
        <script>
		//This function gets called once per macro (synchronously).  
		function main(macroId){
			var macroCode = "";
			switch(macroId){
			case 1:
				macroCode += 'PROMPT "First Macro"\n'; //Replace as necessary
				break;
			case 2:
				macroCode += 'PROMPT "Second Macro"\n'; //Replace as necessary
				break;
			default:
				// No more code to run
				return;
			}
			//Now append the macro code needed to generate a custom event as the last line of the macro
			macroCode += eventMacro(macroId);
			launchMacro(macroCode);
		}
		
		//This function adds the code needed to generate a custom event into the iMacros EVAL command
		function eventMacro(macroId){
			eventCode = "";
			eventCode += 'SET !VAR1 EVAL("' +
				//To allow the event listener to have the permissions to read the event detail, 
				//the detail must be cloned into the document.
				//See here: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent#Firing_from_privileged_code_to_non-privileged_code
				'var clonedDetail = cloneInto({id:'+macroId+'}, document.defaultView);' +
				//Now create the event (with the macro id number) and dispatch it
				'var event = new CustomEvent(\\"iMacrosFinished\\", {detail:clonedDetail});' +
				'document.dispatchEvent(event);")\n';
			return eventCode;
		}

		//This function runs the macro using the supplied code
        function launchMacro(macroCode){
            try{
                    if(!/^(?:chrome|https?|file)/.test(location)){
						alert('iMacros: Open webpage to run a macro.');
						return;
					}
			   
					var macro = {}; 
					macro.source = macroCode;
					macro.name = 'EmbeddedMacro';
					
					// create and dispatch the event
					var event = new CustomEvent('iMacrosRunMacro', {detail: macro});
					window.dispatchEvent(event);
			}catch(e){
                alert('iMacros error: '+e.toString());
            };
        }
		
		//Add a listener to catch the event created by the last line of the macro
		document.addEventListener("iMacrosFinished", function(e){
			//Call the main function requesting the next macro
			main(e.detail.id+1);
		});
		
        </script>

        <a id="criimlaunch" href="javascript:main(1);">Launch iMacros</a>

    </body>
</html>
chivracq
Posts: 8535
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to launch a macro >50 lines from the command line.

Post by chivracq » Tue Sep 11, 2018 9:43 pm

engeeaitch wrote:

Code: Select all

iMacros for Firefox 10.0.2.1450
Browser Firefox 62.0
OS: Windows 10
I am using the suggestion in the Wiki for Command Line Support here but needed to run a macro that was more than 50 lines long. I tried splitting the macro into two and then launching the macros one after the other, but that didn’t work, because they ran in parallel with each other, not sequentially. So I have modified the solution in the Wiki to launch an event as the last line of the first macro, and used that event to launch the second macro. If anyone is interested, here is my code:

Code: Select all

<html>
	<meta charset="UTF-8">
    <body onload="window.setTimeout('document.getElementById(\'criimlaunch\').click();', 1000);">
        <script>
		//This function gets called once per macro (synchronously).  
		function main(macroId){
			var macroCode = "";
			switch(macroId){
			case 1:
				macroCode += 'PROMPT "First Macro"\n'; //Replace as necessary
				break;
			case 2:
				macroCode += 'PROMPT "Second Macro"\n'; //Replace as necessary
				break;
			default:
				// No more code to run
				return;
			}
			//Now append the macro code needed to generate a custom event as the last line of the macro
			macroCode += eventMacro(macroId);
			launchMacro(macroCode);
		}
		
		//This function adds the code needed to generate a custom event into the iMacros EVAL command
		function eventMacro(macroId){
			eventCode = "";
			eventCode += 'SET !VAR1 EVAL("' +
				//To allow the event listener to have the permissions to read the event detail, 
				//the detail must be cloned into the document.
				//See here: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent#Firing_from_privileged_code_to_non-privileged_code
				'var clonedDetail = cloneInto({id:'+macroId+'}, document.defaultView);' +
				//Now create the event (with the macro id number) and dispatch it
				'var event = new CustomEvent(\\"iMacrosFinished\\", {detail:clonedDetail});' +
				'document.dispatchEvent(event);")\n';
			return eventCode;
		}

		//This function runs the macro using the supplied code
        function launchMacro(macroCode){
            try{
                    if(!/^(?:chrome|https?|file)/.test(location)){
						alert('iMacros: Open webpage to run a macro.');
						return;
					}
			   
					var macro = {}; 
					macro.source = macroCode;
					macro.name = 'EmbeddedMacro';
					
					// create and dispatch the event
					var event = new CustomEvent('iMacrosRunMacro', {detail: macro});
					window.dispatchEvent(event);
			}catch(e){
                alert('iMacros error: '+e.toString());
            };
        }
		
		//Add a listener to catch the event created by the last line of the macro
		document.addEventListener("iMacrosFinished", function(e){
			//Call the main function requesting the next macro
			main(e.detail.id+1);
		});
		
        </script>

        <a id="criimlaunch" href="javascript:main(1);">Launch iMacros</a>

    </body>
</html>
Ah-ah...!, funny "Timing"...! :shock:

I did also a bit of "Thinking" a few hours ago for the following Thread:
- Re: Launch imacros from terminal using imacros://run/?m=
... where I mentioned (among other (possible) "Solutions")...:
chivracq wrote:Maybe you can play with the 'EventListener' and have several Macros play after each other...? :idea:
... and a few hours later, you come up with a POC, perfect, ah-ah...! :D

Thanks for sharing of course...! :D

(You never followed up on your previous Thread from 2 years ago btw... :shock: )

>>>

But hum..., I would actually eventually be "interested" by the "they ran in parallel with each other" Case...! 8)
Do you still have that first Version of your Script...? And were both Scripts running at the same time in the same FF Instance / same Tab...!? I'm very curious...! :twisted:
I know of a way to run a Macro in an "invisible" Tab that has not the Focus, based on a Bug..., but I've never seen/managed to get 2 Macros running at the same time in the same FF Instance... (Hum, I never really tried either, but that could be "interesting", ah-ah...!)
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
engeeaitch
Posts: 4
Joined: Sun Aug 14, 2016 3:54 pm

Re: How to launch a macro >50 lines from the command line.

Post by engeeaitch » Wed Sep 12, 2018 7:59 pm

Hi,

Here is the code I started with. It seems to produce multiple prompts (five in my case), probably because of some race condition, which is not what I wanted, and why I went for the event solution.

Code: Select all

<html>
	<meta charset="UTF-8">
    <body onload="window.setTimeout('document.getElementById(\'criimlaunch\').click();', 1000);">
        <script>
		//This function gets called once per macro (synchronously).  
		function main(){
			var macroCode = "";
			macroCode += 'PROMPT "First Macro"\n'; //Replace as necessary
			launchMacro(macroCode);
			macroCode += 'PROMPT "Second Macro"\n'; //Replace as necessary
			launchMacro(macroCode);
		}

		//This function runs the macro using the supplied code
        function launchMacro(macroCode){
            try{
                    if(!/^(?:chrome|https?|file)/.test(location)){
						alert('iMacros: Open webpage to run a macro.');
						return;
					}
			   
					var macro = {}; 
					macro.source = macroCode;
					macro.name = 'EmbeddedMacro';
					
					// create and dispatch the event
					var event = new CustomEvent('iMacrosRunMacro', {detail: macro});
					window.dispatchEvent(event);
			}catch(e){
                alert('iMacros error: '+e.toString());
            };
        }
		
        </script>

        <a id="criimlaunch" href="javascript:main();">Launch iMacros</a>

    </body>
</html>
chivracq
Posts: 8535
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to launch a macro >50 lines from the command line.

Post by chivracq » Wed Sep 12, 2018 10:19 pm

engeeaitch wrote:Hi,

Here is the code I started with. It seems to produce multiple prompts (five in my case), probably because of some race condition, which is not what I wanted, and why I went for the event solution.

Code: Select all

<html>
	<meta charset="UTF-8">
    <body onload="window.setTimeout('document.getElementById(\'criimlaunch\').click();', 1000);">
        <script>
		//This function gets called once per macro (synchronously).  
		function main(){
			var macroCode = "";
			macroCode += 'PROMPT "First Macro"\n'; //Replace as necessary
			launchMacro(macroCode);
			macroCode += 'PROMPT "Second Macro"\n'; //Replace as necessary
			launchMacro(macroCode);
		}

		//This function runs the macro using the supplied code
        function launchMacro(macroCode){
            try{
                    if(!/^(?:chrome|https?|file)/.test(location)){
						alert('iMacros: Open webpage to run a macro.');
						return;
					}
			   
					var macro = {}; 
					macro.source = macroCode;
					macro.name = 'EmbeddedMacro';
					
					// create and dispatch the event
					var event = new CustomEvent('iMacrosRunMacro', {detail: macro});
					window.dispatchEvent(event);
			}catch(e){
                alert('iMacros error: '+e.toString());
            };
        }
		
        </script>

        <a id="criimlaunch" href="javascript:main();">Launch iMacros</a>

    </body>
</html>
OK, Thanks, I'll have a look..., a bit curious to see how it behaves, ah-ah...! :wink:
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
groundfalling
Posts: 46
Joined: Tue Jul 01, 2014 1:26 pm

Re: How to launch a macro >50 lines from the command line.

Post by groundfalling » Thu Sep 13, 2018 3:36 am

engeeaitch wrote:iMacros for Firefox 10.0.2.1450
Browser Firefox 62.0
OS: Windows 10

I am using the suggestion in the Wiki for Command Line Support here but needed to run a macro that was more than 50 lines long. I tried splitting the macro into two and then launching the macros one after the other, but that didn’t work, because they ran in parallel with each other, not sequentially. So I have modified the solution in the Wiki to launch an event as the last line of the first macro, and used that event to launch the second macro. If anyone is interested, here is my code:

Code: Select all

<html>
	<meta charset="UTF-8">
    <body onload="window.setTimeout('document.getElementById(\'criimlaunch\').click();', 1000);">
        <script>
		//This function gets called once per macro (synchronously).  
		function main(macroId){
			var macroCode = "";
			switch(macroId){
			case 1:
				macroCode += 'PROMPT "First Macro"\n'; //Replace as necessary
				break;
			case 2:
				macroCode += 'PROMPT "Second Macro"\n'; //Replace as necessary
				break;
			default:
				// No more code to run
				return;
			}
			//Now append the macro code needed to generate a custom event as the last line of the macro
			macroCode += eventMacro(macroId);
			launchMacro(macroCode);
		}
		
		//This function adds the code needed to generate a custom event into the iMacros EVAL command
		function eventMacro(macroId){
			eventCode = "";
			eventCode += 'SET !VAR1 EVAL("' +
				//To allow the event listener to have the permissions to read the event detail, 
				//the detail must be cloned into the document.
				//See here: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent#Firing_from_privileged_code_to_non-privileged_code
				'var clonedDetail = cloneInto({id:'+macroId+'}, document.defaultView);' +
				//Now create the event (with the macro id number) and dispatch it
				'var event = new CustomEvent(\\"iMacrosFinished\\", {detail:clonedDetail});' +
				'document.dispatchEvent(event);")\n';
			return eventCode;
		}

		//This function runs the macro using the supplied code
        function launchMacro(macroCode){
            try{
                    if(!/^(?:chrome|https?|file)/.test(location)){
						alert('iMacros: Open webpage to run a macro.');
						return;
					}
			   
					var macro = {}; 
					macro.source = macroCode;
					macro.name = 'EmbeddedMacro';
					
					// create and dispatch the event
					var event = new CustomEvent('iMacrosRunMacro', {detail: macro});
					window.dispatchEvent(event);
			}catch(e){
                alert('iMacros error: '+e.toString());
            };
        }
		
		//Add a listener to catch the event created by the last line of the macro
		document.addEventListener("iMacrosFinished", function(e){
			//Call the main function requesting the next macro
			main(e.detail.id+1);
		});
		
        </script>

        <a id="criimlaunch" href="javascript:main(1);">Launch iMacros</a>

    </body>
</html>
Hi,
When i replace this:

Code: Select all

case 1:
				macroCode += 'PROMPT "First Macro"\n'; //Replace as necessary
				break;
with this:

Code: Select all

case 1:
				macroCode += 'url GOTO=https://forum.imacros.net/\n'; //Replace as necessary
				break;
it does not goto case 2 after loading the url and macros stop.
engeeaitch
Posts: 4
Joined: Sun Aug 14, 2016 3:54 pm

Re: How to launch a macro >50 lines from the command line.

Post by engeeaitch » Thu Sep 13, 2018 8:24 pm

@groundfalling Apologies - you're right. I need to re-think all of this. I've got a specific use case that works for me - but not for the more general case. Back to the drawing board.
groundfalling
Posts: 46
Joined: Tue Jul 01, 2014 1:26 pm

Re: How to launch a macro >50 lines from the command line.

Post by groundfalling » Fri Sep 14, 2018 7:39 am

engeeaitch wrote:@groundfalling Apologies - you're right. I need to re-think all of this. I've got a specific use case that works for me - but not for the more general case. Back to the drawing board.
@engeeaitch sorry off the topic, it seems that you are a JS person, do you happen to know the correct syntax to use this EVAL in JS in .htm with iMacros 10.0.2.1450

Code: Select all

SET pass EVAL("if (\"{{!EXTRACT}}\" == \"#EANF#\") {var x = 'ctrl';} else {var x = '';} x;")
PROMPT {{pass}}
Last edited by groundfalling on Fri Sep 14, 2018 7:46 am, edited 1 time in total.
groundfalling
Posts: 46
Joined: Tue Jul 01, 2014 1:26 pm

Re: How to launch a macro >50 lines from the command line.

Post by groundfalling » Fri Sep 14, 2018 7:42 am

i tried this:

Code: Select all

macroCode += 'SET pass EVAL(\"if (\\\"{{!EXTRACT}}\\\" == \"#EANF#\") {var x = 'ctrl';} else {var x = '';} x;\")\n';
macroCode += 'PROMPT {{pass}}\n';
and this:

Code: Select all

macroCode += 'SET pass EVAL(\"if (\"{{!EXTRACT}}\" == \"#EANF#\") {var x = 'ctrl';} else {var x = '';} x;\")\n';
macroCode += 'PROMPT {{pass}}\n';
it doesn't work.
Post Reply