ifwinactive or batch file?

Get help with using AutoHotkey and its commands and hotkeys
bootlegRat
Posts: 8
Joined: 18 Feb 2018, 19:10

ifwinactive or batch file?

21 Feb 2018, 01:47

How can I make a script run when a program opens?

I know nothing of coding and I would like a specific code to be ran when I open a program. I have heard of batch files but I can't seem to get them to work, and is there a way to do it via ahk?

Thanks
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: ifwinactive or batch file?

21 Feb 2018, 01:59

You have to have an already running script to detect a programs open window on the fly.

Code: Select all

SetTitleMatchMode, 2
SetTimer, Check, % 1000 * 1   ; check every second ( = 1000ms)
Return

Check:
   If WinActive(" - Notepad")
      SoundBeep   ; beep if a Notepad window is (still) open
   Return
Click on every command within this code box to get further details! Happy scripting.
garry
Posts: 1568
Joined: 22 Dec 2013, 12:50

Re: ifwinactive or batch file?

21 Feb 2018, 03:23

also an example with PID and GUI
start ahk-script > start notepad and controlsend text to notepad
when close ahk-script > close also notepad

Code: Select all

; example =
; start notepad end controlsend to it
; close this script  > notepad close also
;---------------------------------------------

#warn
setworkingdir,%a_scriptdir%

pr=notepad.exe
sc=ahk_exe notepad.exe
IfWinNotExist,%sc%
 {
 Run, %pr%,,,pid2
 scx:= "ahk_pid " . PID2
 WinWait,%scx%
 }
IfWinNotActive ,%scx%,,WinActivate,%scx%
    WinWaitActive,%scx%
controlsend,,Line1`nLine2`n,ahk_pid %pid2%
Gui,2:default
Gui,2: +alwaysontop
Gui,2:Color,Black
Gui,2: font,s12 cBlack,Lucida Console
Gui,2: add,button , x10 y10 w100 h26 gA1,TEST1
Gui,2:show,x1 y1 w140 h50,TEST
return

a1:
msgbox, 262208,Button-Test1 ,You click on button test1
return

2Guiclose:
Gui,2:submit,nohide
Process ,Exist, %pid2%
if (ErrorLevel == pid2)
    Process, Close, %pid2%
exitapp
;=============== END test script =====================


Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], euras, hd0202, lmstearn, newbieforever and 32 guests