Open and close Script when application is opened and closed

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
shehanbope
Posts: 1
Joined: 23 May 2022, 03:45

Open and close Script when application is opened and closed

Post by shehanbope » 23 May 2022, 03:57

I'm really new here so pardon me if i haven't followed any rules here yet.. :/
So i need to figure out if its possible to open and close AHK scripts attached to applications
example : I have a script written just for photoshop functions and i want to open the photoshop script automatically with the application and when i close photoshop i need it close without me manually going to the system tray to close the script...
Is that something I can do ?

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

Re: Open and close Script when application is opened and closed

Post by mikeyww » 23 May 2022, 04:27

Welcome to this AutoHotkey forum!

You could do it with WinWaitActive, but if you run one master script and use #IfWinActive to set a context, it often makes for a more manageable coding experience. You can also frequently avoid software or hook conflicts by doing so. You can define as many different contexts as you like within one script. You can then use the same hotkeys to do different things in the different contexts. Example

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Open and close Script when application is opened and closed

Post by BoBo » 23 May 2022, 06:06

Well, I've had a try... and AFAICS it should do the job.
A process-associated script file has to be named '<processname>.ahk'.
So, 'notepad.exe' makes it mandatory to create a 'notepad.ahk' beforehand.

Code: Select all

#SingleInstance, Force
#Persistent
SetWorkingDir, A_ScriptDir

global apps := {"notepad.exe":0                       ; apps-array <application>:<script status>
               ,"mspaint.exe":0
               ,"calculator.exe":0}

SetTimer, runApp, 500                                 ; check every 500ms..
Return

runApp:
   for proc, scr in apps                              ; ..for existing apps that are listed in the array
      proc(proc)
   Return

proc(app) {
   if FileExist(app)=0 {                              ; checking for an associated script for that process
      MsgBox % "Missing " SubStr(app,1,-4) ".ahk !"
      ExitApp
      }
   Process, Exist,% app                               ; let's see if the expected app is executed?!
   if (errorlevel != 0) && (apps[app] = 0) {          ; it is - but no associated script is running.
      Run % SubStr(app,1,-4) ".ahk",,, scrID          ; so, let's run the script..
      apps[app] := scrID                              ; ..and update its status within the apps-array.
      }
   if (errorlevel = 0) && (apps[app] != 0) {          ; nope, the app isn't processing anymore, but its associated script is still running
      Process, Close,% apps[app]                      ; therefore let's close the scripts process
      apps[app] := 0                                  ; ..and let's update the scripts 'running' status accordingly!
      }
   }
Updated. Script will start/close with the application. For testing, I've used this...

notepad.ahk

Code: Select all

Loop
ToolTip % "Notepad " A_Index

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

Re: Open and close Script when application is opened and closed

Post by mikeyww » 23 May 2022, 06:34

This one might work, too (most of the time).

Code: Select all

#SingleInstance Force
dir = %A_ScriptDir%
GroupAdd, go, ahk_exe notepad.exe
GroupAdd, go, ahk_exe ApplicationFrameHost.exe
WinGet, match, List, ahk_group go
Loop, %match%
 runScript(dir, match%A_Index%)
Loop {
 Sleep, 500
 last := match
 WinGet, match, List, ahk_group go
 (match > last) && runScript(dir, match1)
}

runScript(dir, hWnd) {
 WinGet, proc, ProcessName, ahk_id %hWnd%
 SoundBeep, 1500
 Run, % dir "\" SubStr(proc, 1, -4) ".ahk"
}

Post Reply

Return to “Ask for Help (v1)”