Automatically Stop / Pauze a script after software crashes.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Automatically Stop / Pauze a script after software crashes.

12 Dec 2018, 10:38

Hi I have a question.

At work I work with SAP Business one which sometimes out of the blue crashes.

When SAP crashes the scripts keeps sending keys and cllicking away which sometimes
causes it to do unwanted actions like clicking an email and deleting it for instance :crazy:

Is there a way to automatically detect if SAP is still active and then pauze or stop the
active ahk script ?

I know I can create a hotkey to do an ExitApp but sometimes I'm not at my computer when
it occurs.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Automatically Stop / Pauze a script after software crashes.

12 Dec 2018, 10:49

This sound to me, as if you are using a script which sends keystrokes/mouseclicks into the "wild".
Use a different script, or post your script, and someone can at least have a look.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Automatically Stop / Pauze a script after software crashes.

13 Dec 2018, 02:34

wolf_II wrote:
12 Dec 2018, 10:49
This sound to me, as if you are using a script which sends keystrokes/mouseclicks into the "wild".
Use a different script, or post your script, and someone can at least have a look.
I understand what you're saying wolf_II, but I think in this case posting the script wouldn't help
that much.

The thing with SAP Business one is, for instance the button controls all have the same name, like ClassNN: TMEditTextClass1
So I am resorting to imagesearch to check if the correct screen is open.

After that I have the script press certain buttons, like {F11} which opens the picklist screen. I use imagesearch to check
if the picklist screen is actually open, if it doesn't it presses {F11} again, using loop.

If in that exact moment SAP Business One crashes, the picklist screen is never found so it keeps pressing {F11}, so
when I have a browser window open it keeps minimizing and maximizing the screen for instance.

But here's one of the scripts that I have running which sometimes causes issues:

Code: Select all

;17/10 Optie toegevoegd die controleert of alle regels gepickt zijn.


;-----------------------------------
#NoEnv
#Warn
#SingleInstance Force
SetTitleMatchMode, 2
SetBatchLines, -1
SetDefaultMouseSpeed, 0 ; test 01-05-2017

SendMode, Input
;SetWorkingDir, D:\AHK Scripts\Images\SRT afmelden
SetWorkingDir, %A_ScriptDir% ;TEST 16/05/2017

screenshot1=tabblad logistiek.png
screenshot2=verzendwijze PostNL.png
screenshot3=verzendwijze brief.png
screenshot4=verzendwijze HSL.png
screenshot5=gepickt.png
screenshot6=picklijst.png
screenshot7=factuur.png

FullScreen()
{
	Sleep, 1000
	ImageSearch, ZoekX, ZoekY, 0, 0, A_ScreenHeight, A_ScreenWidth, fullscreen.png
	If ErrorLevel = 0
	{
		MouseClick, Left, %ZoekX%, %ZoekY%, 1, 1
		SLEEP, 1000

	}
}
TabbladLogistiek()
{
	global
	Loop
	{
		ImageSearch, TabX, TabY, 0, 0, A_Screenwidth, A_Screenheight, *30 %screenshot1% ; zoek tabblad logistiek
		If ErrorLevel = 0 ; Gevonden ? 
		{
			Sleep, 1000 ; nieuw 30/08
			MouseClick, Left, %TabX%, %TabY%
			ToolTip, gevonden !
			Break
		}
		Else
		{	
			ToolTip, niet gevonden !
			Continue
		}
	}	
	ToolTip
}

Verzendwijze()
{
	global
	Loop
	{
		ImageSearch, PostNLX, PostNLY, 0, 0, A_Screenwidth, A_Screenheight, *30 %screenshot2% ; zoek verzendwijze POSTNL
		If ErrorLevel = 0 ; Gevonden ?
		{	
			MouseClick, Left, %PostNLX%, %PostNLY%
			Sleep, 1000
			Break
		}
		Else
		{
			ImageSearch, BriefX, BriefY, 0, 0, A_Screenwidth, A_Screenheight, *30 %screenshot3% ; zoek verzendwijze BRIEF			
			If ErrorLevel = 0
			{	
				MouseClick, Left, %BriefX%, %BriefY%
				Sleep, 1000
				Break
			}
		Continue
		}	
	}
}
Vervoerder()
{
	global
	Loop
	{
		If v_colli = b
		{
			ImageSearch, BriefX, BriefY, 0, 0, A_Screenwidth, A_Screenheight, *30 %screenshot3% ; zoek verzendwijze BRIEF
			MouseClick, Left, %BriefX%, %BriefY%
			Send {tab}{enter}
			break
		}
			
		If v_colli = p
		{
			ImageSearch, PalletX, PalletY, 0, 0, A_Screenwidth, A_Screenheight, *30 %screenshot4% ; zoek verzendwijze Huisman Transport
			MouseClick, Left, %PalletX%, %PalletY%
			Send {tab}{enter}
			break
		}
		Break ; als het geen brief of palletzending betreft, bovenstaande overslaan
	}
}
SluitTSI() ; Als TSI Script actief is, sluit deze dan af.
{
	IfWinExist, PickID TSI:
	{
		WinActivate, PickID TSI:
		Send, !{F4}{Up}
		Sleep, 1000
		Send, {Enter}
	}
}
AllePickenKnop()
{
	global
	Loop
	{
		;PixelSearch, AllePickenX, AllePickenY, 1023,885,A_ScreenWidth,A_ScreenHeight, 0x399EF7, 3, Fast ; Zoek (Alle Picken) VERSIE 15/05/2018
		PixelSearch, AllePickenX, AllePickenY, 1023,885,A_ScreenWidth,A_ScreenHeight, 0x399EF7, 4, Fast ; Zoek (Alle Picken)
		if ErrorLevel
		{
			Progress, B fs11 zh0 ctWhite cwNavy Y350, Zoek Alle Picken knop
			Sleep 1000
			continue
		}
		else
		{
			Sleep, 750
			Send !p ; alle picken
			break
		}
	}
}
ActualiserenKnop()
{
	Loop
	{
		PixelSearch, ActualiserenX, ActualiserenY, 33,880,A_ScreenHeight,A_ScreenWidth, 0x31B2D6, 3, Fast ; zoek actualiseren knop (NIEUW 19/04)
		if ErrorLevel
		{
			Progress, B fs11 zh0 ctWhite cwNavy Y350, Zoek Actualiseren knop ;09-06
			Sleep 1000
			continue
		}
		else
		{
			Send, {enter} ; actualiseren
			break
		}
	}
}
CreërenKnop()
{ 	
	global ; 20 /10
	Loop
	{
		PixelSearch, CreerenX, CreerenY, 941,893,A_ScreenHeight,A_ScreenWidth, 0x392429, 10, Fast ; zoek Creëren knop
		if ErrorLevel
		{
			Progress, B fs11 zh0 ctWhite cwNavy Y350, Zoek Creëeren knop
			Sleep 1000
			continue
		}
		else
		{
			MouseClick, Left, %CreerenX%, %CreerenY%,, 5 ; klik op creëren
			Sleep, 150
			MouseClick, Left, 0, -69,5, 10,, R ; klik op 5x handmatige faktuur
			MouseClick, Left, 
			break
		}
	}	
}
Factuurscherm()
{
	global
	Loop
	{
		imagesearch, FactuurX, FactuurY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *30 %screenshot7% ; zoek het faktuurscherm
		if ErrorLevel = 1
		{
			Progress, B fs11 zh0 ctWhite cwNavy Y350, wacht op factuur scherm.
			Sleep, 3000 
			continue
		}
		else
		{
			Progress, Off
			Sleep, 1000
			Loop ; check of factuurscherm actief is
			{
				FullScreen()
				MouseClick, Left, %PosX%, %PosY%,, 0 ; click op colli veld in factuur scherm 
				MouseGetPos, MouseX, MouseY
				PixelGetColor, color, %MouseX%, %MouseY%
				if (color = 0x9CF3FF)
				{
					Progress, B fs11 zh0 ctWhite cwNavy Y350, factuur scherm gevonden
					if ((v_colli = "p") or (v_colli = "b"))		
					{
						TabbladLogistiek()
						Verzendwijze()
						Vervoerder()
						v_colli = %v_pallet%
						Sleep, 1000
						MouseClick, Left, %MouseX%, %MouseY%
						Sleep, 1000
					}
					Sleep, 100
					Send, %v_colli%
					v_colli =
					PosY := 126
					break
				}
				else
				{
					continue
				}
			}
			Loop
			{
				ImageSearch, PicklijstX, PicklijstY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *30 %screenshot6% ; zoek het picklijst scherm
				if ErrorLevel = 1
				{
					Progress, Off
					Sleep, 100 ; 19/04/2017
					Send, {enter}
					continue
				}
				else
				{
					Sleep, 500
					send ^f{tab} ; test 08-06
					Sleep, 500
					break
				}	
			}	
			break
		}
	}
}
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen

PosX := A_ScreenWidth - 30 ; positie van het colli veld 
PosY := 126 			   ; hoogte van het colli veld (120 voor colli 135 voor pallet) 

#Pause::Pause
*#ESC::ExitApp
*#Q::Reload
#A::

SluitTSI()

WinWait, SAP Business One 9.2
WinActivate
Sleep, 500

IfWinExist, , Picklijst
{
	Send, !na ; alle vensters sluiten
	Sleep, 300
}
Loop
{
	WinActivate, - SAP Business One
	Gui +LastFound +OwnDialogs +AlwaysOnTop
	InputBox, PickID, PickID SRT:, Voer het picklistnummer in., , 200, 150, , , , , 0
	
	If ErrorLevel
	{
		MsgBox,0, Cancel, Je hebt op CANCEL gedrukt. Herstarten met Windows-toets+A, 2
		Send, !na ; alle vensters sluiten
		return
	}
	Else
	{
		If (StrLen(PickID) < 5) ; picknummer kan nooit korter dan 5 cijfers zijn
		{
			MsgBox, 262192, Waarschuwing !, Picklijstnummers zijn minimaal 5 cijferig !
			continue
		}
		InputBox, v_colli, Picklijst %PickID%, %PickID% -> Aantal colli: `n(p = pallet - b = brief), , 200, 150,,,,,1
		If ErrorLevel
		{	
			MsgBox,0, Cancel, Je hebt op CANCEL gedrukt. Herstarten met Windows-toets+A, 2
			Send, !na ; alle vensters sluiten
			return
		}
		If (StrLen(v_colli) > 2)
		{
			MsgBox, 262192, Waarschuwing !, Zeg, zijn dat niet een beetje veel doosjes ?
			continue
		}
		else
		{
			if v_colli = p
			{
				PosY := 140 ; was 136
				InputBox, v_pallet, Picklijst %PickID%, %PickID% -> Aantal pallets:, , 200, 150,,,,,1
			}
		}
		Loop
		{
			ImageSearch, PicklijstX, PicklijstY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, *30 %screenshot6% ; zoek het picklijst scherm 13/12: op *30 gezet (was 10)
			if ErrorLevel = 1
			{
				WinActivate, SAP Business One
				Progress, B fs11 zh0 ctWhite cwNavy Y350, Wacht op picklijst scherm
				Send, {F11}
				Sleep, 2000
				Continue
			}
			else
			{
				FullScreen() ; checken of picklijstscherm op FullScreen staat.
				PixelSearch, PicklijstX, PicklijstY, 157,107,303,199, 0x9CF3FF, 3, Fast ; kijk of picklijstscherm actief is, en picknummer leeg (geel)
				MouseClick, Left, %PicklijstX%, %PicklijstY%,, 
				Sleep, 100
				if ErrorLevel ; Niet gevonden ? zoek opnieuw. 
				{
					;MsgBox, Picklijst Gevonden op positie %PicklijstX%`, %PicklijstY%
					WinActivate, SAP Business One
					Sleep, 500
					;send ^f{tab} ; stuur ctrl-f tab om een nieuwe picklijst te activeren
					send, {enter} ;;;; TEST 19-09-2017
				}
				else
				{
					Progress, B fs11 zh0 ctWhite cwNavy Y350, Picklijstscherm gevonden.
					Send, %PickID%{enter}
					Sleep, 1000 ; vertraging omdat anders op Alles verwijderen wordt geklikt.
					Progress, Off
					AllePickenKnop()	; Wacht op Alle Picken knop
					ActualiserenKnop() 	; Wacht tot Actualiseren actief wordt
					CreërenKnop() 		; Klik op de Creëren knop
					Factuurscherm()	; Wacht op het factuurscherm
					break
				}
			}
		}
	}
	Progress, Off
	}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Automatically Stop / Pauze a script after software crashes.

13 Dec 2018, 05:39

:think:
Maybe try to ControlSend instead of Send ...

Code: Select all

	...
	WinActivate, SAP Business One
	Progress, B fs11 zh0 ctWhite cwNavy Y350, Wacht op picklijst scherm
	ControlSend, {F11} ; <-- change here
	...
I hope that helps, but I can't test.
At that point in the code, WinActivate has already set the last found window, and ControlSend will need no WinTitle. Fingers crossed.

My hope is, the Send {F11} command will behave more robustly, and no email gets deleted. Good luck.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Sumsaam and 104 guests