[basic] go to Chrome if it is running; else run it Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
junberry
Posts: 4
Joined: 06 Aug 2020, 17:05

[basic] go to Chrome if it is running; else run it

06 Aug 2020, 21:16

Hello,

I would like to ask the community for help on a seemingly basic command that somehow doesn't work on one specific device.

It is a script that replaces the caps lock key with the following:
  • * check for an existing instance of chrome.exe
    * if this exists, switch to Chrome; if currently on a new tab, close it, else open a new tab and go to address bar
    * if there is no existing instance of chrome.exe, run it
The script has been working perfectly well, so I moved it onto a new device, where it has mysteriously stopped working. (Both devices are running the latest version of Windows.)

After progressively trimming down the script to identify the problem, I am left with the following bare-bone script:

Code: Select all

Capslock::

SetTitleMatchMode, 2

If WinExist ("ahk_exe chrome.exe")

{
WinActivate, ahk_exe chrome.exe
Send ^t
}	

else

{
Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
WinActivate, ahk_exe chrome.exe
Send ^l
Send ^t
}

return

+Capslock::Capslock

And it still does not seem to work!

The desired result of this trimmed down script is:
Check if Chrome is running. If so, switch to it and open a new tab; if not, run Chrome, switch to it, go to navigation bar, then open a new tab.

Instead, I observe the following behaviour:
When Chrome is running, the script runs successfully (i.e. switches to Chrome and opens a new tab).
When Chrome is NOT running, however, the script behaves unpredictably. A few hours ago, it sent ^l and ^t without running Chrome (which results in explorer going to the navigation bar with ^l then playing an error sound); but now, it appears to just sit there and do absolutely nothing when Chrome isn't already running.

Could someone shed some light on why this might be happening, and how I can change the code to make it more stable?

Thanks!
junberry
Posts: 4
Joined: 06 Aug 2020, 17:05

Re: [basic] go to Chrome if it is running; else run it

07 Aug 2020, 08:27

Hi Xtra,

Thank you for the suggestion. I've added a WinWait command as follows:

Code: Select all

else

{
Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
WinActivate, ahk_exe chrome.exe
WinWaitActive, ahk_exe chrome.exe
Send ^l
Send ^t
}
It does not appear to change the behaviour.

In fact, prior to trimming down the code for troubleshooting, I used sleep 100 after the run command (and WinWaitActive after any WinActivate commands); this worked on the original device but not the current device.

Another thing I observed during testing:

Prior to trimming down, the code used to look for newly open tabs if Chrome is already open. If a tab titled 'new tab' existed, that tab would be closed; else a new tab would open (so like an on/off button). What I noticed was that the script appears to somehow run in the background, such that nothing appears to happen, but when I manually run Chrome from elsewhere afterwards, it instantly closes because the script suddenly takes effect to close the 'new tab'.

I don't understand why this is so; perhaps this is a device-specific quirk, where Chrome is actually running in the background and can somehow be detected by WinExist but cannot be called by WinActivate?
User avatar
Xtra
Posts: 1913
Joined: 02 Oct 2015, 12:15

Re: [basic] go to Chrome if it is running; else run it

07 Aug 2020, 17:02

Try it like this:

Code: Select all

Capslock::

SetTitleMatchMode, 2

If WinExist ("ahk_exe chrome.exe")
{
    WinActivate, ahk_exe chrome.exe
    WinWaitActive, ahk_exe chrome.exe
    Send, {Ctrl Down}t{Ctrl Up}
}
else
{
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    WinWait, ahk_exe chrome.exe
    WinActivate, ahk_exe chrome.exe
    WinWaitActive, ahk_exe chrome.exe
    Send, {Ctrl Down}l{Ctrl Up}
    Sleep, 50
    Send, {Ctrl Down}t{Ctrl Up}
}

return
junberry
Posts: 4
Joined: 06 Aug 2020, 17:05

Re: [basic] go to Chrome if it is running; else run it

13 Aug 2020, 14:16

Hi Xtra,

Thanks for the help and sorry it took a while for me to try this out.

I used your script exactly as prescribed. The same issue was encountered, namely:

* The script works when Chrome is already visible
* It does not work when Chrome is not visible; however...
* If we activate the script by pressing caps lock then open Chrome manually, the newly opened Chrome has one new tab (and only one, regardless of how many times we press caps lock beforehand).
User avatar
Xtra
Posts: 1913
Joined: 02 Oct 2015, 12:15

Re: [basic] go to Chrome if it is running; else run it  Topic is solved

13 Aug 2020, 15:28

I just noticed

Code: Select all

If WinExist ("ahk_exe chrome.exe")
Should be

Code: Select all

If WinExist("ahk_exe chrome.exe")
I copy pasted from your OP and didnt see it before.
junberry
Posts: 4
Joined: 06 Aug 2020, 17:05

Re: [basic] go to Chrome if it is running; else run it

13 Aug 2020, 16:12

It worked! I can't believe it was such a tiny little thing!

Thank you :) I am going to insert the other sections and test it again. If everything goes well, I will close this topic.

Thanks again.

Edit:
That was exactly the issue. Everything works with the space removed!

Return to “Ask For Help”

Who is online

Users browsing this forum: charge, hasantr, RubbeH, songdg and 48 guests