How to use EVAL to Stop the Macro when there is no Next?

Support for iMacros. The iMacros software is the unique solution for automating every activity inside a web browser, for data extraction and web testing.
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
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Tue Oct 03, 2017 9:22 pm

Hello;
Imacros v8.9.7 on FF ver 55.0.3 32-bit, Win 7 32
I run a js file that runs 2 macros, there is an If statement and a loop,
the macro simply do a search on google and yahoo.
and click on next button to go to the next result page,
but when there is no more pages, script keep working.
I want a IF statement in imacros ,
when there is no next button/ next text, stop the loop.

here is the js I have:
iimPlay("myMacro1 run");
var retcode;
retcode = iimPlay("myMacro1 runs")

if (retcode > 0) {
retcode = iimPlay("myMacro2 work")
}
iimDisplay (retcode);

for (var i=0;i<=9;i++) {
iimPlay("myMacro2 work");
}
a simple yahoo search macro:
VERSION BUILD=8970419 RECORDER=FX
TAB T=1
URL GOTO=https://us.yahoo.com/
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:uh-search-form ATTR=ID:uh-search-box CONTENT=Dog<SP>grooming
TAG POS=1 TYPE=BUTTON FORM=ID:uh-search-form ATTR=ID:uh-search-button
TAG POS=1 TYPE=A ATTR=TXT:Next
for the google the next button is different:
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT=Dog<SP>grooming
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK
TAG POS=1 TYPE=A ATTR=ID:hdtb-tls
TAG POS=1 TYPE=A ATTR=ID:PNNEXT
I tried some Eval example from this forum, but could not make it work for this.
anyone can help me on this?
appreciate it in advance.
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Thu Oct 05, 2017 3:14 pm

it seems my request is very complicated, I changed the subject

I tried the following, but I don't know how to proceed.
SET !NX_BT PNNEXT

TAG POS=1 TYPE=A ATTR=ID:PNNEXT EXTRACT=TXT
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}\"; if(text==\"!NX_BT\") Here I don't know to code it to continue, Else stop )
'if the code recognize NEXT button then it should continue the next command which is click the next

TAG POS=1 TYPE=A ATTR=ID:PNNEXT
Anyone Can help me with the rest of the code?
thanks
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by chivracq » Thu Oct 05, 2017 4:14 pm

green.pine wrote:Hello;
Imacros v8.9.7 on FF ver 55.0.3 32-bit, Win 7 32
I run a js file that runs 2 macros, there is an If statement and a loop,
the macro simply do a search on google and yahoo.
and click on next button to go to the next result page,
but when there is no more pages, script keep working.
I want a IF statement in imacros ,
when there is no next button/ next text, stop the loop.

here is the js I have:

Code: Select all

iimPlay("myMacro1 run");
var retcode;
retcode = iimPlay("myMacro1 runs")

if (retcode > 0) {
retcode = iimPlay("myMacro2 work")
}
iimDisplay (retcode);

for (var i=0;i<=9;i++) {
iimPlay("myMacro2 work");
}
a simple yahoo search macro:

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
URL GOTO=https://us.yahoo.com/
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:uh-search-form ATTR=ID:uh-search-box CONTENT=Dog<SP>grooming
TAG POS=1 TYPE=BUTTON FORM=ID:uh-search-form ATTR=ID:uh-search-button
TAG POS=1 TYPE=A ATTR=TXT:Next
for the google the next button is different:

Code: Select all

TAG POS=1 TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT=Dog<SP>grooming
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK
TAG POS=1 TYPE=A ATTR=ID:hdtb-tls
TAG POS=1 TYPE=A ATTR=ID:PNNEXT
I tried some Eval example from this forum, but could not make it work for this.
anyone can help me on this?
appreciate it in advance.
green.pine wrote:it seems my request is very complicated, I changed the subject

I tried the following, but I don't know how to proceed.

Code: Select all

SET !NX_BT PNNEXT

TAG POS=1 TYPE=A ATTR=ID:PNNEXT EXTRACT=TXT
SET !VAR1 EVAL("var text=\"{{!EXTRACT}}\"; if(text==\"!NX_BT\") Here I don't know to code it to continue, Else stop )
'if the code recognize NEXT button then it should continue the next command which is click the next

TAG POS=1 TYPE=A ATTR=ID:PNNEXT
Anyone Can help me with the rest of the code?
thanks
Well, no, it's not very complicated, but, pfff..., I don't really do '.js' Scripts as I find them cumbersome and unnecessarily complicated compared to pure '.iim'...

But OK, you simply need to "follow" the Workflow of your '.js' Script. What you told it to do is currently the following (from your 1st Post):

Code: Select all

1- Play Macro_1.
2- Play Macro_2.
3- If 'Play Macro_2' went OK, => Play Macro_3.
4- Play Macro_3 x9 times.
This is what your Script is doing. If that's correct, then OK, otherwise you must adapt that "Workflow" to what you want exactly...
But I guess you probably want a 'while' instead of a 'for' for your Loop, but you need to include a Check/Condition for the 'while' to stop...
Now what your Script is doing is 'Play Macro_3' once Conditionally, based on 'Macro_2', and then whatever happens, 'Play Macro_3' x9 times.

Hum, and you were mentioning "I run a js file that runs 2 macros,...", but your '.js' Script is actually with 3 different '.iim' Scripts... Hum...!?! :?

Your 2nd Approach in your 2nd Post looks a bit cumbersome to me as well... And watch out with using '!' in your own User-Defined Vars, this Char is normally reserved for Built-in Vars..., and your first Var is not declared...
But yep, checking on the Presence of the 'Next' Button is a way to go, then you need to use 'MacroError()' within the 'EVAL()' to stop that '..iim'.
But as you play your '.iim' from within a '.js' Script, I think that will only stop the '.iim' and the main '.js' Workflow will continue. Trying to click on that 'Next' Button from the '.iim' with '!ERRORIGNORE' disabled was already doing the same thing...

If you want to make a Check on that 'Next' Button, you need to pass the Content of the 'EXTRACT' to the '.js' Script (well, is already done automatically) and handle the further Processing/Workflow from the '.js', based on the Content of 'iimGetExtract()' or 'iimGetLastError()', but directing your Workflow on the 'RetCode' for that '.iim' was already probably good enough...
- (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...
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Fri Oct 06, 2017 2:55 pm

Thank you for your reply;
I tried to do it on Javascript as you mentioned that I have a better chance with passing the Next button extract to javascript,
I don't know about js, I tried to find something that can be used to my need.
I found some script and changed it around, but when it runs, I get some error, hopefully you or other people can help me with right codes to make it work,
this is the error message:
ReferenceError: Macro2 is not defined, line 14 (Error code: -991)
Line 14 is a line after Do function :
iimPlay(Macro2);

I am not sure how to do it,
I have put a variable on top for macro2 to define it:
var Macro2;
but I get another error
TypeError: path is undefined, line -631 (Error code: -991)


totally I have 3 macro files,
macro 1: do the google search, on TAB1, go to TAB2 and paste some text there, back to TAB1
Macro 2: it just extract the NEXT button of Google result, on TAB1,if there is any,
macro 3: on TAB1 click Next, copy some text, go to TAB 2 and paste it there, stops after doing it for say 6 times.

anyhow Here is my js file content:
iimPlay("Macro1");
var retcode;
retcode = iimPlay("Macro1 runs")

// I think the following if statement is useless!
if (retcode = 0) {
retcode = iimPlay("Macro1")
}

iimDisplay (retcode);
iimPlay("Macro2");

do{

iimPlay(Macro2);
var macro2Extract = iimGetLastExtract();

if (macro2Extract !== 'Next') {
break;
}
for(var j = 1; j <= 6; j++){
iimSet("j",j);
iimPlay(Macro2);
}
iimPlay(Macro3);
}while (true);
Tank you for your help;
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by chivracq » Fri Oct 06, 2017 5:37 pm

green.pine wrote:Thank you for your reply;
I tried to do it on Javascript as you mentioned that I have a better chance with passing the Next button extract to javascript,
I don't know about js, I tried to find something that can be used to my need.
I found some script and changed it around, but when it runs, I get some error, hopefully you or other people can help me with right codes to make it work,
this is the error message:
ReferenceError: Macro2 is not defined, line 14 (Error code: -991)
Line 14 is a line after Do function :
iimPlay(Macro2);

I am not sure how to do it,
I have put a variable on top for macro2 to define it:
var Macro2;
but I get another error
TypeError: path is undefined, line -631 (Error code: -991)


totally I have 3 macro files,
macro 1: do the google search, on TAB1, go to TAB2 and paste some text there, back to TAB1
Macro 2: it just extract the NEXT button of Google result, on TAB1,if there is any,
macro 3: on TAB1 click Next, copy some text, go to TAB 2 and paste it there, stops after doing it for say 6 times.

anyhow Here is my js file content:

Code: Select all

iimPlay("Macro1");
var retcode;
retcode = iimPlay("Macro1 runs")

// I think the following if statement is useless!
if (retcode = 0) {
retcode = iimPlay("Macro1")
}

iimDisplay (retcode);
iimPlay("Macro2");

do{
 
    iimPlay(Macro2);
    var macro2Extract = iimGetLastExtract();

    if (macro2Extract !== 'Next') {
         break;
    }
    for(var j = 1; j <= 6; j++){
        iimSet("j",j);
        iimPlay(Macro2);
    }
    iimPlay(Macro3);
}while (true);
Tank you for your help;
Yeah..., very cumbersome, ah-ah...! And you mention 3 '.iim' Macros but your '.js' Script uses 4 different Names now...

But OK, I find it very cumbersome, here is an Example for an "easy" Solution in pure '.iim' in just one single '.iim' Script, applied to your Google Search, easy to adapt for the Yahoo Search...:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
SET !ERRORIGNORE YES
SET !EXTRACT_TEST_POPUP NO
TAB T=1

'Easy Access for Vars:
'**********************
SET URL_Search http://www.google.com/
SET Search_Content "Dog grooming"

SET !LOOP 1


'Do the Search only for 'Loop=1':
'*********************************
'Recorded:
'**********
'URL GOTO=http://www.google.com/
'TAG POS=1 TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT=Dog<SP>grooming
'TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK
'TAG POS=1 TYPE=A ATTR=ID:hdtb-tls // (Click on 'Tools'.)
'TAG POS=1 TYPE=A ATTR=ID:PNNEXT

'=> Compute a 1/0 to use for '!TIMEOUT_STEP' and 'POS=n':
SET LOOP_Switch EVAL("var n='{{!LOOP}}'; var x,y,z; if(n==1){z=1;} else{z=0;}; z;")
SET !TIMEOUT_STEP {{LOOP_Switch}}
'>
'=> Define URL to use: (The empty String won't do anything for 'Loop>1'...!)
SET URL_GOTO EVAL("var n='{{!LOOP}}', u='{{URL_Search}}'; var x,y,z; if(n==1){z=u;} else{z='';}; z;")
URL GOTO={{URL_GOTO}}
'>
'Do the Search: ('POS=0' won't do anything for 'Loop>1'...!)
TAG POS={{LOOP_Switch}} TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT={{Search_Content}}
TAG POS={{LOOP_Switch}} TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK


'Now the part for all Loops:
'****************************
SET !TIMEOUT_STEP 1

'Part on TAB_1:
'***************
TAB T=1
WAIT SECONDS=2

'Click on 'Tools':
TAG POS=1 TYPE=A ATTR=ID:hdtb-tls

'Extract and copy the Content from TAB_1 that we want to keep:
SET !EXTRACT NULL
'TAG POS=1 ....... EXTRACT=TXT
SET Extracted_Data {{!EXTRACT}}
'...
'...


'Part on TAB_2:
'***************
TAB T=2
WAIT SECONDS=2

'Paste the extracted Data on TAB_2:
'>
'May need to reload the Page to clear Content from previous Loop:
'URL GOTO=...?
'>
'Paste the Data:
'TAG POS=1 ........ CONTENT={{Extracted_Data}}
'...
'>
'Want to extract the Results...?:
SET !EXTRACT NULL
'TAG POS=1 ......... EXTRACT=TXT
'>
'Want to save the extracted Results...?:
'SAVEAS TYPE=EXTRACT ... etc ...


'Go back to TAB_1 to click on 'Next':
'*************************************
TAB T=1
WAIT SECONDS=2

'Deactivate '!ERRORIGNORE' for the Script to abort if there is no 'Next' Button/Link:
SET !ERRORIGNORE NO
TAG POS=1 TYPE=A ATTR=ID:PNNEXT

'<END OF SCRIPT>
(Written and tested on iMacros for FF v8.8.2, Pale Moon v26.3.3 (=FF47), Win10_x64.)

I think the Principle is fairly simple and I've put Comments (like always, ah-ah...!) in the Script itself.

Only for 'Loop=1', the Script loads the URL for Google (which is maybe not even needed if you start your Script from the Google Page already...), then does the Search, then does your "Stuff" on TAB_1, then goes to TAB_2 and does your Stuff there, then comes back to TAB_1 to (try to) click on the 'Next' Button/Link.

And for 'Loop>1', the 'URL GOTO' for the Search does nothing, same thing for the Search itself and the '!TIMEOUT_STEP' is even shortened for that part to "0" to avoid the Script to keep looking for a 'POS=0' Element which obviously does not exist...!

And you loop it from the 'Play (Loop)' Button on the iMacros Side Panel how many times you want, and if, say, at the end of Page_5 there is no 'Next' Page, the Script will automatically abort because '!ERRORIGNORE' just got deactivated before that 'TAG' Statement.

Oh...!, and the Script "assumes" that you already have a 2nd TAB opened on the Right of TAB_1 that can be used for the 'TAB_2' Stuff...! (A Conditional 'TAB OPEN' only for 'LOOP=1' is probably possible but a bit cumbersome...)

That's it..., enjoy...! 8)
- (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...
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Fri Oct 06, 2017 9:10 pm

Thank you for your reply;
that is great that you helped me with codes completely in Imacros;

it is working fine.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by chivracq » Fri Oct 06, 2017 10:12 pm

green.pine wrote:Thank you for your reply;
that is great that you helped me with codes completely in Imacros;

I have problem running it , i get this on the address bar :

Code: Select all

http://www.__undefined__.com/
Confused over

Code: Select all

SET URL_Search http://www.google.com/
isn't the URL defined here?

Code: Select all

SET URL_GOTO EVAL("var n='{{!LOOP}}', u='{{URL_Search}}'; var x,y,z; if(n==1){z=u;} else{z='';}; z;")
URL GOTO={{http://google.com}}
'>
'Do the Search: ('POS=0' won't do anything for 'Loop>1'...!)
TAG POS={{LOOP_Switch}} TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT={{Search_Content}}
TAG POS={{LOOP_Switch}} TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK
this variable (URL_Search) suppose to the fill the area with google.com, isn't it?
but it won't,

Thanks;
Hum..., works fine for me..., try looping this exact Script 3 times... (And read carefully the Content of the 'PROMPT', ah-a...!):
(I've added some Debug 'PROMPT', interesting Technique for you as well to "learn" how to debug your own Scripts...)

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
SET !ERRORIGNORE YES
SET !EXTRACT_TEST_POPUP NO
TAB T=1

'Easy Access for Vars:
'**********************
SET URL_Search http://www.google.com/
SET Search_Content "Dog grooming"

SET !LOOP 1


'Do the Search only for 'Loop=1':
'*********************************
'Recorded:
'**********
'URL GOTO=http://www.google.com/
'TAG POS=1 TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT=Dog<SP>grooming
'TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK
'TAG POS=1 TYPE=A ATTR=ID:hdtb-tls // (Click on 'Tools'.)
'TAG POS=1 TYPE=A ATTR=ID:PNNEXT

'=> Compute a 1/0 to use for '!TIMEOUT_STEP' and 'POS=n':
SET LOOP_Switch EVAL("var n='{{!LOOP}}'; var x,y,z; if(n==1){z=1;} else{z=0;}; z;")
SET !TIMEOUT_STEP {{LOOP_Switch}}
'>
'=> Define URL to use: (The empty String won't do anything for 'Loop>1'...!)
SET URL_GOTO EVAL("var n='{{!LOOP}}', u='{{URL_Search}}'; var x,y,z; if(n==1){z=u;} else{z='';}; z;")
URL GOTO={{URL_GOTO}}
'>
'Debug:
SET PROMPT_URL_Explanation "'URL_GOTO' should be '_http://www.google.com/_' for 'LOOP=1'...!"
ADD PROMPT_URL_Explanation "<BR>(or whatever is defined in the 'URL_Search' Var at the beginning of the Script...)"
ADD PROMPT_URL_Explanation "<BR>... and '__' for 'LOOP>1'...!"
'>
PROMPT LOOP:<SP>_{{!LOOP}}_<BR>URL_Search:<SP>_{{URL_Search}}_<BR>URL_GOTO:<SP>_{{URL_GOTO}}_<BR><BR>{{PROMPT_URL_Explanation}}
'>
PAUSE
'>
'Do the Search: ('POS=0' won't do anything for 'Loop>1'...!)
TAG POS={{LOOP_Switch}} TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT={{Search_Content}}
TAG POS={{LOOP_Switch}} TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK


'Now the part for all Loops:
'****************************
SET !TIMEOUT_STEP 1

'Part on TAB_1:
'***************
TAB T=1
WAIT SECONDS=2

'Click on 'Tools':
TAG POS=1 TYPE=A ATTR=ID:hdtb-tls

'Extract and copy the Content from TAB_1 that we want to keep:
SET !EXTRACT NULL
'TAG POS=1 ....... EXTRACT=TXT
SET Extracted_Data {{!EXTRACT}}
'...
'...


'Part on TAB_2:
'***************
TAB T=2
WAIT SECONDS=2

'Paste the extracted Data on TAB_2:
'>
'May need to reload the Page to clear Content from previous Loop:
'URL GOTO=...?
'>
'Paste the Data:
'TAG POS=1 ........ CONTENT={{Extracted_Data}}
'...
'>
'Want to extract the Results...?:
SET !EXTRACT NULL
'TAG POS=1 ......... EXTRACT=TXT
'>
'Want to save the extracted Results...?:
'SAVEAS TYPE=EXTRACT ... etc ...


'Go back to TAB_1 to click on 'Next':
'*************************************
TAB T=1
WAIT SECONDS=2

'Deactivate '!ERRORIGNORE' for the Script to abort if there is no 'Next' Button/Link:
SET !ERRORIGNORE NO
TAG POS=1 TYPE=A ATTR=ID:PNNEXT

'<END OF SCRIPT>
And "yours" should work as well if you didn't remove the 'URL_Search' Definition at the beginning of the Script...
Once you understand what you did "wrong", you can keep the 'Debug' part, you only need to comment out the 2 'PROMPT' and 'PAUSE' Statements...
- (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: How to use EVAL to Stop the Macro when there is no Next?

Post by chivracq » Fri Oct 06, 2017 10:29 pm

green.pine wrote:Thank you for your reply;
that is great that you helped me with codes completely in Imacros;

it is working fine.
Ah OK, I see that you've edited your previous Reply, ah-ah..., I was testing and replying in the meantime...

But hum, my previous Post/Script is still interesting for you, pay attention to the way I added some Debug 'PROMPT' and how I "built" it, that's a very useful Technique to debug yourself your own Scripts to follow your Vars, apply this Technique and you won't need anybody anymore to help you for your Scripts, ah-ah...!

But hum, some little further "Thinking"..., I hope you "realize" that the completely "ironic" Solution to your original Thread Qt/Title is simply "to do nothing" (by disabling '!ERRORIGNORE') as your Script will simply abort by itself if that 'Next' Button/Link is not found for a next Page at the end of your Script... I find it nearly funny, oops...! :twisted:

And yep, it's always possible to "improve" a bit the "Quality" of the 'Abort' by checking/extracting if the 'Next' Link does exist or not, and just like I used to "construct" the 'PROMPT_URL_Explanation' Content, to define some mini-Report to display in the Side-Panel instead of the "standard" RuntimeError that some HTML Element was not found Line_x, and to abort the Macro yourself with 'EVAL()' and 'MacroError()'... :idea:

:arrow: Post your complete final working Script and I'll add that "Functionality" for you, I use it in many of my own Macros so it's a quick mostly Copy&Paste Work for me... 8)

:arrow: Start from the "Template-Script" I posted in my last Reply (with the 'Debug/'PROMPT'' Functionality as well), you only need to fill in the parts of what you do on TAB_1 and TAB_2... I think I use a very "secure" and easy to understand and to re-adapt for other Sites/Needs way of writing "my" Scripts, then I can adapt and tune "yours" to make it a bit of a Showcase for yourself and other Users... :idea:
- (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...
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Sat Oct 07, 2017 6:41 am

Thanks again,
yes indeed the next button solution and bypass method of some lines are very smart way,
Thanks to you to come up with such a simple yet powerful solution.
Yes, I did edited my post, I did copy and past the code, and it did not work in the first place, I missed some of the codes while adding needed part, after some time, I did copy paste the code again, and replaced those places with correct info, then it worked, the only thing I added was a OPEN TAB for tab2,
so I did have a post asking to help me to make it work, but when I realized it was my mistake, then it was not needed to have that post, so removed it.

every loop it opens a new tab ( TAB 2) and do the task, but when it opens a new tab, the work will be done in the first TAB2, which is good, no need to reload the page again, but after say 8 time of looping there are 8 empty tabs on the right side of TAB2,
anyhow this is not a problem, and it works nicely.
thanks
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by chivracq » Sat Oct 07, 2017 8:43 am

green.pine wrote:Thanks again,
yes indeed the next button solution and bypass method of some lines are very smart way,
Thanks to you to come up with such a simple yet powerful solution.
Yes, I did edited my post, I did copy and past the code, and it did not work in the first place, I missed some of the codes while adding needed part, after some time, I did copy paste the code again, and replaced those places with correct info, then it worked, the only thing I added was a OPEN TAB for tab2,
so I did have a post asking to help me to make it work, but when I realized it was my mistake, then it was not needed to have that post, so removed it.

every loop it opens a new tab ( TAB 2) and do the task, but when it opens a new tab, the work will be done in the first TAB2, which is good, no need to reload the page again, but after say 8 time of looping there are 8 empty tabs on the right side of TAB2,
anyhow this is not a problem, and it works nicely.
thanks
OK, thanks for the Feedback and the "Rewarding", ah-ah...! But yep, (fairly) "simple" Solutions usually work the best with pure '.iim', no need for "cumbersome" if-else-do-for-while-until..., ah-ah...!

But hum, about the 'TAB OPEN' (hum and not "OPEN TAB"), yep, that's why I mentioned a "Conditional OPEN" which can be done I reckon, as I once did some Testing about a "Conditional CLOSE" for which I found a nice Solution that can very probably applied to 'OPEN' in a similar way, but hum, still a bit cumbersome in my Opinion I said, that's why I said that the easiest Solution was to make sure to start the Macro with a 2nd Tab open next... Or you could fire a 'TAB CLOSE' at the end of the TAB_2 Actions..., but it slows down the "general" Execution, and getting constantly Tabs being opened and closed is a bit confusing for "Humans" as you quickly lose the "Feeling" where the Script is in its Workflow... :idea:

>>>

Hum, funny..., a bit curious about your previous Threads as I was a bit "surprised" by your "Level" as you must have been using iMacros since at least 2010 from your Registration Date on the Forum, and using '.js' Scripts since at least since 2011 from this Thread..., funny to see that then, 3 Pages of Posts were needed to come to a Solution..., I would solve it in 1 or 2 Post(s) with a Working Script in pure '.iim'..., but hum, not sure if 'EVAL()' had already been implemented at that time, ah-ah...!
Hum, nope indeed, 'EVAL()' is from around 2011-09-15 from the Creation Date for its dedicated Page in the Wiki, while that Thread was from 6 months earlier... OK then, fair enough... Nearly a pity that nobody bumped it since, that's an interesting Case as well, ah-ah....

Hum, I thought I had a Solution/Workaround without 'EVAL()', but oops, not good either as it would require the 'EVENT' Mode which was even later implemented... Hum, difficult to do Conditional Behaviour in pure '.iim' then, ah-ah...! I started for myself only after 'EVAL()' got implemented, I think..., and to avoid '.js' Scripts, ah-ah...! :oops:
- (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...
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Sat Oct 07, 2017 9:04 pm

thanks for more info,
about working on Imacros since 2010, but I have not been really active on imacros, I do have another job that is not related to web automation, when I get a chance I try to do something,
I read and learn, since it takes time to come back again to coding I forget most of it, that is how it has been, I am not young anymore.
about Open tab, yes, I did read your comment on the code line, but I could not figure it out then,
after running it with Open new tab, came back 15 min later, FF really slow, Ha ha, so I took it off,

anyhow, I want to do another loop on this macro,
since imacros can not do nested loop, I converted it into js,
and on the js file I have set another loop, there is no error but it is locked and won't play,

the way I wanted to do is:
to get the keywords for searching from csv file, do the search, go to the last NEXT button, when done, start the macro from beginning and pull second keyword from csv file, do this until the last keyword on the csv file has been used.

I wanted to use the error message as one of the conditions well, I have put these lines of code at the end of the macro,
but the macro won't run for the first time,
var ErrMsg = iimGetLastError();
var MacroRun;
MacroRun = iimPlay("macro")
iimDisplay (MacroRun);
if ((MacroRun => 1 ) && (ErrMsg == '#EANF#'))
{
iimPlay(macro)
}
I have put the condition, when macro reaches at the end, if the marco runs and there is an error message then run the macro again, but since for the first time, there is no error and macro has not been played yet, ( both condition are zero) so it does not met the requirement to play, that is suppose to be that way, isn't it?
but this condition will not let the macro run at all from outside ( imacro play button).

do you have any solution for this?
thanks
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by chivracq » Sat Oct 07, 2017 9:58 pm

green.pine wrote:thanks for more info,
about working on Imacros since 2010, but I have not been really active on imacros, I do have another job that is not related to web automation, when I get a chance I try to do something,
I read and learn, since it takes time to come back again to coding I forget most of it, that is how it has been, I am not young anymore.
about Open tab, yes, I did read your comment on the code line, but I could not figure it out then,
after running it with Open new tab, came back 15 min later, FF really slow, Ha ha, so I took it off,

anyhow, I want to do another loop on this macro,
since imacros can not do nested loop, I converted it into js,
and on the js file I have set another loop, there is no error but it is locked and won't play,

the way I wanted to do is:
to get the keywords for searching from csv file, do the search, go to the last NEXT button, when done, start the macro from beginning and pull second keyword from csv file, do this until the last keyword on the csv file has been used.

I wanted to use the error message as one of the conditions well, I have put these lines of code at the end of the macro,
but the macro won't run for the first time,

Code: Select all

var ErrMsg = iimGetLastError();
var MacroRun;
MacroRun = iimPlay("macro")
iimDisplay (MacroRun);
if ((MacroRun => 1 ) && (ErrMsg == '#EANF#'))
	{
		iimPlay(macro)
	}
I have put the condition, when macro reaches at the end, if the marco runs and there is an error message then run the macro again, but since for the first time, there is no error and macro has not been played yet, ( both condition are zero) so it does not met the requirement to play, that is suppose to be that way, isn't it?
but this condition will not let the macro run at all from outside ( imacro play button).

do you have any solution for this?
thanks
"do you have any solution for this?"
=> Boah...!, Yep of course...!, but in pure '.iim' re-off course, ah-ah...!
=> Where did you get "since imacros can not do nested loop,..."...!? :lol:

=> Search my Threads on "Nested Loop(s)" + "Modulo" and you'll find a few Threads with Nested Loops implemented in pure '.iim'... :idea:
+ Maybe not only my Posts but other Advanced User @iimfun's as well who posted once I think a maybe simpler Solution/Syntax than the one I was using... (But my Scripts/Syntax are/is usually easier to understand and adapt/reuse than @iimfun's I would think...)

To increment your '!DATASOURCE_LINE' to fetch the next Keyword(s), you'll have to base it indeed then on if the 'Next' Button/Link was present or not (with 'EXTRACT') at the end of the previous Search+Extraction+Handling-on-TAB_2+Back-to-TAB_1.
- (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...
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Sun Oct 08, 2017 10:58 pm

Thanks for reply;
Eval is not an easy task for me, I did go through all the Eval example, but could not understand how to make it start my macro to start over, except Loop, and I don't understand the usage of Modulo,
I manage to set new number (1) to the loop, but I guess I am wrong, because it does nothing.
anyhow this is what i could do, it runs as before, it seems this code does nothing to the whole thing.

TAG POS=1 TYPE=A ATTR=ID:PNNEXT EXTRACT=TXT
TAG POS=1 TYPE=A ATTR=ID:PNNEXT
SET Start_Over EVAL("var s='{{!EXTRACT}}'; var x,y,z; if(s!=='next'){z=1;} else{z=0;}; z;")
SET !TIMEOUT_STEP {{Start_Over}}
SET !LOOP {{Start_Over}}
do you help me with this to make it work,
thanks
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by chivracq » Mon Oct 09, 2017 5:57 am

green.pine wrote:Thanks for reply;
Eval is not an easy task for me, I did go through all the Eval example, but could not understand how to make it start my macro to start over, except Loop, and I don't understand the usage of Modulo,
I manage to set new number (1) to the loop, but I guess I am wrong, because it does nothing.
anyhow this is what i could do, it runs as before, it seems this code does nothing to the whole thing.

Code: Select all

TAG POS=1 TYPE=A ATTR=ID:PNNEXT EXTRACT=TXT
TAG POS=1 TYPE=A ATTR=ID:PNNEXT
SET Start_Over EVAL("var s='{{!EXTRACT}}'; var x,y,z; if(s!=='next'){z=1;} else{z=0;}; z;")
SET !TIMEOUT_STEP {{Start_Over}}
SET !LOOP {{Start_Over}}
do you help me with this to make it work,
thanks
"Eval is not an easy task for me, I did go through all the Eval example[s]...", tja 1 'EVAL()' Statement = 1 (compact) JavaScript Statement and you each time want to use '.js' Scripts, ah-ah...!
Euh, which "all the Eval example[s]"...?, my previous Reply was about "Nested Loops" (and 'Modulo'), but the 'EVAL)' Statement you posted in your Script and tried to modify has nothing really related to "Nested Loops"... The Maths Mechanism for Nested Loops is a bit different than the Base_2 Model I used for some previous Implementation in your Thread for the 'POS=n'...
Post a Link or quote which Thread(s) you use as Ref for Nested Loops.., but hum..., it's not complicated at all, some simple Maths that all Kids learn when they are 10-12-14 years old at school... :wink:

But hum, yep about your Try..., hum-hum..., yep-yep..., you are a little-little bit in the good Direction, even if I'm not sure what you expect from 'LOOP=0'...!?, oops...!, but more important to know is that you can only use 'SET 'LOOP' once and it gets read by iMacros on the first Loop ONLY, and it is ignored on the Loops after... => You cannot use Raw '!LOOP' directly, but you'll need to compute your own 'MyLoop', based on '!LOOP' indeed.
it took me a mini-while to understand the Mechanism, but it's cleverly logical actually... Easy Explanation is that 'SET !LOOP 2' (instead of '...=1' which is the Default) is meant to tell your Macro to start looping through your DataSource from Row_2, to avoid the Table Header. If iMacros was still reading the 'SET !LOOP 2' Statement on each Loop, it would start over again and again from Row_2 and would never check the next Rows, ah-ah, oops...! => The clever Solution by Design is to read the '!SET LOOP' Statement only on the first Run of the Macro...! 8)

And hum, you'll need to think about how you want to handle the inner Loop for the say 6 Search Pages for each Keyword..., 3 Cases...:
- Either all your Searches will always yield at least 6 Pages of Results... => Inner_Loop=6. (Fixed, "Standard" Nested Loop...)
- Or some Searches might yield less than 6 Pages, but you don't mind "losing" 1-2 min for some "fake" Extract and Check on TAB_2 for no Data... => Inner_Loop=6 (Fixed forced...!) But hum, the "1-2 min" can be drastically reduced to maybe 1-2 sec, using the same Mechanism I used for your '!TIMEOUT_STEP'...
- Or for the "shorter" Searches that might yield less than 6 Pages, if you don't want to "wait" for the sometimes 2 "fake" Pages, (Inner Loop not fixed), you'll need a Mechanism for your Script to know to then jump at that moment from say '!DATASOURCE_LINE=6' to '...=7'..., using a Temp mini-'SAVEAS' that will also be a Secondary DataSource for the same Script (hum..., I wouldn't be surprised if I had lost you here, ah-ah...!!), or the Input Field on your TAB_2 for example, but that would imply an extra Check on TAB_2 at the beginning of each Loop, or the Google Search Input Field on TAB_1 could also be "misused" for the same Purpose, ah-ah..., is also a Possibility... (And there are probably others, as I already mentioned in some previous Thread, your Creativity is "the Limit", but don't worry, good News is that everything is always possible... :D )
- (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...
green.pine
Posts: 62
Joined: Thu Nov 04, 2010 5:21 pm

Re: How to use EVAL to Stop the Macro when there is no Next?

Post by green.pine » Mon Oct 09, 2017 1:15 pm

thank you for your reply, and explanation;
I did a new try, but still can not do the anther loop, it seems that I have to change the whole coding to do that, I get confused, anyhow

this time I use it another way, which does not work properly;

Code: Select all

TAG POS=1 TYPE=A ATTR=ID:PNNEXT EXTRACT=TXT
TAG POS=1 TYPE=A ATTR=ID:PNNEXT
SET New_KW_Start EVAL("var s='{{!EXTRACT}}'; var x,y,z; if(s!=='next'){z=1;} else{z=0;}; z;")

TAG POS={{New_KW_Start}} TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT={{!COL1}} 
TAG POS={{New_KW_Start}} TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK
SET !TIMEOUT_STEP 1
this time it gets keyword from csv file, do the search and grab the text, and go to TAB2, do the task there,come back to TAB1, click the Next, but get new keyword from CSV file and do a search, it suppose to do the highlighting and copy the text.

It does not even go through all keywords in the csv file, only 5 keywords and stops, no errors,

somehow I have to change the value of Loop_Switch again, or redo the whole coding, which is too complicated for me.

Code: Select all

SET URL_GOTO EVAL("var n='{{!LOOP}}', u='{{URL_Search}}'; var x,y,z; if(n==1){z=u;} else{z='';}; z;")
URL GOTO={{URL_GOTO}}
TAG POS={{LOOP_Switch}} TYPE=INPUT:TEXT FORM=ID:tsf ATTR=ID:lst-ib CONTENT={{!COL1}} 
TAG POS={{LOOP_Switch}} TYPE=INPUT:SUBMIT FORM=ID:tsf ATTR=NAME:btnK
SET !TIMEOUT_STEP 1
appreciate your help again.
thanks
Post Reply