random number help

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
lime
Posts: 3
Joined: Sun May 10, 2009 11:02 pm

random number help

Post by lime » Sun May 10, 2009 11:07 pm

i am trying to get imacros to generate me some random numbers and put them in a text box.

it looks like this:

VERSION BUILD=6210326 RECORDER=FX
TAB T=1
URL GOTO=http://www.google.com/
//imacros-js:showsteps yes
var randomnumber=Math.floor(Math.random()*11)
iimSet("rnumber", randomnumber);
iimPlay("yourmacro.iim")
TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:f ATTR=NAME:q CONTENT=rnumber


i looked through some old questions about the same thing, but they didn't help.
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: random number help

Post by josephconlin » Mon May 11, 2009 6:09 pm

lime wrote:i am trying to get imacros to generate me some random numbers and put them in a text box.

it looks like this:

VERSION BUILD=6210326 RECORDER=FX
TAB T=1
URL GOTO=http://www.google.com/
//imacros-js:showsteps yes
var randomnumber=Math.floor(Math.random()*11)
iimSet("rnumber", randomnumber);
iimPlay("yourmacro.iim")
TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:f ATTR=NAME:q CONTENT=rnumber


i looked through some old questions about the same thing, but they didn't help.
You are trying to mix .iim and .js code in the same file (probably .iim file from what it looks like). You can't do that. You can have a .js file that calls an existing .iim file, or you can embed the iim commands in a .js file. Here are examples of both using your macro above.

CallExistingMacro.js (this and ExistingMacro.iim need to be in the Macros directory defined in your settings).

Code: Select all

//imacros-js:showsteps yes
var randomnumber=Math.floor(Math.random()*11)

var retcode=0;

iimSet("rnumber", randomnumber);
retcode = iimPlay("ExistingMacro.iim");

if (retcode < 0)               // an error has occured
{
	errtext = iimGetLastError();
	alert("Error "+retcode+": "+errtext);
}
ExistingMacro.iim

Code: Select all

VERSION BUILD=6210326 RECORDER=FX
TAB T=1
URL GOTO=http://www.google.com/
TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:f ATTR=NAME:q CONTENT={{rnumber}}
Notice that your .iim file needs to refer to the value received via iimSet with double curly braces around the variable name, ie {{rnumber}}.

Embedded.js

Code: Select all

//imacros-js:showsteps yes
var randomnumber=Math.floor(Math.random()*11)
var macro;
var retcode=0;

macro="CODE: ";
macro+="TAB T=1\n";
macro+="URL GOTO=http://www.google.com/\n";
macro+="TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:f ATTR=NAME:q CONTENT={{rnumber}}\n";

iimSet("rnumber", randomnumber);
retcode = iimPlay(macro);

if (retcode < 0)               // an error has occured
{
	errtext = iimGetLastError();
	alert("Error "+retcode+": "+errtext);
}
I haven't tested this, but this shows how to handle mixed Javascript and iMacros files and passing variables to the iMacros scripts. To pass data back, store it in !EXTRACT and use iimGetLastExtract(), http://wiki.imacros.net/iimGetLastExtract(), in the js file.

Hope this helps.
lime
Posts: 3
Joined: Sun May 10, 2009 11:02 pm

Re: random number help

Post by lime » Mon May 11, 2009 11:31 pm

thanks! that helped a bunch!!

edit: how can you make the contenent something, and then add a random number to it,
i tried: CONTENT= some content here {{rnumber}}
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: random number help

Post by josephconlin » Tue May 12, 2009 4:16 pm

lime wrote:thanks! that helped a bunch!!

edit: how can you make the contenent something, and then add a random number to it,
i tried: CONTENT= some content here {{rnumber}}
By your example, I assume by "add a random number to it" you mean concatenate the random number to the end of a string and not a mathematical add of two numbers.

What you attempted looks right to me, and is used with !VAR1, etc in Demo-ExtractAndFill.iim (with the exception that, in a .iim file, you need to represent spaces with <SP>, ie: CONTENT=some<SP>content<SP>here<SP>{{rnumber}}). What happens when you try that? Do you just get your content without the number showing up at the end?

Something to try, just in case, would be converting your random number from a Number to a String in javascript before you pass it to the macro, just to make sure that you are concatenating two string types. Something like

Code: Select all

iimSet("rnumber", randomnumber.toString());
Hope this helps.
Hello 71
Posts: 96
Joined: Mon Feb 18, 2008 12:06 am
Location: Toronto, ON, Canada

Re: random number help

Post by Hello 71 » Tue May 12, 2009 7:59 pm

josephconlin, do you code ECMAscript? Sounds like you don't. Anyways, ECMAscript (what JavaScript was called before M$ called it JScript and Netscape called it Javascript) is weakly typed, i.e. there are no strictly declared types for variables, so there is no need for a "tostring" function.
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: random number help

Post by josephconlin » Tue May 12, 2009 11:53 pm

Hello 71 wrote:josephconlin, do you code ECMAscript? Sounds like you don't. Anyways, ECMAscript (what JavaScript was called before M$ called it JScript and Netscape called it Javascript) is weakly typed, i.e. there are no strictly declared types for variables, so there is no need for a "tostring" function.
I admit to being more familiar with programming concepts and less familiar with JavaScript. I don't recall ever hearing about ECMAscript.

However, Java is a strongly typed language, and that is the language that is being used to generate the random number. I have seen enough gotchas in my Javascript and iMacros experience to see that sometimes a variable that I think is being interpreted as a Number (or a String) is in fact being interpreted as the other. Number and String functions both exist in JavaScript, including the toString() function, and when I'm in doubt, I use them to make sure I know what type of data I'm working with. Just my preference.
Post Reply