Jump to content

get URL from browser's address bar and store it in a text file


AutoSam
 Share

Recommended Posts

I am making some slow process with the AutoIt tutorials and examples. Looking through the online documentation I was wondering if there is a function or something else that can read the current URL from the current open browser window and store that string as data to work with as a variable or simply store it in a text file.

If it cannot be done with the current open browser window of any browser then I would just like to limit this function to Firefox of course.

Basically I need to know how to get URLs from the browser and later on I will be needing to know how to get and store data from the corresponding websites in a table or text file or in a temp file or temp location (ram? cache? where is such data actually shortly stored in AutoIt or on the computer??)

Link to comment
Share on other sites

I know there has to be a better way, but this can do it. It checks for Firefox first and if it doesn't exist, then checks for IE.

If WinExists("[CLASS:MozillaWindowClass]") Then
    WinActivate("[CLASS:MozillaWindowClass]")
ElseIf WinExists("[CLASS:IEFrame]") Then
    WinActivate("[CLASS:IEFrame]")
EndIf
Sleep(100)
send ("!d")
send ("^c")
$link = ClipGet()

MsgBox(0, "", $link)

#include <ByteMe.au3>

Link to comment
Share on other sites

Thanks for your fast response! This looks like I can understand it.

So it is ClipGet that actually gets me there and with $link you define the variable "link", i.e. meaning the browser's current address bar URL in it.

Side question:

All of this information/data/variables is stored in the system memory or in the browser cache or in the ram or where exactly please? I am trying to think of it in a way, sort of thinking in pictures, so if I find a picture that can make me understand the process I can understand the whole part of coding better, I am sort of coming from the creative side and often think in pictures.

Link to comment
Share on other sites

sleepydvdr,

what do you do with !d, do you send the keystroke ctrl+d, bookmarking the page? Similar I can image that ^c copies the selected link, is that right? Where can I read up on keystrokes and the ! and ^ in front of the letters, are those operators or where can I find out more about them? Also, just read this thread here, how would I use ControlGetText with FF. Please if you can and have a few other forum links on this or some tutorials, that would be highly appreciated, I like to learn this! Thanks.

edena,

I have no idea what you mean with "cookie behaviour".

Edited by AutoSam
Link to comment
Share on other sites

sleepydvdr,

what do you do with !d, do you send the keystroke ctrl+d, bookmarking the page? Similar I can image that ^c copies the selected link, is that right? Where can I read up on keystrokes and the ! and ^ in front of the letters, are those operators or where can I find out more about them? Also, just read this thread here, how would I use ControlGetText with FF. Please if you can and have a few other forum links on this or some tutorials, that would be highly appreciated, I like to learn this! Thanks.

edena,

I have no idea what you mean with "cookie behaviour".

!d sends ALT+d. That selects the address bar. The ^c copies the address to the clipboard. To find out more about these, look up the "send" command in the help file.

#include <ByteMe.au3>

Link to comment
Share on other sites

Oh and what about checking other browsers, like Opera, Chrome and Safari? Are there particular classes for that too? Can that be done with the Window Information Tool? So that basically it checks pretty much all of the widely available browser's URL in sequence, so that the script can be used with not just one browser. Is alt+d selecting the address bar in all browsers the same?

Link to comment
Share on other sites

In some cases the script will take the clipboard's previous content, at least this has happened to me. So I tried to change it to empty the clipboard before it copies new content to it but that did only always give me an empty clipboard result, as if the ctrl+c command was skipped. Do you know why?

ClipPut ("")
If WinExists("[CLASS:MozillaWindowClass]") Then
    WinActivate("[CLASS:MozillaWindowClass]")
ElseIf WinExists("[CLASS:IEFrame]") Then
    WinActivate("[CLASS:IEFrame]")
EndIf
Sleep(100)
send ("!d")
send ("^c")
$link = ClipGet()

MsgBox(0, "", $link)

Note FF was running during the test runs, IE not.

Link to comment
Share on other sites

If WinExists("[CLASS:Chrome_WidgetWin_0]") Then
    WinActivate("[CLASS:Chrome_WidgetWin_0]")
EndIf
Sleep(500)
Send ("!d")
Send ("^c")
$link = ClipGet ()
MsgBox(0, "Chrome current open tab URL", $link)

Tried with Chrome_WidgetWin_0, Chrome_AutocompleteEditView and Chrome_RenderWidgetHostHWND, all don't seem to pick up the script.

Does not seem to work for Chrome at all. Any suggestions?

Edit:

Same with Opera ( OperaWindowClass ). Any ideas?

Edited by AutoSam
Link to comment
Share on other sites

If you solved the problems you had with activating/focusing chrome browser please let me know how you solved it.

I figured I could solve it using a segment from my hotkey / automation program

This is part of a 4000 line Autoit program, I let the user browse the titles of running windows and store the selection title in $macro[$macroid][$macrocommand[$macroid][0]][1] and it works for me, but I have not figured out why I can't reproduce a simple version for you.

code segment that will focus IE or Google Chrome browsers with the user selected window title

This is just an example that works for me, but I've been working on the 4000 lines for over a year and am not sure what you would need to add yet.

but this is the event that does it for me.

Switch $macro[$macroid][$macrocommand[$macroid][0]][0]; switch macro_command_type
case $macrocommand_activate_window
  if $macro[$macroid][$macrocommand[$macroid][0]][2]= 1 Then;choose windowmode
   if winactivate($macro[$macroid][$macrocommand[$macroid][0]][1])<> 0 then
    WinSetState($macro[$macroid][$macrocommand[$macroid][0]][1], "", @SW_MINIMIZE)
   endif
  elseif $macro[$macroid][$macrocommand[$macroid][0]][2]= 2 Then
   if winactivate($macro[$macroid][$macrocommand[$macroid][0]][1])<> 0 then
    WinSetState($macro[$macroid][$macrocommand[$macroid][0]][1], "", @SW_MAXIMIZE)
   endif
  Else
   if winactivate($macro[$macroid][$macrocommand[$macroid][0]][1])<> 0 then
    WinSetState($macro[$macroid][$macrocommand[$macroid][0]][1], "", @sw_SHOWNORMAL)
   endif
  endif;end switch(windowmode)
  macrocommandinc($macroid)
endswitch; end switch macro_command_type

I developed this code as a solution to your post.

The code below works on chrome and IE, but will only work on chrome if the chrome window is not minimized

even still I had trouble focusing the chrome window even when I had activated it, so I get the position of the window and click the bastard to focus it.

but the window cords I get for the chrome window are off!, $po[0] seems to be the x cord just fine, but $po[1] is y+h so I tried subtracting $po[3] but $po[3] was only 20 which was not the height of the chrome window and I give up.

This will click the bottom of the chrome window (to focus it), only if the window is not minimized when you invoke the script. It will also work with IE, but IE can be minimized doesn't matter.

$browserlistdatamax= 2
$browserlistmax= 3
$link= "no browser found"; this Link is missing Zelda
dim $browserlist[$browserlistmax][$browserlistdatamax]
$browserlist[0][0]= "[CLASS:MozillaWindowClass]"; use 'autoit window info' tool to aquire these values from each browser
$browserlist[1][0]= "[CLASS:IEFrame]"
$browserlist[2][0]= "[CLASS:Chrome_WidgetWin_0]"
$browserlist[2][1]= 1; click window to focus
for $i= 0 to $browserlistmax-1; loop through browser list. catch the first browser we find, let all the other ones go
if WinExists($browserlist[$i][0])= 1 then; check if each browser is running
  winactivate($browserlist[$i][0])
  ;WinSetState($browserlist[$i][0], "", @SW_SHOWNORMAL)
  WinWaitActive($browserlist[$i][0])
  if $browserlist[$i][1]= 1 Then; click to focus
   $winpo= WinGetPos($browserlist[$i][0])
   mouseclick("left", $winpo[0]+10, $winpo[1]+10, 1, 0)
  endif; end if click to target window
  sleep(50)
  Send("{F6}")
  sleep(200)
  send ("^c")
  $link= ClipGet()
  exitloop
endif; end id winexists($browserlist[$i][0])
next; end for $browserlistmax-1
MsgBox(0, "browser address:", $link)
exit
 
func out($output= "");debug tool
ConsoleWrite(@CRLF&$output);to console: new line, value of $output
EndFunc; end out()
Link to comment
Share on other sites

songersoft, thanks so much for your lengthy reply, help and code! :graduated:

I have currently focused on Firefox only since I wanted to get the basic script done and then start adding more options, gui etc once I have the basic construct. I hope that is good programming architecture (is it???) as I have no clue about it and just think about it like building a house, first the cellar and walls, then the windows and carpets.

I had a look at your post yesterday but it did not make much sense to me then. However this has rapidly changed.

From looking at your script and having spent days in front of the screen with AutoIt, it starts to make sense, a lot of sense even. At the moment I am fine with it I think, I only need to read up on the variables you describe. $macro for example. Are those system wide variables or do I change those at will in my code as long as the meanings/semantics are kept?

Also

but the window cords I get for the chrome window are off!, $po[0] seems to be the x cord just fine, but $po[1] is y+h so I tried subtracting $po[3] but $po[3] was only 20 which was not the height of the chrome window and I give up.

does not make so much sense to me, meaning I don't understand it quite now, this however will change soon, no worries.

So I will be sure to use and edit this for my script, is it ok to come back to you once I have the basics done and working? Any ways, thanks, this is really appreciated.

Are there some good sections on how to design/build (good programming sequence I mean) AutoIt scripts perhaps, somehow I am worried about that, kinda would like to get it done half right from the beginning.

Link to comment
Share on other sites

$macro[$macromax][$macrocommandmax][$macrodatamax] is an array of numbers I created to store macro commands and data for macros.

I only included it because it is a segment of a 4000 line script that activates windows consistently.

Basically the whole macro thing is useless to you without the other 4000 lines of code.

you would replace the $macro[$x][$x][$x], with the title or class of the window you wanted to activate. but I was unable to make you a perfect solution.

So I will be sure to use and edit this for my script, is it ok to come back to you once I have the basics done and working? Any ways, thanks, this is really appreciated.

excellent, I put a couple of hours into it and felt like I failed. I hope very much that you can use it to learn. You may contact me, I will see you in the future Sam.
Link to comment
Share on other sites

but the window cords I get for the chrome window are off!, $po[0] seems to be the x cord just fine, but $po[1] is y+h so I tried subtracting $po[3] but $po[3] was only 20 which was not the height of the chrome window and I give up.

does not make so much sense to me, meaning I don't understand it quite now, this however will change soon, no worries.

Well I found that chrome windows did not receive focus when I activated them, as in I could get the window to show, but keystrokes were not received by chrome. So I get the position of the chrome window then try to click the window in a safe place to give it focus. $winpo becomes an array where $winpo[0] is the x cord of the chrome window, [1]y, [2]width, [3]height. However y or $winpo[1] was not the top cord, lets just say I do not understand that. I did not want to click all the browsers, so I added a click yes or no element to the browserlist[x][1] array. Then when the chrome browser is found the program knows to click it because 2 is chrome in the browserlist[2][0] and browserlist[2][1] is set to 1. I didn't set the other [x][1] elements to anything because I want them to be zero and when Autoit made the array it set all the elements to NULL or "", which is a empty string and mathematically zero here in Autoit. Oh god, I hope that makes sence.

here is where I set the array, and where I check if I should click the window. It is just a stripped down version of the code earlier, useful only for this example.

$browserlist[0][0]= "[CLASS:MozillaWindowClass]"; use 'autoit window info' tool to aquire these values from each browser
$browserlist[1][0]= "[CLASS:IEFrame]"
$browserlist[2][0]= "[CLASS:Chrome_WidgetWin_0]"
$browserlist[2][1]= 1; set to 1 to tell lines below to click this browse type
if $browserlist[$i][1]= 1 Then; click window to focus
  $winpo= WinGetPos($browserlist[$i][0])  
  mouseclick("left", $winpo[0]+10, $winpo[1]+10, 1, 0)
endif; end if click to target window
Edited by songersoft
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...