Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

IfWinActive based on URL, not Window Title


  • Please log in to reply
10 replies to this topic
NamesTwister
  • Members
  • 10 posts
  • Last active: Dec 12 2012 04:42 PM
  • Joined: 04 Dec 2012
I've been searching for a solution to this for way too long now.
I want one script to be active only if I'm at www.reddit.com and another script to only be active if I'm at www.gmail.com (and so on).
In Chrome the URL only shows up in ( TitleMatchMode=slow Visible Text ), I can't seem to get #IfWinActive to work with that.

Any help or alternate solutions for changing hotkeys based on what website I'm on?

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
These work for me
Reddit page active:
SetTitleMatchMode, 2
#IfWinActive, reddit:

F1::Msgbox Reddit Window Active!
Google search page active:
SetTitleMatchMode, 2
#IfWinActive, - Google Chrome

F1::Msgbox Google Search Window Active!
I used F1 in both instance as an example.
Feel free to change the hotkey to one you want.

Posted Image

don't duplicate, iterate!


NamesTwister
  • Members
  • 10 posts
  • Last active: Dec 12 2012 04:42 PM
  • Joined: 04 Dec 2012
That doesn't work for reddit if you are in the comments or a subreddit, which is why I think it has to be done by URL.
Forgot to mention that was my main problem. I can get everything to work fine if I stick to the reddit home page, but I need this to work in the comments page and subreddit pages.

njciancio
  • Members
  • 141 posts
  • Last active: Sep 14 2014 06:31 PM
  • Joined: 31 May 2012
What browser are you using? Extracting the page's URL will depend on this. This is a method I've used in an 'alternate history' script for Google Chrome:

DetectHiddenText, On ; the url of the active Chrome window is hidden text...
SetTitleMatchMode, Slow ; ...we also need match mode 'slow' to detect it
; activate chrome window,
; just for demonstation:
WinActivate, ahk_class Chrome_WidgetWin_1
IfWinActive, ahk_class Chrome_WidgetWin_1 ; we only want to check for the hidden text if Chrome is the active window,
{
WinGetText, wintext, ahk_class Chrome_WidgetWin_1 ; if it is we grab the text from the window...
If InStr(wintext,"autohotkey.com")	 ;		 ...and if it contains a url that we want,
{
;### code conditional on url goes here ###
msgbox % "The active Chrome window is on the autohotkey.com domain! The page title and URL is:`n`n>" wintext								 ; <<== we run the desired code here.
}
}
exitapp

Edit: I thought I'd add, use the hotkey command to turn hotkeys on and off with the above code.

LiveRegExTester - an ahk RegEx testing tool with live result highlighting


NamesTwister
  • Members
  • 10 posts
  • Last active: Dec 12 2012 04:42 PM
  • Joined: 04 Dec 2012
It's for google chrome.
I think that could work. Any way to put context-sensitive hotkeys in there?

njciancio
  • Members
  • 141 posts
  • Last active: Sep 14 2014 06:31 PM
  • Joined: 31 May 2012
Yes - the code I provided will only execute the msgbox (and any other code you want, including hotkey commands) if the active page is the desired url (autohotkey.com in my example). Please read about the hotkey command in the autohotkey help documents.

Instead of creating a hotkey like this:
^tab::
(some code)
return

Do it like this:
Hotkey, ^!z, MyLabel
return

MyLabel:
MsgBox You pressed %A_ThisHotkey%.
return
...this gives you flexibility for enabling and disabling hotkeys.

So you can disable your hotkey initially, like this:
Hotkey, $+#c, Off ; Disables the Shift-Win-C hotkey.

...and replace my msgbox command with:
Hotkey, #c, On ; Re-enables the Win-C hotkey.

Remember to disable the hotkey again once the window text does not contain the url you specify.

LiveRegExTester - an ahk RegEx testing tool with live result highlighting


NamesTwister
  • Members
  • 10 posts
  • Last active: Dec 12 2012 04:42 PM
  • Joined: 04 Dec 2012
I'm sorry, this is just making me more confused. Can you give me a full example of what the .ahk file would look like?

njciancio
  • Members
  • 141 posts
  • Last active: Sep 14 2014 06:31 PM
  • Joined: 31 May 2012
Here you go. Try it out. Win+Z will activate a message box only if:
- Chrome is the active window
- Autohotkey.com is the active page

;**********************************************************************
/*
IfWinActive based on URL, not Window Title
Started by NamesTwister, Today, 02:47 PM
http://www.autohotkey.com/board/topic/87788-ifwinactive-based-on-url-not-window-title/
*/
;**********************************************************************

; Initial use of the Hotkey Command
;-----------------------------------
Hotkey, #z, MyHotkeyLabel ; <<== this line of code does two things:
		 ; 1 - specifies the hotkey (Win+Z)
		 ; 2 - specifies the label (MyHotkeyLabel) to execute when the hotkeys are pressed
Hotkey, #z, MyHotkeyLabel, Off ; <<== this line turns our new hotkey OFF UNTIL we use another 'hotkey' command to turn it on.

; Set a Timer to Check the Active Window
;-----------------------------------
SetTimer, CheckActiveWindowLabel, 500 ; <<== this line tells the script to go to a label (CheckActiveWindowLabel)
							 ; every 1/2 second (500ms); we'll use this label to check if the autohotkey.com page
							 ; is active.
return
		
; MyHotkeyLabel
;-----------------------------------
MyHotkeyLabel: ; <<== this is the label which will execute when the hotkey is pressed...
; ...whatever code you want your hotkey to trigger goes here, for example, this message box:
msgbox % "This msgbox will only show if the active window of Chrome is at the autohotkey.com domain."
return

; Active Window Checking Label
;-----------------------------------
CheckActiveWindowLabel:
; settings to ensure we detect the hiddent text in Chrome:
DetectHiddenText, On ; the url of the active Chrome window is hidden text...
SetTitleMatchMode, Slow ; ...we also need match mode 'slow' to detect it
; IfWinActive checks to see if the Chrome window is active -
; if it is, we get the text of the window using WinGetText...
; ...this text will contain the url of the active page
IfWinActive, ahk_class Chrome_WidgetWin_1
{
WinGetText, wintext, ahk_class Chrome_WidgetWin_1
; here, we use the InStr() function to see if the text
; we got from Chrome contains a url we have specified
If InStr(wintext,"autohotkey.com")
{
	 ; if it does, whatever code is placed here will execute...
	 ; so we place a hotkey command here to turn our hotkey ON
	 ; (remember, the hotkey is off until now).
	 HotKey, #z, On
}
else
{
	 ; if the text doesn't contain "autohotkey.com"
	 ; this 'else' statement will execute. Here we must
	 ; turn the hotkey off.
	 HotKey, #z, Off
}
}
else
{
; also, make sure that the hotkey is off if the Chrome
; window is not active (not just if the page isn't
; an autohotkey.com page)
HotKey, #z, Off
}
return
esc::exitapp
PS: The code looks much less menacing with the explanatory comments removed - it is in reality quite simple and compact.

EDIT:
How do I know how to get the url out of Chrome? With Active Window Spy (see Scite4Autohotkey).
This tool reveals valuable information about every window, such as the URL being revealed in the
hidden text of Chrome - this is what Active Window Spy looks like:

>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
IfWinActive based on URL, not Window Title - Support - AutoHotkey Community - Google Chrome
ahk_class Chrome_WidgetWin_1
>>>>>>>>>>>>( Mouse Position )<<<<<<<<<<<<<
On Screen: 1009, 353 (less often used)
In Active Window: -663, 361
>>>>>>>>>( Now Under Mouse Cursor )<<<<<<<<
Color: 0xFFFFFF (Blue=FF Green=FF Red=FF)
>>>>>>>>>>( Active Window Position )<<<<<<<<<<
left: 1672	 top: -8	 width: 1696	 height: 1036
>>>>>>>>>>>( Status Bar Text )<<<<<<<<<<
>>>>>>>>>>>( Visible Window Text )<<<<<<<<<<<
IfWinActive based on URL, not Window Title - Support - AutoHotkey Community
>>>>>>>>>>>( Hidden Window Text )<<<<<<<<<<<
>>>>( TitleMatchMode=slow Visible Text )<<<<
www.autohotkey.com/board/topic/87788-ifwinactive-based-on-url-not-window-title/page__gopid__557244#entry557244
>>>>( TitleMatchMode=slow Hidden Text )<<<<
Note that the URL only appears under TitleMatchMode=slow Hidden Text. This is why we adjust these settings in the script above.

EDIT: For a more graceful example, see TLM's code below - note that using hotkey commands (vs. labels) allows the toggling of hotkeys anywhere in the script.

LiveRegExTester - an ahk RegEx testing tool with live result highlighting


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

Forgot to mention that was my main problem.

Its ok cause your new, make sure your very specific in your questions from now on.

Assuming your using the latest version of AHK, this should work.
#If WinActive("ahk_class Chrome_WidgetWin_1") && (GetWinText("www.reddit") || GetWinText("www.google"))

F1::Msgbox Chrome Active
ESC::ExitApp	; <-- Press escape to exit.

GetWinText(t) {
	WinGetText, wText, A
	Return RegExMatch( wText, "s)" t )
}
Give it a test.

Posted Image

don't duplicate, iterate!


NamesTwister
  • Members
  • 10 posts
  • Last active: Dec 12 2012 04:42 PM
  • Joined: 04 Dec 2012
njciancio,
I got your method to work (sort of). Sometimes the hotkey works, sometimes it doesn't.

TLM,
Thanks, that's exactly what I'm looking for.

Thanks for your help guys.

njciancio
  • Members
  • 141 posts
  • Last active: Sep 14 2014 06:31 PM
  • Joined: 31 May 2012

No worries. I'm not certain why the script is spotty for you - it works perfectly fine on my system.

Possibly, it's the set timer - it's supposed to toggle the hotkey in no more and no less than 500 ms, but such timing can be way off, especially if you don't have a lot of CPU overhead, or if there's lot going on in your script. I run a quite powerful desktop, so if you're on a laptop or an older computer, switching windows and using hotkeys quickly, my script might not be very reliable. There are other  more reliable ways you could check a condition at a specific interval, but that's another story for another day. Should you really want to use that method, you might try lowering the 500ms interval substantially (at a cost of cpu). Cheers!


LiveRegExTester - an ahk RegEx testing tool with live result highlighting