Switch to or launch an App
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Switch to or launch an App
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...)
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...)
Re: Switch to or launch an App
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.
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.
If you cycle in the opposite order, this is all easy with GroupActivate.
That's a start for you. WinGet gets the list of matching windows. Best of luck!Activates the next window in a window group that was defined with GroupAdd.
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.
Re: Switch to or launch an App
Among other issues, line 48 be:
…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.
Code: Select all
If WinExist("ahk_exe chrome.exe")
Moving this thread to the v2 section.
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Re: Switch to or launch an App
Perfect, that's exactly what I needed to know.boiler wrote: ↑29 Jan 2023, 23:28Among other issues, line 48 be:…and the same for the WinActivate line. That’s why it is treating ahk_exe as a variable. You need quotes around literal strings.Code: Select all
If WinExist("ahk_exe chrome.exe")
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Re: Switch to or launch an App
Many thanks @mikeyww - I'll investigate and let you know how I get on!mikeyww wrote: ↑29 Jan 2023, 17:26Unless 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.That's a start for you. WinGet gets the list of matching windows. Best of luck!Activates the next window in a window group that was defined with GroupAdd.
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.
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Re: Switch to or launch an App
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
Re: Switch to or launch an App
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.
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.
Re: Switch to or launch an App
Thanks, boiler, I never noticed that!
RoyMarriott: Sorry if I unintentionally posted some v1 links in the v2 forum. You cited the correct section.
RoyMarriott: Sorry if I unintentionally posted some v1 links in the v2 forum. You cited the correct section.
Re: Switch to or launch an App
No problem! The docs2 tag is somewhat new.
-
- Posts: 9
- Joined: 14 Jan 2023, 13:15
Re: Switch to or launch an App
Really appreciate help from both of you