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 ?
Open and close Script when application is opened and closed
-
- Posts: 1
- Joined: 23 May 2022, 03:45
Re: Open and close Script when application is opened and closed
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
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
Re: Open and close Script when application is opened and closed
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.
Updated. Script will start/close with the application. For testing, I've used this...
notepad.ahk
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!
}
}
notepad.ahk
Code: Select all
Loop
ToolTip % "Notepad " A_Index
Re: Open and close Script when application is opened and closed
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"
}
Who is online
Users browsing this forum: Google [Bot], JKJadan and 267 guests