Jump to content

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 post
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 post
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 post
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 post
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 post
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 post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Jahar
      Hi,
      I am new to Autoit, Kindly guide me. I wanted to send a message with HTTP/GET to the URL https://ghsff.it/. How to do so?
    • By Colduction
      Hi dear friends!, i'm sorry for creating a new thread (a new problem), i have over than 9 lists that i want to combine them to be this (in this example, there are 3 test files):


      I've written a little code for splitting main information, but i really confused how to make results as "Output.txt", here is that code:
       
      $sRegex_1 = StringRegExp(FileRead("1.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) $sRegex_2 = StringRegExp(FileRead("2.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) $sRegex_3 = StringRegExp(FileRead("3.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) For $i = 0 To UBound($sRegex_1) - 1 ConsoleWrite($sRegex_1[$i] & @CRLF) For $j = 0 To UBound($sRegex_2) - 1 ConsoleWrite($sRegex_2[$j] & @CRLF) For $k = 0 To UBound($sRegex_3) - 1 ConsoleWrite($sRegex_3[$k] & @CRLF) Next Next Next  
    • By WoodGrain
      Hi All,
      Trying to open windows explorer to a WebDav location and it's not working quite how I want, on the computers it is setup as a "network location" (as opposed to a "mapped drive", and this unfortunately can't be changed), the "Data" WebDav folder sits directly under "This PC" if that's an easier way to get to it. any suggestions as to what I can correct to get the 2nd example to work?
      ; This works, but I'm trying to avoid this as users normally see the URL style in the 2nd example below $folderToOpen = "\\mycompany.sharepoint.com@SSL\DavWWWRoot\Data" Run("Explorer.exe " & $folderToOpen) ; This does not work, it tries to open the WebDav url in the default web browser $folderToOpen = "https://mycompany.sharepoint.com/Data" Run("Explorer.exe " & $folderToOpen) ShellExecute also opens it in the default browser.
      Saw _WinAPI_ShellOpenFolderAndSelectItems but couldn't get the 2nd example to work.
      If I manually open Windows Explorer and paste in https://mycompany.sharepoint.com/Data it loads the WebDav directory without issue.
      If I have to use the pathing from the first example it is fine, just trying to give users a familiar experience.
      Thanks!
    • By Colduction
      Hi guys!, i have a problem to convert Python code to AutoIt code, in fact i had not coded with Python yet!, this code is about permutation a string's case, i will be happy with your comments :)❤;

      Python code:
       
      # Python code to print all permutations # with respect to cases # Function to generate permutations def permute(inp): n = len(inp) # Number of permutations is 2^n mx = 1 << n # Converting string to lower case inp = inp.lower() # Using all subsequences and permuting them for i in range(mx): # If j-th bit is set, we convert it to upper case combination = [k for k in inp] for j in range(n): if (((i >> j) & 1) == 1): combination[j] = inp[j].upper() temp = "" # Printing current combination for i in combination: temp += i print(temp), # Driver code permute("Hello") # This code is contributed by Sachin Bisht
      My code in AutoIt:
      ; https://www.geeksforgeeks.org/permute-string-changing-case/ _PermuteCase("ABC") Func _PermuteCase($sText) If StringRegExp($sText, "^[A-Za-z]{1,}$") Then Local $iLength = StringLen($sText) ; Get length of the text. Local $iMaxPerm = 2 ^ $iLength ; Number of permutations is 2^n Local $sLow_Text = StringLower($sText) ; Converting string to lower case Local $asChrs = StringToASCIIArray($sLow_Text) ; Split the text into array of chars. For $i = 1 To $iMaxPerm Step 1 For $j = 0 To $asChrs[0] ;................................................... Next Next Else Return SetError(-1, 0, "Error: Input is incorrect!") EndIf EndFunc ;==>_PermuteCase  
       
       
       
       
       
      ====================== SOLUTION by @TheXman ======================
       
×
×
  • Create New...