Jump to content
Sign in to follow this  
MrYonG

Open Random URL (from list)

Recommended Posts

MrYonG

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

Share this post


Link to post
Share on other sites
Rogue5099

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
  • Like 1

Share this post


Link to post
Share on other sites
MrYonG

THANX ALOT,

Spammer!

That did the trick ! :oops:

Edited by MrYonG

Share this post


Link to post
Share on other sites
MrYonG

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

Share this post


Link to post
Share on other sites
Rogue5099

; 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

Share this post


Link to post
Share on other sites
MrYonG

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:

Share this post


Link to post
Share on other sites
Jos

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] )

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
VIP

Thanks! :)


Regards,
 

Share this post


Link to post
Share on other sites
pecsenye

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

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • Rocker
      By Rocker
      I apologize for my poor coding skills, but only on occasion do I have a need to automate a repetitious task to save time.  In a nutshell the following code is supposed to go to a website, save the site using Scrapbook (a plugin used by an older version of Firefox), rename the folder in Windows Explorer that Scrapbook assigns a random name for and then moves on to the next website, of which there are hundreds.  Everything works, except after the address bar gets populated with the URL, it's like the ENTER key never gets pushed.  The weird thing is, this worked yesterday before I left to go home and when I came in this morning it didn't work.  The next thing I'll be trying to learn is to loop this with a the list of the websites.  Thank you for your help!
      WinActivate( "[CLASS:MozillaWindowClass]" ) ;Apply focus to the open Mozilla firefox window Sleep(500) Send("^k") ;Apply focus to the address bar Sleep(500) Send("www.WEBSITE.com {ENTER}") ;Send the website to the address bar Sleep(10000) ;Wait for the website to fully open Send("!c") ;Opens the drop down menu for Scrapbook Sleep(2000) Send("{ENTER}") ;Selects the default drop down selection of save Sleep(20000) WinActivate( "[CLASS:CabinetWClass]" ) ;Applies focus to the open Windows Explorer window where the websites are saved Sleep(1000) Send("{SPACE}") ; Selects a folder in the folders list Sleep(200) Send("{HOME}") ;Moves to the top folder Sleep(200) Send("{F2}") ;Keyboard shortcut to rename the folder Sleep(200) Send("www.WEBSITE.com {ENTER}") ;Applies the new name to the folder  
    • nacerbaaziz
      By nacerbaaziz
      Hello all
      I have a question please
      I have a link to an .exe file
      for example this link
      https://www.nvaccess.org/files/nvda/releases/2017.1/NVDA_2017.1.exe
      I want to check if the link is exists orno
      surely  Without downloading the file
      is there any  function or a way to  do this?
      Thanks in advance
    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1 - 4.5 (on this version Microsoft destroy old rules) System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • nacerbaaziz
      By nacerbaaziz
      can we  create a list box with  Columns?
      welcome everybody
      Dears I have a question if you let me
      can we  create a list box with  Columns?
      i know we can create a list view with Columns
      but my question is about the list box
      I'm waiting your responses
      Thank you in advance
    • JustinZandee
      By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?
×