Focus or open if app doesn't exist shortcut stops working after first time

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TheNomadicAspie
Posts: 140
Joined: 07 Jan 2020, 15:39

Focus or open if app doesn't exist shortcut stops working after first time

23 Jul 2021, 14:03

I'm using Caps + V to open Visual Studio Code, but about half the time I use the shortcut it breaks the shortcut. And not always in the same way. Sometimes I'm unable to use Caps + V after opening or focusing on it one time, sometimes I'm not able to use any hotkeys, sometimes it works fine.

I'm going to post my whole AHK file in case there's something wrong with the code before or after it? Or could it be that there's spaces in the file name? Is there a way around that? I could remove the spaces in the program folder, but I think that may break the program itself right? I censored the username of my computer below, the XXXXXX isn't the problem.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

CapsLock::SetCapsLockState, % A_PriorHotkey = "CapsLock" && A_TimeSincePriorHotkey < 500 ? "On" : "Off"
Return

CapsLock & c::
IfWinExist ahk_exe chrome.exe
	winactivate ahk_exe chrome.exe
else
	run, "C:\Program Files\Google\Chrome\Application\chrome.exe"
WinWait ahk_exe chrome.exe
WinActivate ahk_exe chrome.exe
WinWaitActive ahk_exe chrome.exe
Return

CapsLock & t::
IfWinExist ahk_exe todoist.exe
	winactivate ahk_exe todoist.exe
else
	run, "C:\Users\XXXXXX\AppData\Local\Programs\todoist\Todoist.exe"
WinWait ahk_exe todoist.exe
WinActivate ahk_exe todoist.exe
WinWaitActive ahk_exe todoist.exe
Return

CapsLock & v::
IfWinExist ahk_exe Visual Studio Code.exe
	winactivate ahk_exe Visual Studio Code.exe
else
	run, "C:\Users\XXXXXX\AppData\Local\Programs\Microsoft VS Code\Code.exe"
WinWait ahk_exe Visual Studio Code.exe
WinActivate ahk_exe Visual Studio Code.exe
WinWaitActive ahk_exe Visual Studio Code.exe
Return

CapsLock & s::
IfWinExist ahk_exe spotify.exe
	winactivate ahk_exe spotify.exe
else
	run, "C:\Users\XXXXXX\AppData\Roaming\Spotify\Spotify.exe"
WinWait ahk_exe spotify.exe
WinActivate ahk_exe spotify.exe
WinWaitActive ahk_exe spotify.exe
Return

CapsLock & j::
IfWinExist ahk_exe chrome.exe
	winactivate ahk_exe chrome.exe
else
	run, "C:\Program Files\Google\Chrome\Application\chrome.exe"
WinWait ahk_exe chrome.exe
WinActivate ahk_exe chrome.exe
WinWaitActive ahk_exe chrome.exe
Send ^t
Sleep 50
SendInput http://localhost:8096/web/index.html{Enter}
Return
User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Focus or open if app doesn't exist shortcut stops working after first time

23 Jul 2021, 14:29

Since your script does not work, pare it down until you get it working. You could start with the following, with no other code in the script.

Code: Select all

wTitle := "ahk_exe " app := "notepad.exe"
CapsLock & v::
If WinExist(wTitle)
 WinActivate
Else Run, %app%
Return
This worked in my testing. If it works for you, add to the script incrementally, testing carefully along the way.
TheNomadicAspie
Posts: 140
Joined: 07 Jan 2020, 15:39

Re: Focus or open if app doesn't exist shortcut stops working after first time

23 Jul 2021, 17:07

Thank you, I was able to get it to work after some troubleshooting as well as defining the entire path instead of just the name.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 387 guests