June 16, 2023

AutoHotkey Alternative to Browser Redirectors

Unfortunately using tools like Browser Tamer to redirect links to specific browsers or browser profiles is not allowed by our IT. As alternative I present here an AutoHotkey-based solution that will open links with preconfigured browsers on a Shift+Click.

Configuration

The url<-> browser mapping is configured in an .ini file of following format:

[Browsers]
ChromeJira = "C:\Program Files\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 2"
ChromeWork = "C:\Program Files\Google\Chrome\Application\chrome.exe" --profile-directory="Default"
ChromePerso = "C:\Program Files\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1"
ChromeJiraCloud = "C:\Program Files\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 8"
[BrowserRules]
ChromeJira = jira.xxx.de,wiki.xxx.de
ChromeWork = yyy.atlassian.net
ChromePerso = autohotkey.com,github.com,goodreads.com,tdalon.blogspot.com,linkedin.com,stackoverflow.com,tdalon.github.io
ChromeJiraCloud = xxx.atlassian.net

In the .ini file you can declare in the section [Browsers] the list of Browsers you want to used in the next sections.

Each line has the format BrowserName= command string


In the .ini file section [BrowserRules] you can configure the rules upon which an url will be opened with a specific browser.

A rule has following format:
BrowserName=url1,url2,url3

BrowserName is previously declared in the [Browsers] section.
The list of urls are separated by a comma. A match is done if e.g. url1 is part of the Url you want to open.

If no match is found, the system default browser will be used.

AutoHotkey implementation

To get the url of the link under the mouse cursor, the script makes use of opening the context menu. (Right-Mouse Click)

The implementation depends on the application used.
The default way is to use the C (for copy) accelerator key to Copy the hyperlink.

For Excel it uses the H accelerator copy for Copy Hyperlink.

Open Link Function

It is available in the NWS.ahk script 

Open Link Hotkey

The Hotkey is implemented in NWS.ahk as follows:

#IfWinActive, ahk_group OpenLinks
; Open in Default Browser (incl. Office applications) - see OpenLink function

; Shift Mouse click
+LButton:: ;
Clip_All := ClipboardAll  ; Save the entire clipboard to a variable
Clipboard =  ; Empty the clipboard to allow ClipWait work

Send {Shift Up} ; Release shift because of Conflict to open context menu if pressed down
SendEvent {RButton} ;Click Right does not work in Outlook embedded tables
sleep, 200 ;(wait in ms) give time for the menu to popup    

If WinActive("ahk_exe onenote.exe")
    SendInput i ; Copy Link
Else If WinActive("ahk_exe " . Teams_GetExeName()) ; ByPass SafeLink https://tdalon.blogspot.com/2023/01/teams-bypass-safelink.html
    SendInput {Up} {Enter}
Else If WinActive("ahk_exe chrome.exe")
    SendInput e ; Send the underlined key https://superuser.com/questions/1721702/how-to-show-the-underlines-for-navigation-key-hotkey-in-context-menu-when-ri that copies the link from the right click menu. see https://productforums.google.com/forum/#!topic/chrome/CPi4EmhqHPE
    ; See also https://stackoverflow.com/questions/62707998/chrome-windows10-right-click-context-menu-option-underline-on-key-missing Press Alt+Shift before Right-Click
Else If WinActive("ahk_exe EXCEL.exe") {
    SendInput H
    sleep 500
    Send ^c
    Send {Esc}
} Else
    SendInput c ; Copy Link

ClipWait, 2
sUrl := Clipboard

If sUrl { ; Not empty
    OpenLink(sUrl)
} Else {
    Send {LButton}
}      

Clipboard := Clip_All ; Restore the original clipboard
return

See also

Browser Tamer (Tool recommendation)

Redirect links in browser to default browser (using BrowserTamer)

Opening external links from Teams in dedicated browser (bypass safelinks)

No comments:

Post a Comment