Loop imacros and csv with javascript

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
lumer8
Posts: 17
Joined: Tue Sep 02, 2014 3:15 am

Loop imacros and csv with javascript

Post by lumer8 » Fri Oct 03, 2014 4:10 am

I need 4 imacros to all be played in 1 click , i successfully created that part with javascript. Each imacros will need to be looped 5 times, it is sending info from a csv file so it should send out 5 lines from the csv except it keeps only sending the 1st line of the csv 5 times when i try to do it with javascript. Keep in mind it works without javascript so im sure the imacros are fine. Can someone please help me out, im stuck and cant figure it out.

Code: Select all

iimPlay("Dummy1.iim");
iimPlay("Dummy2.iim");
iimPlay("Dummy3.iim");
iimPlay("Dummy4.iim");
skippyto
Posts: 108
Joined: Wed Aug 20, 2008 8:14 am

Re: Loop imacros and csv with javascript

Post by skippyto » Fri Oct 03, 2014 6:36 am

Hi lumer8

Just use a for loop in your javascript :

Code: Select all

for (csvLine = 1 ; csvLine <= 5 ; csvLine++)
{
	iimSet ("-var_CSVLINE", csvLine);
	iimPlay("Dummy1.iim");
	iimSet ("-var_CSVLINE", csvLine);
	iimPlay("Dummy2.iim");
	iimSet ("-var_CSVLINE", csvLine);
	iimPlay("Dummy3.iim");
	iimSet ("-var_CSVLINE", csvLine);
	iimPlay("Dummy4.iim");
}
Then use !DATASOURCE_LINE (http://wiki.imacros.net/!DATASOURCE_LINE) in your iim to set the line :

Code: Select all

SET !DATASOURCE_LINE {{CSVLINE}} 
Skippyto
chivracq
Posts: 8765
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop imacros and csv with javascript

Post by chivracq » Fri Oct 03, 2014 12:24 pm

lumer8 wrote:I need 4 imacros to all be played in 1 click , i successfully created that part with javascript. Each imacros will need to be looped 5 times, it is sending info from a csv file so it should send out 5 lines from the csv except it keeps only sending the 1st line of the csv 5 times when i try to do it with javascript. Keep in mind it works without javascript so im sure the imacros are fine. Can someone please help me out, im stuck and cant figure it out.

Code: Select all

iimPlay("Dummy1.iim");
iimPlay("Dummy2.iim");
iimPlay("Dummy3.iim");
iimPlay("Dummy4.iim");
CIM...! :mrgreen:

I'm not sure (if it makes a difference...) if you need Dymmy1.iim to be looped 5 times, then Dymmy2.iim to be looped 5 time, etc... or if Dummy1/2/3/4.iim can first run with Row1, then Dummy1/2/3/4 with Row2, etc..., then 'Skippyto''s Solution is the "clean" .js Solution for the 2nd case, otherwise you'd need a separate 'for' Loop for each 'Dummyx.iim' Macro, a "dirty" Trick for the 1st case (each Macro looping separately) would be to use Negative !LOOP in each .iim Macro. (Then you don't need to handle any Looping from the .js Script, each .iim Macro should be auto-looping... (Though I've never tried...)):

Code: Select all

SET !LOOP -3
SET MyLoop {{!LOOP}}
ADD MyLoop 4
SET !DATASOURCE_LINE {{MyLoop}}
(Assuming your .CSV File doesn't have a Header, use "+5" if it has a Header...)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
lumer8
Posts: 17
Joined: Tue Sep 02, 2014 3:15 am

Re: Loop imacros and csv with javascript

Post by lumer8 » Mon Oct 06, 2014 1:47 am

I tried both of your suggestions and they didnt work?!

Here is my code , it loops each imacros 5 times but the csv file isnt looping inside the imacros and keeps sending out only the first line, hope someone can help me with my problem.

Code: Select all

for (i=1;i<=5;i++){iimPlay("Dummy1.iim");}
for (i=1;i<=5;i++){iimPlay("Dummy2.iim");}
for (i=1;i<=5;i++){iimPlay("Dummy3.iim");}
for (i=1;i<=5;i++){iimPlay("Dummy4.iim");}
lumer8
Posts: 17
Joined: Tue Sep 02, 2014 3:15 am

Re: Loop imacros and csv with javascript

Post by lumer8 » Mon Oct 06, 2014 2:31 am

Nevermind i just retried Skippyto's method and it works like a charm. Thanks alot! I appreciate it. :D
chivracq
Posts: 8765
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop imacros and csv with javascript

Post by chivracq » Mon Oct 06, 2014 2:42 am

CIM again...! :mrgreen:
lumer8 wrote:I tried both of your suggestions and they didnt work?!
I wonder what didn't work...? Did you get any RuntimeError or what...?
I tested mine and it works...
lumer8 wrote:Here is my code , it loops each imacros 5 times but the csv file isnt looping inside the imacros and keeps sending out only the first line, hope someone can help me with my problem.

Code: Select all

for (i=1;i<=5;i++){iimPlay("Dummy1.iim");}
for (i=1;i<=5;i++){iimPlay("Dummy2.iim");}
for (i=1;i<=5;i++){iimPlay("Dummy3.iim");}
for (i=1;i<=5;i++){iimPlay("Dummy4.iim");}
Yep, normal, you've dropped the 'CSVLINE' part from 'Skippyto''s Script...

For my Solution, you don't need any 'for' Loop in your .js Script, you use directly the original .js Script you posted in your first Post, the Looping through the .CSV File is handled from within each .iim Script.

EDIT:
Ah...!, you've posted again while I was typing mine...:
lumer8 wrote:Nevermind i just retried Skippyto's method and it works like a charm. Thanks alot! I appreciate it. :D
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
provoquestosito
Posts: 75
Joined: Sat Jan 30, 2010 12:43 am

Re: Loop imacros and csv with javascript

Post by provoquestosito » Fri Mar 13, 2015 2:52 am

Hi
@skippyto @chivracq
I tryed your solution and do not work, do not looping the csv row.

Can you post other working solution please?
chivracq
Posts: 8765
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop imacros and csv with javascript

Post by chivracq » Fri Mar 13, 2015 3:29 pm

provoquestosito wrote:Hi
@skippyto @chivracq
I tryed your solution and do not work, do not looping the csv row.

Can you post other working solution please?
CIM...! :mrgreen: for you too, you are not a Newbie anymore, I don't dig into/answer any Thread when People don't mention their FCI...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
provoquestosito
Posts: 75
Joined: Sat Jan 30, 2010 12:43 am

Re: Loop imacros and csv with javascript

Post by provoquestosito » Fri Mar 13, 2015 5:28 pm

Thanks for reply @chivracq

I using imacros 8.8.8 and from some days i search a macro to using loop in csv.

VERSION BUILD=8881205 RECORDER=FX

My problems is to create a macro to start from csv row 100 and stop at csv row 200.

Actually i tryed some solution but i do not find any solution in jvascript, with .iim i create a good solution but i must add max loop manually.

THIS IS MY simplemacro.iim and works but i must add manually the max loop

Code: Select all

SET !ERRORIGNORE YES
SET !LOADCHECK YES
SET !REPLAYSPEED FAST
SET !POINTER YES
SET !TIMEOUT 40
SET !TIMEOUT_TAG 0
SET !EXTRACT_TEST_POPUP NO

'/// Csv Settings ///
CMDLINE !DATASOURCE proxy_extracted.csv
SET !DATASOURCE_COLUMNS 1
SET !LOOP 100
SET !DATASOURCE_LINE {{!LOOP}}

'/// Set Proxy ///
CLEAR
PROXY ADDRESS={{!COL1}}
WAIT SECONDS=1

'/// Load site ///
TAB T=1
URL GOTO=http://www.google.com
WAIT SECONDS=1
Javascript macro tryed but do not recognize the start csv row and start from csv row 1

Code: Select all

for (csvLine = 1 ; csvLine <= 100 ; csvLine++)
{
   iimSet ("-var_CSVLINE", csvLine);
   iimPlay("simplemacro.iim");
}

I search a javascript solution to create different macros with differents start loop and end loop (ex. macro 1 start from csv row 1 and loop for 100 times, macro2 start from 101 csv row and loop for 100 times).

Solution for me is
macro1.js - start from csv row 1 and loop from 100 times (end csv row 100)
macro2.js - start from csv row 101 and loop from 100 times (end csv row 200)
macro3.js - start from csv row 201 and loop from 100 times (end csv row 300)

Can you help me please?

Thanks, best regards
provoquestosito
Posts: 75
Joined: Sat Jan 30, 2010 12:43 am

Re: Loop imacros and csv with javascript

Post by provoquestosito » Sat Mar 14, 2015 1:30 pm

@skippyto

Can you post example of your solution please ?

What is this ? -> SET !DATASOURCE_LINE {{CSVLINE}}

What value i must add ?

What is this -> iimSet ("-var_CSVLINE", csvLine);

Help me please

Best regards
chivracq
Posts: 8765
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Loop imacros and csv with javascript

Post by chivracq » Sat Mar 14, 2015 6:45 pm

provoquestosito wrote:@skippyto

Can you post example of your solution please ?

What is this ? -> SET !DATASOURCE_LINE {{CSVLINE}}

What value i must add ?

What is this -> iimSet ("-var_CSVLINE", csvLine);

Help me please

Best regards
Pfff, is it so difficult to read one day the Forum Rules and my Sig...!? => FCIM...!? :mrgreen:

OK, you have a working .iim Script, which looks OK indeed to me, you just need to use the "SET !LOOP 100" like you do to start looping from Row_100, it's not a big deal to have to fill in manually the Max_Loop... But from your .iim Script, 5 of the 7 Commands you use have been deprecated, some in 2008 already, so that means you've been using iMacros for at least 7 years now, you shouldn't need any Help, you should be answering Questions on the Forum..., and/or only coming to the Forum to report Bugs, what you now want is fairly Basic Use of iMacros...

I don't use .js Scripts myself, but OK, you found the:
What is this ? -> SET !DATASOURCE_LINE {{CSVLINE}}

What value i must add ?

What is this -> iimSet ("-var_CSVLINE", csvLine);
You define a Var in your .js Script that you call 'CSVLINE' and you reuse that very same Value in your .iim Script instead of '{{!LOOP}}', read the Wiki a bit about "Passing Arguments", it's all explained there... Read the Wiki to understand the mechanism... :idea:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
giuseppe
Posts: 40
Joined: Sat Oct 15, 2016 3:30 pm

Re: Loop imacros and csv with javascript

Post by giuseppe » Mon Oct 17, 2016 2:25 pm

hi @chivracq and skippyto and all the community,
Two days ago I opened a new thread in order understand how to do LOOP using javascript.
Now I have seen this post, so I write my question here, and I am going to delete the thread I opened 2 days ago.

I have the following macro in .iim
VERSION BUILD=8601111 RECORDER=FX
TAB T=1
SET !EXTRACT NULL
SET !ERRORIGNORE YES
SET !ERRORCONTINUE YES
SET !DATASOURCE file.csv
SET !DATASOURCE_COLUMNS 1
SET !DATASOURCE_LINE {{!LOOP}}
URL GOTO={{!COL1}}
In the file.csv (located of course in dasourse folder) there are 3 url. For example:
http://www.google.com
http://www.yahoo.com
http://www.mozilla.org

I execute it in LOOP MODE and it run well.

Now, I need run it in .js
I have read all the post, and I have done one hundred test using all the suggestions, but all my macros dont run...
I have read in other forums, and my last version of my macro is the following (it dont run...)

var macro, iret;

function FirstMacro()

{
macro = "CODE:";
macro += "VERSION BUILD=8601111 RECORDER=FX\n"
macro += "TAB T=1\n"
macro += "SET !EXTRACT NULL\n"
macro += "SET !ERRORIGNORE YES\n"
macro += "SET !ERRORCONTINUE YES\n"
macro += "SET !DATASOURCE file.csv\n"
macro += "SET !DATASOURCE_COLUMNS 1\n"
macro += "SET !DATASOURCE_LINE {{!LOOP}}\n"
macro += "URL GOTO={{!COL1}}\n"

iret = iimPlay(macro)

}

for(i = 1 ; i <= 3 ; i++)
{
iimSet("loop", i);
FirstMacro();
}
I need to visite these 3 pages in a sequential mode, but with the code I paste here, I only visite the first url 3 times. It repeats 3 times the same url (only the first url...).

Can you help me?
Please, if it is no a problem, I apreciate if you paste all the code (from first strig to the last string), so I will not have a bad interpretation.
Thanks a a lot for your support.

CIM are:
iMacros Firefox 8.9.7 / Browser Firefox 49.0.1 / Windows 7
I dont use the last version of iMacro because chivracq told me it is not a stable version :mrgreen:
CIM are:
iMacros Firefox 9.0.3 / Browser Firefox 52.2.1 / Windows Vista
giuseppe
Posts: 40
Joined: Sat Oct 15, 2016 3:30 pm

Re: Loop imacros and csv with javascript

Post by giuseppe » Sun Oct 23, 2016 6:35 am

Hi, thanks to techimac this post is resolved.
The solution is the following:

var FirstMacro = "";
FirstMacro = "CODE:";
FirstMacro += "TAB T=1 + "\n";
FirstMacro += "SET !EXTRACT NULL + "\n";
FirstMacro += "SET !ERRORIGNORE YES + "\n";
FirstMacro += "SET !ERRORCONTINUE YES + "\n";
FirstMacro += "SET !DATASOURCE file.csv + "\n";
FirstMacro += "SET !DATASOURCE_COLUMNS 1 + "\n";
FirstMacro += "SET !DATASOURCE_LINE {{LOOPNUM}} + "\n";
FirstMacro += "URL GOTO={{!COL1}} + "\n";

for(var i=1; i<101; i++)
{
iimSet("LOOPNUM", i)
iimPlay(FirstMacro);
}

If you want repeat only a part of macro, you can see the following post:
http://forum.imacros.net/viewtopic.php?f=11&t=26815
Thanks for your support.
CIM are:
iMacros Firefox 9.0.3 / Browser Firefox 52.2.1 / Windows Vista
Post Reply