%AppFile% should hold the name of the application's executable. It is then sent the WM_CLOSE message. If it doesn't react in 5 seconds (10 tries) the task gets killed.
TryClose: Process Exist, %AppFile% If ErrorLevel ProgPID = %ErrorLevel% Else Goto CloseDone WinClose ahk_pid %ProgPID%, , 0.5 If Counter >= 10 Process Close, %ProgPID% Counter += 1 Goto TryClose CloseDone: