Reputation: 498
I have this code allready
!^j::
If WinExist("The Title")
WinActivate
else
Run, https://webpage/index.php
Return
Whenever I hit the hotkey, if the web page wanted is open, then the browser is maximized. But if the web page is not open, I have to open the browser (or a new Tab if browser allready open) and go to that page. Up to now everything seems good, but there is one problem...
If the browser is open, and that page is also open but the browser is no sitting in that tab the program open a new tab, which means that every time the browser is in another Tab than the one containing the page looked for, a new tab will be open, how to prevent this?
Is there anyway to fix it? like a function to get all browser tabs and doing a loop?
Edit: Thanks to @Jsmith2800, now I have this code:
!^j::
SetTitleMatchMode, 2
WinActivate, - Mozilla Firefox
SetTitleMatchMode, 1
If WinExist("The Title")
WinActivate
else {
winGetActiveTitle, Title
loop{
send ^{TAB}
If WinExist("The Title")
break
If WinExist(Title)
break
}
If WinExist(Title)
Run, https://webpage/index.php
}
Return
First, I have to open the browser window, so the Ctrl+Tab actually change tab. So the first 3 lines are for opening Mozilla Firefox.
Then I ask if the page I'm looking for is in the current tab, if it is, then only activate that tab. But if it is not, then get the current Title and start a loop.
In that loop, first change tab, then ask again if that new tab is the one I was looking for. If the new tab is the one, break the loop and return. If not, ask if the new tab is the same one before starting the loop, if that is true, then the wanted page is not openned, so open it and break the loop.
This is working right, except for one little thing: The tabs are acceded in a random way, so sometimes it goes back to the Title
page before reaching the wanted one How can I make it not random. Allready tried with SetWinDelay, 250
but didn't work.
Upvotes: 1
Views: 4184
Reputation: 1113
I don't have a great deal of experience with Autohotkey, but I found this which seems like it would cover what you need, you'd need to combine it with some of the bits you have there already, but if my understanding is correct it should look something like this.
If WinExist("The Title")
WinActivate
else {
winGetActiveTitle, Title
loop{
send ^{TAB}
if WinActive ("The Title")
break
if WinActive ("Title")
break
Run, https://webpage/index.php
else
continue
}
}
I'm not 100% on this as it looks a little messy to me (an if else statement nested within an else statement) but from my understanding this should first look to see if the title you're looking for exists, if it does great, it will activate that page. If it doesn't it will then drop into the next statement, which will record the title of your currently selected page as the variable 'Title' and then start a loop, where it will send the keys ctrl+tab to move to the next tab, if the window title now equals what you are looking for (in this example, shown as 'The Title'), it will stop the loop. If the title equals the title you recorded at the start (i.e. you have looped round all the open tabs) it will break the loop, and open the page you need. If it doesn't equal either of these, it will restart the loop and hit Ctrl+tab again, check the title again etc until it meets one of the 2 above criteria.
Hopefully this will help you, even if it's just the link to the website with information on that helps you, as I say I'm not the most experience AHK user so can't guarantee my suggestion will work, still good luck with it.
Upvotes: 1
Reputation: 498
Ok, I solved this, thanks to @Jsmith2800 that gave me a head up. Here is the new code
!^j::
SetTitleMatchMode, 2
WinActivate, - Mozilla Firefox
SetTitleMatchMode, 1
If WinExist("The Title")
WinActivate
else {
searchedTab := "The Title"
WinGetActiveTitle, StartingTitle
loop{
send {Control down}{Tab}{Control Up}
Sleep 400
IfWinActive, %SearchedTab%
Return
WinGetActiveTitle, CurrentTabTitle
If (CurrentTabTitle == StartingTitle)
break
}
Run, https://webpage/index.php
}
Return
So, a fast explain to solution. after openning the browser tab, and checking if page looked for is active... When entering the loop, I get the title of the current page. Inside the loop, first change tab, then sleep .4 secs, so active tab changes and then ask the same (if page wanted, done. If not, if first page searched, exit the loop and run wanted page (also work if not browser open)).
Basically, the problem was that SetWinDelay was not working as expected, replace it with Sleep and done. Also if browser was not open, the old script was useless.
I hope to be clear and with this help anyone who need it.
Upvotes: 0