Hotkey when one of two programs are active

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CuriousDad
Posts: 75
Joined: 21 Mar 2023, 11:23

Hotkey when one of two programs are active

18 Jan 2024, 16:38

I would like to pause my script (which starts up when my computer boots up) to allow me to use F12 as the "Save As" function whenever Word or Excel is active. I wrote the following thread within the script:

Code: Select all

#IfWinActive ahk_exe EXCEL.EXE or ahk_exe WINWORD.EXE
	^+s:: ; Save As
	Run "I:\Temp Downloads\Macros\Suspend 7 Seconds.ahk"
	Sleep 500
	Send {F12}
	ExitApp
I have another script that will restart my original script after 7 seconds. The script works for Word, but not for Excel. Am I utilizing the "or" statement incorrectly?

Secondary question: ChatGPT suggested use of Pause function. Would it work for what I want to do? How would I automatically restart the script after 7 seconds?
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Hotkey when one of two programs are active

18 Jan 2024, 19:31

CuriousDad wrote:
18 Jan 2024, 16:38
The script works for Word, but not for Excel. Am I utilizing the "or" statement incorrectly?
The right syntax is

Code: Select all

#If WinActive("ahk_exe EXCEL.EXE") or WinActive("ahk_exe WINWORD.EXE")
The rest of your question isn't clear to me. It needs to be clarified.
There are probably much better ways to accomplish that task.
CuriousDad
Posts: 75
Joined: 21 Mar 2023, 11:23

Re: Hotkey when one of two programs are active

18 Jan 2024, 22:02

Thanks. It now works.

As for the use of Pause, ChatGPT wrote the following:

Code: Select all

F12::
Pause ; Pause the current script
Send {F12} ; Send F12 to the active window
Pause ; Unpause the current script
return
Will accomplish the same thing that I'm doing with my other hotkey thread if I modified it as such:

Code: Select all

#If WinActive("ahk_exe EXCEL.EXE") or WinActive("ahk_exe WINWORD.EXE")
^+s:: ; Save As
Pause ; Pause the current script
Send {F12} ; Send F12 to the active window
Sleep 7000
Pause ; Unpause the current script
return
CuriousDad
Posts: 75
Joined: 21 Mar 2023, 11:23

Re: Hotkey when one of two programs are active

18 Jan 2024, 22:08

Sorry. I know that I could test it out myself. I was just responding to the comment of using Pause. The question stemmed from my explanation of what I was asking.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Hotkey when one of two programs are active

19 Jan 2024, 05:44

ChatGPT output is not permitted on this forum.

If you have a loop or timer running somewhere in the script, you can have that loop check to see which window is active. If the active window 's title is "Save As", then you skip the actions until the window title changes.

Code: Select all

#Requires AutoHotkey v1.1.35
#Persistent
Gosub Timer
Return

Go:
WinGetTitle active, % "ahk_id" WinActive("A")
If (active = "Save As")
 WinWaitNotActive
Send 1
SoundBeep 2500
Timer:
SetTimer Go, -900
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 268 guests