The renewal maintenance has officially ended for Progress iMacros effective November 30, 2023.
This Wiki site will also no longer be moderated from the Progress side.
Thank you again for your business and support.
Sincerely, The Progress Team
EVAL
This command allows you to evaluate values, and trigger macro errors if certain conditions are met. It can can also be used to convert and modify extracted content. Inside EVAL, you have the full power of Javascript available!
Example uses:
- Detect if e. g. date is "older than 40 days"
- Detect if a number, e. g. exchange rate is above/below a certain value
- Make small clean-up operations. If the extracted text is "US$33.33" you might remove the "US$" part before saving it via SAVE TYPE=EXTRACT
Syntax
SET !VAR1 EVAL("[javascript statements]")
Parameters
None
Examples
Wait for a random time (1 to 10 seconds) after a page is loaded:
SET !VAR1 EVAL("var randomNumber=Math.floor(Math.random()*10 + 1); randomNumber;") URL GOTO=http://www.iopus.com ' waits 1 to 10 seconds WAIT SECONDS={{!VAR1}}
Save EUR rates in USD only if it is above a certain threshold (1.35 USD). If it is below, stop the macro and give an error:
URL GOTO=http://www.xe.com/ucc/convert/?Amount=1&From=EUR&To=USD SET !EXTRACT_TEST_POPUP NO TAG POS=1 TYPE=TD ATTR=TXT:*<SP>USD&&WIDTH:46%&&ALIGN:left EXTRACT=TXT SET !VAR1 EVAL("var s=\"{{!EXTRACT}}\"; s.replace(\"USD\",\"\"); var d=parseFloat(s); if(d < 1.3){MacroError(\"Euro is below 1.3 USD.\");} else d;") SET !EXTRACT NULL SET !EXTRACT {{!VAR1}} SAVEAS TYPE=EXTRACT FOLDER=* FILE=EuroRate.csv
Check if the most recent post is older then some hours (12 hours):
CLEAR URL GOTO=http://www.readwriteweb.com/ SET !EXTRACT_TEST_POPUP NO TAG POS=1 TYPE=DIV ATTR=CLASS:submeta EXTRACT=TXT SET !VAR1 EVAL("var h=12; var s=\"{{!EXTRACT}}\"; var re=/\\w+\\s\\d{1,2},\\s\\d{4}\\s+\\d{1,2}\\:\\d{2}\\s[A|P]M/; var postDateStr=re.exec(s); var today=new Date(); var postAge=today.getTime()-Date.parse(postDateStr); var hInMilli=h*60*60*1000; if(postAge>hInMilli){MacroError(\"Post is older than 12 hours\");}")
Demo Eval.iim:
URL GOTO=http://www.iopus.com/imacros/demo/v7/eval.htm SET !VAR1 EVAL("Math.floor(Math.random()*5 + 1);") WAIT SECONDS={{!VAR1}} SET !EXTRACT_TEST_POPUP NO TAG POS=1 TYPE=B ATTR=TXT:* EXTRACT=TXT SET !VAR2 EVAL("var h=20; var s=\"{{!EXTRACT}}\"; var today=new Date(); var postAge=today.getTime()-Date.parse(s); var hInMilli=h*60*60*1000; if(postAge>hInMilli){MacroError(\"Time deviates more than 20 hours\");}else null") TAG POS=1 TYPE=INPUT:TEXT FORM=ID:form1 ATTR=NAME:textfield EXTRACT=TXT SET !VAR2 EVAL("var s=\"{{!EXTRACT}}\"; var d = parseFloat(s); if((d>99)&&(d<101))d; else MacroError(\"Value is not in the set range\");")