Switch to or launch an App

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Switch to or launch an App

Post by RoyMarriott » 29 Jan 2023, 16:41

Hi Everyone,
I'd like create a hotkey that switches to an app, e.g. Chrome, Word etc etc.
If it isn't open yet, then it opens it.
If there's more than one instance already open, it switches to the most recently used one on a single press of this key, and then with a second press of the key switches to the next most recently used instance, etc.

How can I do that?

Apologies if this is a repeat of an old topic and already solved. (I found https://www.autohotkey.com/board/topic/107450-switch-to-or-open-application/ but it's for v1 and I couldn't get it working.... I just get the following error message, as ahk_exe isn't assigned a value...)
image.png
image.png (10.91 KiB) Viewed 1964 times

User avatar
mikeyww
Posts: 26857
Joined: 09 Sep 2014, 18:38

Re: Switch to or launch an App

Post by mikeyww » 29 Jan 2023, 17:26

Unless you change your definition of "most recent", you will cycle between a pair of windows, because after you switch from the first window to the second window, the window that was most recently accessed is the first one. Some of the windows could also disappear after the initial assessment of windows, and "initial assessment of windows" is undefined in terms of when it happens.

If you cycle in the opposite order, this is all easy with GroupActivate.
Activates the next window in a window group that was defined with GroupAdd.
That's a start for you. WinGet gets the list of matching windows. Best of luck!

A possible strategy:
1. A hotkey assesses a window group. Another hotkey traverses the group's windows.
2. An index tracks the current window's position in the group (1, 2, 3...).
3. When traversing, the index is incremented. If at the end, it restarts at 1.
4. If a window is not found, the index is incremented again.

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Switch to or launch an App

Post by boiler » 29 Jan 2023, 23:28

Among other issues, line 48 be:

Code: Select all

If WinExist("ahk_exe chrome.exe")
…and the same for the WinActivate line. That’s why it is treating ahk_exe as a variable. You need quotes around literal strings.

Moving this thread to the v2 section.

RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Re: Switch to or launch an App

Post by RoyMarriott » 30 Jan 2023, 13:12

boiler wrote:
29 Jan 2023, 23:28
Among other issues, line 48 be:

Code: Select all

If WinExist("ahk_exe chrome.exe")
…and the same for the WinActivate line. That’s why it is treating ahk_exe as a variable. You need quotes around literal strings.
Perfect, that's exactly what I needed to know.

RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Re: Switch to or launch an App

Post by RoyMarriott » 30 Jan 2023, 13:49

mikeyww wrote:
29 Jan 2023, 17:26
Unless you change your definition of "most recent", you will cycle between a pair of windows, because after you switch from the first window to the second window, the window that was most recently accessed is the first one. Some of the windows could also disappear after the initial assessment of windows, and "initial assessment of windows" is undefined in terms of when it happens.

If you cycle in the opposite order, this is all easy with GroupActivate.
Activates the next window in a window group that was defined with GroupAdd.
That's a start for you. WinGet gets the list of matching windows. Best of luck!

A possible strategy:
1. A hotkey assesses a window group. Another hotkey traverses the group's windows.
2. An index tracks the current window's position in the group (1, 2, 3...).
3. When traversing, the index is incremented. If at the end, it restarts at 1.
4. If a window is not found, the index is incremented again.
Many thanks @mikeyww - I'll investigate and let you know how I get on!

RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Re: Switch to or launch an App

Post by RoyMarriott » 30 Jan 2023, 17:26

mikeyww wrote:
29 Jan 2023, 17:26
WinGet gets the list of matching windows. Best of luck!
Can I just check something? Your link seems to go to v1 documentation - should I use v1 to do this, or can I do it in v2?
If v2, is https://www.autohotkey.com/docs/v2/lib/WinGetList.htm the right version of the WinGet function to use?
Thanks again :-)

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Switch to or launch an App

Post by boiler » 30 Jan 2023, 18:00

You can use v2 for anything you can use v1 for, although some libraries and custom functions available in v1 might not have v2 versions yet.

By the way, you can use the <docs></docs> tags (with square brackets [ ], not angular ones <>) to link to the documentation page for v1 commands/functions and and <docs2></docs2> tags to link to v2 documentation pages. Both are available in buttons above the reply box. See the difference between these two links: MsgBox and MsgBox. If you quote my post (before/without submitting it), you can see the actual tags used.

User avatar
mikeyww
Posts: 26857
Joined: 09 Sep 2014, 18:38

Re: Switch to or launch an App

Post by mikeyww » 30 Jan 2023, 18:14

Thanks, boiler, I never noticed that!

RoyMarriott: Sorry if I unintentionally posted some v1 links in the v2 forum. You cited the correct section.

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Switch to or launch an App

Post by boiler » 30 Jan 2023, 18:16

No problem! The docs2 tag is somewhat new.

RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Re: Switch to or launch an App

Post by RoyMarriott » 31 Jan 2023, 06:12

Really appreciate help from both of you :-)

Post Reply

Return to “Ask for Help (v2)”