Jump to content

Open Random URL (from list)


Recommended Posts

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

Link to comment
Share on other sites

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 by rogue5099
Link to comment
Share on other sites

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... :oops:

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 by MrYonG
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

; 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 by rogue5099
Link to comment
Share on other sites

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 :oops:

Link to comment
Share on other sites

  • Developers

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.
  :)

Link to comment
Share on other sites

  • 1 year later...
  • 2 years later...

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 by pecsenye
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...