MrYonG Posted March 17, 2012 Posted March 17, 2012 Hello, i was wondering how i would go about opening a random URL from a predefined list? i am trying to create a small game for the kids, where they have a question and they have to find out the answer (finish the story) from the pages that open...but i need all the pages to be at random, so they need to work out which part comes first, second, third...etc. the code i use to open the URL is and to close the browser: shellexecute("THE URL") If ProcessExists("firefox.exe") Then ProcessClose("firefox.exe") endif is it possible to creaet like a shellexecute("$random") and then add my url's as a ranodm variables? or would it be better to add each script with URL / porcessclose and randomly pick which "url" to open? all help much appreciated MrYonG
Rogue5099 Posted March 17, 2012 Posted March 17, 2012 (edited) You can put the list into an Array: Sites.txt: http://www.yahoo.com http://www.bing.com http://www.google.com #include <file.au3> Dim $aSite If Not _FileReadToArray("Sites.txt",$aSite) Then MsgBox(4096,"Error", " Error reading file to Array error:" & @error) Exit EndIf $x = Random(1, $aSite[0], 1) ShellExecute($aSite[$x]) ; Msgbox(0,'Random Site', $aSite[$x]) Edited March 17, 2012 by rogue5099 MrYonG 1 My projects: Inventory / Mp3 Inventory, Computer Stats
MrYonG Posted March 17, 2012 Author Posted March 17, 2012 (edited) THANX ALOT, Spammer!That did the trick ! Edited March 17, 2012 by MrYonG
MrYonG Posted March 18, 2012 Author Posted March 18, 2012 (edited) hey hey, i had a problem with it always giving me, the error message: error reading file to array...i have now sorted that, but it just opens up the location of the .txt file...it does not open the url's from within... any sugestions? ; Save Links to Text file FileWrite("url.txt", $var1 & @CRLF) FileWrite("url.txt", $var2 & @CRLF) FileWrite("url.txt", $var3 & @CRLF) FileWrite("url.txt", $var4 & @CRLF) FileWrite("url.txt", $var5 & @CRLF) #include <file.au3> Dim $aSite If Not _FileReadToArray("url.txt",$aSite) Then MsgBox(4096,"Error", " Error reading file to Array error:" & @error) Exit EndIf $x = Random(1, $aSite[0], 1) ShellExecute( $aSite ) EDIT*** i just looked over it again and i realised i am missing the function for _FileReadToArray ... any help for that would be nice im kinda new to autoit and dont know what to do? Edited March 18, 2012 by MrYonG
JohnOne Posted March 18, 2012 Posted March 18, 2012 The function _FileReadToArray resides in the standard UDF <File.au3>url.txt should be in the same folder as your script.It is usual to put the include at the very top of your script. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Rogue5099 Posted March 18, 2012 Posted March 18, 2012 (edited) ; Save Links to Text file FileWrite("url.txt", $var1 & @CRLF) FileWrite("url.txt", $var2 & @CRLF) FileWrite("url.txt", $var3 & @CRLF) FileWrite("url.txt", $var4 & @CRLF) FileWrite("url.txt", $var5 & @CRLF) #include <file.au3> Dim $aSite If Not _FileReadToArray("url.txt",$aSite) Then MsgBox(4096,"Error", " Error reading file to Array error:" & @error) Exit EndIf $x = Random(1, $aSite[0], 1) ShellExecute( $aSite ) If you already have the URL's assigned a variable why do you need to write them and then open them from a text file? You can just call the variable. Global $var[6] $var[1] = "www.google.com" $var[2] = "www.yahoo.com" $var[3] = "www.bing.com" $var[4] = "www.autoitscript.com" $var[5] = "www.wikipedia.org" $x = Random(1, 5, 1) ShellExecute($var[$x]) OR: $var1 = "www.google.com" $var2 = "www.yahoo.com" $var3 = "www.bing.com" $var4 = "www.autoitscript.com" $var5 = "www.wikipedia.org" $x = Random(1, 5, 1) ShellExecute(Eval("var" & $x)) Edited March 18, 2012 by rogue5099 My projects: Inventory / Mp3 Inventory, Computer Stats
MrYonG Posted March 18, 2012 Author Posted March 18, 2012 because i didn't post the hole script here...only the part that was giving me problems. so i am even more confused now... this part of the script works (the url's get writen to a txt file) all i want to do is read the .txt file and chose a url at random to be opened. this looks nice !! and thank you for this: #include <file.au3> Dim $aSite If Not _FileReadToArray("url.txt",$aSite) Then MsgBox(4096,"Error", " Error reading file to Array error:" & @error) Exit EndIf $x = Random(1, $aSite[0], 1) ShellExecute( $aSite ) But it somehow does not work ... at first i got the "Error reading file to Array" and then the shellexecute didnt open the url's, basically it just took me to the fodler that contained the .txt file (same folder as the script) ... if their is another way of doing this please let me know...i am new to scripting
Rogue5099 Posted March 18, 2012 Posted March 18, 2012 (edited) Change ShellExecute($aSite) to ShellExecute($aSite[$x]) to open url. Edited March 18, 2012 by rogue5099 My projects: Inventory / Mp3 Inventory, Computer Stats
Developers Jos Posted March 18, 2012 Developers Posted March 18, 2012 shouldn't that be?: #include <file.au3> Dim $aSite If Not _FileReadToArray("url.txt",$aSite) Then MsgBox(4096,"Error", " Error reading file to Array error:" & @error) Exit EndIf $x = Random(1, $aSite[0], 1) ShellExecute( $aSite[$x] ) SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
pecsenye Posted June 29, 2015 Posted June 29, 2015 (edited) Thank you, it's working:Global $var[6]$var[1] = "www.google.com"$var[2] = "www.yahoo.com"$var[3] = "www.bing.com"$var[4] = "www.autoitscript.com"$var[5] = "www.wikipedia.org"$x = Random(1, 5, 1)ShellExecute($var[$x])But how can I open more than 1 random url (For example: 20) in the same window?For example: open 1. url, wait X seconds, open 2. url, wait X seconds, open 3. url, wait X second (same window) Edited June 29, 2015 by pecsenye
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now