11

I'm looking for a way in AutoHotkey to put the currently visited url in a variable.

The goal of this AHK is to trace what I've been doing during the day to log my hours better. I have another system which I use to clock my work, but sometimes I forget to use it when I get side-tracked.

loop
{
    ; Get current Window ID & Name
    WinGet, active_id, ID, A
    WinGet, process_name, ProcessName, A

    ; Only do anything if any other windows was activated
    if(active_id = PrevActiveId)
    {
        ; Do nothing
    }
    else
    {
        ; Format the time-stamp.
        current=%A_DD%/%A_MM%/%A_YYYY%, %A_Hour%:%A_Min%

        ; Write this data to the log.txt file.
        fileappend, %current% - %process_name%`n, log.txt

        ; Get the URL if process_name = "chrome.exe"
        if(process_name = "chrome.exe")
        {
            ; Put URL in log file
            ; fileappend, %current% - %current_url%`n, log.txt
        }
    }

    PrevActiveId = %active_id%
    Sleep, 100
}
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
masterdam79
  • 113
  • 1
  • 1
  • 7
  • 3
    Solved it but "Users with less than 100 reputation can't answer their own question for 8 hours after asking. You may self-answer in 5 hours." So will report back in 5 hours..... :-( – masterdam79 Jun 08 '11 at 09:28
  • 2
    Good idea to come back in 5 hours and post your answer. It could help others :) – ardavis Jun 15 '11 at 01:08

6 Answers6

6

All browsers that I've used support Alt+D to focus and select the url. Here are my AHK scripts that duplicates the current tab in Google Chrome, Firefox, and Internet Explorer by pressing Ctrl+Shift+D..

#IfWinActive ahk_class MozillaUIWindowClass ; Mozilla Firefox 3.x
   ^+d::GenericDuplicateTab() ; (Control+Shift+D)
#IfWinActive

#IfWinActive ahk_class MozillaWindowClass ; Firefox 4, 5, 6, 7, 8+ (?)
   ^+d::GenericDuplicateTab() ; (Control+Shift+D)
#IfWinActive

#IfWinActive ahk_class Chrome_WidgetWin_1 ; Chromium and Chrome 19+
   ^+d::GenericDuplicateTab() ; (Control+Shift+D)
#IfWinActive

#IfWinActive ahk_class Chrome_WidgetWin_1 ; Chrome 18 and less
   ^+d::GenericDuplicateTab() ; (Control+Shift+D)
#IfWinActive

#IfWinActive ahk_class IEFrame
   ^+d::InternetExplorerDuplicateTab() ; (Control+Shift+D)
#IfWinActive

GenericDuplicateTab()
{
   ; Wait for both Control and Shift to be released.
   KeyWait Control
   KeyWait Shift

   BackupClipbrd := Clipboard
   Sleep 50

   Send !d ; Select the url textbox
   Sleep 150

   Send ^x ; Copy the url
   ClipWait 0.1
   If ERRORLEVEL
   {
    Clipboard := BackupClipbrd
    Return
   }

   Send ^t ; Open a new tab
   Sleep 50

   Send ^v ; Paste the url into the new tab's url textbox
   Sleep 50
   Send {Enter}

   Clipboard := BackupClipbrd
}

InternetExplorerDuplicateTab()
{
   ; Wait for both Control and Shift to be released.
   KeyWait Control
   KeyWait Shift

   Send ^k ; Call IE's shortcut to duplicate tab (Control+K)
   Sleep 100

   Send ^{TAB} ; Switch to that tab
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
kodybrown
  • 2,337
  • 26
  • 22
4

Or you can use F6.

Most of the browsers get to address bar and select entire URL for you when press F6.

Then its just matter of copy-paste.

For newer Firefox versions, its Ctrl+L though.

For this, you can check window title.

Syscall
  • 19,327
  • 10
  • 37
  • 52
tumchaaditya
  • 1,267
  • 7
  • 19
  • 49
  • this is not a good trick . i am interfering in user browser which user can think is a hack short of thing . – Himanshu sharma May 30 '16 at 05:25
  • I recommend using **Alt+D** instead of F6. The F6 key toggles focus, meaning you can accidentally unselect the URL bar if it was already in focus. – Stevoisiak Sep 22 '17 at 18:46
4

For Chrome, get the text of the control Chrome_OmniboxView1, which is the omnibox (as for the current version of Chrome, 21.0.1180.83).

This code puts the content of the omnibox into the variable omniboxContents:

ControlGetText omniboxContents, Chrome_OmniboxView1, Chrome

Note that omniboxContents doesn't necessarily contain a correct URL, because "http://" is left out if the URL starts with "http://". So instead of "http://www.google.com" you will get "www.google.com", which strictly speaking isn't a correct URL. This is simply because Chrome shows the address that way in the Omnibox. You will have add extra code to get a correct URL from the contents of the omnibox.

2

A clean way to do this:

    GroupAdd, WebBrowsers, ahk_class MozillaWindowClass
    GroupAdd, WebBrowsers, ahk_class IEFrame
    GroupAdd, WebBrowsers, ahk_class Chrome_WidgetWin_0
    GroupAdd, WebBrowsers, ahk_class Chrome_WidgetWin_1
    GroupAdd, WebBrowsers, ahk_class OperaWindowClass
    GroupAdd, WebBrowsers, ahk_class {1C03B488-D53B-4a81-97F8-754559640193}
    ; etc.

    #IfWinActive, ahk_group WebBrowsers
    {
        ^+d::
        ; [Instructions...]
        return
    }#If
Exnius
  • 31
  • 2
2

It is also possible to do it without relying on the clipboard, by directly inspecting the actual components which make up the browser window. More info: http://www.autohotkey.com/board/topic/111114-get-the-url-of-the-current-active-browser-tab/. This method much more effective, but relies on using OS APIs.

Mihai Rotaru
  • 1,953
  • 3
  • 26
  • 28
1

You can use the Alt+D shortcut to activate address bar and then copy the url using Ctrl+C shortcut

F1::
{
Send, !d
Sleep 50
Send, ^c
Sleep 50
Return
}
insearchofcode
  • 438
  • 5
  • 9