Close browser after done looping in js

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

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
justaguyhere
Posts: 6
Joined: Fri Nov 03, 2017 4:17 pm

Close browser after done looping in js

Post by justaguyhere » Fri Nov 03, 2017 4:39 pm

I'm using Pale Moon 27.5.1, Imacros 8.9.7 for firefox, and Windows Server 2012.

I currently use a batch file, which goes to a .js and opens my macro to loop 10 times. I want to automatically close palemoon once the macro is done looping so next time it runs it will be able to launch the browser again.

This is my .js code:

Code: Select all

var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i);
     iimPlay("MyMacro");
}
Is there a code I can add here to close the pale moon browser opened by the batch file, after the loops are complete?

EDIT: After researching, it appears the best way may be to close it through the batch file. I have experimented with TIMEOUT /T 30 and in the command line it shows the countdown and closes the browser. When I put 2000 instead, it does not show in the cmd box, and there is no countdown or shutdown. I am doing 10 loops and each can take anywhere from 1 minute to 5 minutes if the server is clogged up.

The timeout counter doesnt start under I manually close the browser

Code: Select all

start /wait firefox.exe imacros://run/?m="UsersDLoop.js"
TIMEOUT /T 2000 /NOBREAK
Taskkill /IM "firefox.exe" /F
Exit
Last edited by justaguyhere on Fri Nov 03, 2017 6:28 pm, edited 1 time in total.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Close browser after done looping in js

Post by chivracq » Fri Nov 03, 2017 6:20 pm

justaguyhere wrote:I'm using

Code: Select all

Pale Moon 27.5.1, Imacros 8.9.7 for firefox, and Windows Server 2012.
I currently use a batch file, which goes to a .js and opens my macro to loop 10 times. I want to automatically close palemoon once the macro is done looping so next time it runs it will be able to launch the browser again.

This is my .js code:

Code: Select all

var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i);
     iimPlay("MyMacro");
}
Is there a code I can add here to close the pale moon browser opened by the batch file, after the loops are complete?
Yep, you can either close the Browser from your Batch Fiel with a 'kill' or from your Script itself, search my Posts on "Browser + Close" as Search Keywords I would think, I've posted once a Method to do so (Conditionally, but that won't even be needed for you, I guess...) from a Script (in pure '.iim', but easy to adapt for '.js'), "my" Method was actually to close one Tab, but closing the last Tab would close the Browser, and I think that in the same Thread other Advanced Users posted their "best" way to close the Browser and/or I must have linked to other relevant Threads...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Close browser after done looping in js

Post by chivracq » Fri Nov 03, 2017 6:32 pm

OK, I'm being nice, here is the Thread I was referring to, and indeed, my last Post in the Thread contains Links to several (and probably all) relevant Threads...:
- Re: How to close the iMacros Browser?
(Option 4. for you if you want to go quick to the Solution... :wink: )
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
justaguyhere
Posts: 6
Joined: Fri Nov 03, 2017 4:17 pm

Re: Close browser after done looping in js

Post by justaguyhere » Fri Nov 03, 2017 6:46 pm

chivracq wrote:OK, I'm being nice, here is the Thread I was referring to, and indeed, my last Post in the Thread contains Links to several (and probably all) relevant Threads...:
- Re: How to close the iMacros Browser?
(Option 4. for you if you want to go quick to the Solution... :wink: )

Thank you very much. I was just reading through your post search results :)

Code: Select all

URL GOTO=javascript:this.focus();self.opener=this;self.close()
I see that you had this solution work. I am going to play around with this and modifying my JS to load this at the right time. I will let you know how it goes.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Close browser after done looping in js

Post by chivracq » Fri Nov 03, 2017 7:17 pm

justaguyhere wrote:Thank you very much. I was just reading through your post search results :)

Code: Select all

URL GOTO=javascript:this.focus();self.opener=this;self.close()
I see that you had this solution work. I am going to play around with this and modifying my JS to load this at the right time. I will let you know how it goes.
Yep, see what Solution you choose in the end, I am no JS Guru myself, a few other Advanced Users (and certainly more advanced than me on '.js') were "pushing" for some other Solutions, check them all I would say and choose the one that works best for you..., and nice indeed afterwards if you can post your "Results" and your final Script, for other Users with a similar Case/Qt...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
justaguyhere
Posts: 6
Joined: Fri Nov 03, 2017 4:17 pm

Re: Close browser after done looping in js

Post by justaguyhere » Fri Nov 03, 2017 7:32 pm

I decided to test on Tor Browser first since its more complicated and an easy way to have 2 IPs/imacros running at once. Your option 4 did not work in Tor, but a CTRL+W keypress closed the window successfully, running an .iim just for that after the loop ended. I then use a taskkill firefox.exe (when using TOR) at the BEGINNING of my batch file, so I can be sure it is closed when the batch file is scheduled next. I will test your code on Pale Moon after work and let you know if I make more progress.
justaguyhere
Posts: 6
Joined: Fri Nov 03, 2017 4:17 pm

Re: Close browser after done looping in js

Post by justaguyhere » Fri Nov 03, 2017 9:22 pm

chivracq wrote:
justaguyhere wrote:Thank you very much. I was just reading through your post search results :)

Code: Select all

URL GOTO=javascript:this.focus();self.opener=this;self.close()
I see that you had this solution work. I am going to play around with this and modifying my JS to load this at the right time. I will let you know how it goes.
Yep, see what Solution you choose in the end, I am no JS Guru myself, a few other Advanced Users (and certainly more advanced than me on '.js') were "pushing" for some other Solutions, check them all I would say and choose the one that works best for you..., and nice indeed afterwards if you can post your "Results" and your final Script, for other Users with a similar Case/Qt...

I ended up using this at my batch:

Code: Select all

taskkill /F /IM palemoon.exe
cd c:\Program Files (x86)\Pale Moon
start /wait palemoon.exe -P "Name" -no-remote imacros://run/?m="Name.js"
This is my .js

Code: Select all

var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i); 
     iimPlay("Name");
} iimPlay("CloseFF.iim")
and this is CloseFF

Code: Select all

EVENT TYPE=KEYPRESS CHAR="w" MODIFIERS="ctrl"

The code works in Palemoon, in TOR it works to close the window but causes a crash or TOR about 60 seconds later, as I've read about in other posts. It works for me, but is not ideal obviously.

It seems like a simple way would be to replace the iimPlay("CloseFF.iim") with a batch file that kills the task but not sure if thats possible.
justaguyhere
Posts: 6
Joined: Fri Nov 03, 2017 4:17 pm

Re: Close browser after done looping in js

Post by justaguyhere » Fri Nov 03, 2017 9:31 pm

Interesting, I found

Code: Select all

var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\Users\\Administrator\\Desktop\\test.bat");
file.launch();
which seems to be a way to launch the bat from my JS. you participated in that thread. I am not familiar with JS at all, this thread is my first .js file ever.
I need to edit the path obviously, then make sure the mozilla.org part works with palemoon and firefox (tor browser), correct? Any input would be appreciated.

I also need to make sure it runs at the right time, replacing the CloseFF line with this code should do that since it worked for the iim.

EDIT: Running code in Pale Moon did indeed close browser, but did so before running macro. Back to where I was before
EDIT2: Code below works in Pale Moon! I just had to move the } from after Name"); to the end of the code. Thanks!
EDIT3: Same code works in TorBrowser also with close2.bat and firefox.exe instead of palemoon.exe

Code: Select all

var i;
for (i = 1; i < 10; i++) {
     iimSet("loop", i); 
     iimPlay("Name");
} 
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\close.bat");
file.launch();
justaguyhere
Posts: 6
Joined: Fri Nov 03, 2017 4:17 pm

Re: Close browser after done looping in js

Post by justaguyhere » Sat Nov 04, 2017 2:21 am

Well I am in shock here.. while the code worked perfectly this afternoon, it now closes the browser without looping. I don't think I've changed anything. This is extremely frustrating.

EDIT: AH I am spending too much time looking at code. Too many or not enough { } in the js
Post Reply