Skip to content

Instantly share code, notes, and snippets.

@morkai
Created April 17, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morkai/1ff910502e36ecf60da2bbdff803d144 to your computer and use it in GitHub Desktop.
Save morkai/1ff910502e36ecf60da2bbdff803d144 to your computer and use it in GitHub Desktop.
#include-once
#include "_Errors.au3"
Global $cancelButtonTexts[] = ["Cancel", "Anuluj", "No", "Nie", "&Cancel", "&Anuluj", "&No", "&Nie", "OK", "Yes", "Tak", "&OK", "&Yes", "&Tak"]
Func ReadIni($section, $key, $default)
return IniRead(@ScriptDir & "\SAP.ini", $section, $key, $default)
EndFunc
Func LogDebug($message = "")
ConsoleWrite($message & @CRLF)
EndFunc
Func LogError($message, $exitCode = -1)
ConsoleWriteError($message & @CRLF)
If $exitCode <> -1 Then
Exit($exitCode)
EndIf
EndFunc
Func IsWinLocked($win)
return BitAND(WinGetState($win), 4) = 0
EndFunc
Func FindModalWin($titleRegexp = "^SAP")
return WinGetHandle("[REGEXPTITLE:" & $titleRegexp & "; CLASS:#32770]")
EndFunc
Func SetVariant($session, $variantName, $variantCreator)
If $variantName <> "" Then
$session.FindById("wnd[0]/tbar[1]/btn[17]").Press()
$session.FindById("wnd[1]/usr/txtV-LOW").Text = $variantName
$session.FindById("wnd[1]/usr/txtENAME-LOW").Text = $variantCreator
$session.FindById("wnd[1]/tbar[0]/btn[8]").Press()
EndIf
EndFunc
Func Unlock()
LogDebug("UNLOCKING")
For $i = 0 To 5
LogDebug("SEARCHING_MODAL_WINDOW")
$modalWin = FindModalWin("^(SAP|Information)")
If Not IsHWnd($modalWin) Then
LogDebug("MODAL_WINDOW_NOT_FOUND")
ExitLoop
EndIf
$modalTitle = WinGetTitle($modalWin)
LogDebug("MODAL_WINDOW_FOUND=" & $modalTitle)
If $modalTitle == "Information" Then
LogDebug("SEARCHING_INFO_BUTTON")
$buttonId = ControlGetHandle($modalWin, "", "[CLASS:Button; INSTANCE:2]")
If IsHWnd($buttonId) Then
LogDebug("INFO_BUTTON_FOUND=" & ControlGetText($modalWin, "", $buttonId))
ControlClick($modalWin, "", $buttonId)
Sleep(500)
ContinueLoop
EndIf
EndIf
LogDebug("SEARCHING_CANCEL_BUTTON")
$buttonFound = False
For $ii = 0 To (UBound($cancelButtonTexts) - 1)
$buttonText = $cancelButtonTexts[$ii]
$buttonId = ControlGetHandle($modalWin, "", "[CLASS:Button; TEXT:" & $buttonText & "]")
If Not IsHWnd($buttonId) Then ContinueLoop
$buttonFound = True
LogDebug("CANCEL_BUTTON_FOUND=" & ControlGetText($modalWin, "", $buttonId))
ControlClick($modalWin, "", $buttonId)
Sleep(500)
ExitLoop
Next
If Not $buttonFound Then
LogDebug("CANCEL_BUTTON_NOT_FOUND")
ExitLoop
EndIf
Next
EndFunc
#include-once
Global $ERR_NO_LOGON_WINDOW = 1
Global $ERR_NO_FRONTEND_WINDOW = 2
Global $ERR_NO_ROT_WRAPPER = 3
Global $ERR_NO_SAPGUI = 4
Global $ERR_NO_SCRIPTING_ENGINE = 5
Global $ERR_NO_CONNECTION = 4
Global $ERR_NO_SESSION = 7
Global $ERR_NO_FREE_SESSIONS = 8
Global $ERR_NO_CONNECTTION = 9
Global $ERR_LOGON_WINDOW_LOCKED = 10
Global $ERR_INVALID_CREDENTIALS = 11
Global $ERR_NO_MAIN_WINDOW = 12
Global $ERR_MISSING_ARGUMENTS = 13
Global $ERR_NON_EMPTY_SBAR = 14
#include <WinAPI.au3>
#include "_Common.au3"
$SAP_LOGON_EXE = ReadIni("Logon", "SapLogonExe", "")
$SAP_CONNECTION_FILTER = ReadIni("Logon", "ConnectionFilter", "PUBLIC")
$SAP_LOGON_CLIENT = ReadIni("Logon", "Client", "000")
$SAP_LOGON_USER = ReadIni("Logon", "User", "test")
$SAP_LOGON_PASSWORD = ReadIni("Logon", "Password", "test")
$SAP_LOGON_LANGUAGE = ReadIni("Logon", "Language", "")
$SAP_TIMEOUT_LOGON = Number(ReadIni("Logon", "SapLogonWindowTimeout", "10"))
$SAP_TIMEOUT_FRONTEND = Number(ReadIni("Logon", "SapFrontendSessionWindowTimeout", "10"))
$SAP_TIMEOUT_SESSION = Number(ReadIni( "Logon", "SapFreeSessionCreationTimeout", "10")) * 1000
$SAP_LOGON_REGEX = ReadIni("Logon", "SapLogonWindowTitleRegex", "^SAP Logon [0-9]+$")
$SAP_FREE_SESSION_REGEX = ReadIni("Logon", "SapFreeSessionWindowTitleRegex", "^SAP Easy Access.*")
$SAP_LOGIN_FORM_WINDOW_TITLE = "[REGEXPTITLE:^SAP$; CLASS:SAP_FRONTEND_SESSION]"
$wrapper = Null
$sapgui = Null
$application = Null
Global $connection = Null
Global $session = Null
Func TryToLogIn()
LogDebug("LOGGING_IN")
$session.findById("wnd[0]/usr/txtRSYST-MANDT").text = $SAP_LOGON_CLIENT
$session.findById("wnd[0]/usr/txtRSYST-BNAME").text = $SAP_LOGON_USER
$session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = $SAP_LOGON_PASSWORD
$session.findById("wnd[0]/usr/txtRSYST-LANGU").text = $SAP_LOGON_LANGUAGE
$session.findById("wnd[0]/tbar[0]/btn[0]").press()
If $session.findById("wnd[0]/sbar").Text <> "" Then
LogDebug('"' & $session.findById("wnd[0]/sbar").Text & '"')
LogError("ERR_INVALID_CREDENTIALS", $ERR_INVALID_CREDENTIALS)
EndIf
LogDebug("LOGGED_IN")
EndFunc
Func CloseSession()
LogDebug("CLOSING_SESSION")
If IsObj($session) Then
$session.TestToolMode = 0
$session.UnlockSessionUI()
$connection.CloseSession($session.Id)
EndIf
EndFunc
Func FindFreeSession()
For $freeSessionIdx = 1 To ($connection.Children.Length - 1)
$freeSessionCandidate = $connection.Children($freeSessionIdx)
If $freeSessionCandidate.Busy Then
ContinueLoop
EndIf
$window = $freeSessionCandidate.findById("wnd[0]")
If IsObj($window) And StringRegExp($window.Text, $SAP_FREE_SESSION_REGEX) And Not IsWinLocked($window.Handle) Then
Return $freeSessionCandidate
EndIf
Next
Return Null
EndFunc
$wrapper = ObjCreate("SapROTWr.SAPROTWrapper")
LogDebug("CHECKING_WRAPPER_1")
If IsObj($wrapper) Then
$sapgui = $wrapper.GetROTEntry("SAPGUI")
LogDebug("CHECKING_SAPGUI_1")
If IsObj($sapgui) Then
$application = $sapgui.GetScriptingEngine()
LogDebug("CHECKING_SCRIPTING_ENGINE_1")
If IsObj($application) Then
$application.AllowSystemMessages = False
$application.HistoryEnabled = False
$connection = $application.Children(0)
LogDebug("CHECKING_CONNECTION_1")
If IsObj($connection) Then
$session = $connection.Children(0)
EndIf
EndIf
EndIf
EndIf
Unlock()
LogDebug("CHECKING_SESSION_1")
If Not IsObj($session) Then
LogDebug("CHECKING_LOGON_WINDOW")
If Not WinExists("[REGEXPTITLE:" & $SAP_LOGON_REGEX & "]") Then
If $SAP_LOGON_EXE = "" Then
$SAP_LOGON_EXE = EnvGet('ProgramFiles(x86)')
If $SAP_LOGON_EXE = "" Then
$SAP_LOGON_EXE = @ProgramFilesDir
EndIf
$SAP_LOGON_EXE = $SAP_LOGON_EXE & "\SAP\FrontEnd\SAPgui\saplogon.exe"
EndIf
LogDebug("RUNNING_SAPLOGON=" & $SAP_LOGON_EXE)
Run($SAP_LOGON_EXE)
WinWait("[REGEXPTITLE:" & $SAP_LOGON_REGEX & "]", "", $SAP_TIMEOUT_LOGON)
If Not WinExists("[REGEXPTITLE:" & $SAP_LOGON_REGEX & "]") Then
LogError("ERR_NO_LOGON_WINDOW", $ERR_NO_LOGON_WINDOW)
EndIf
EndIf
$sapLogonWin = WinGetHandle("[REGEXPTITLE:" & $SAP_LOGON_REGEX & "]")
If IsWinLocked($sapLogonWin) Then
LogError("ERR_LOGON_WINDOW_LOCKED", $ERR_LOGON_WINDOW_LOCKED)
EndIf
LogDebug("CHECKING_SAP_FRONTEND_SESSION_1")
If Not WinExists($SAP_LOGIN_FORM_WINDOW_TITLE) Then
LogDebug("FILTERING_CONNECTIONS")
ControlSetText($sapLogonWin, "", "Edit1", "")
ControlSend($sapLogonWin, "", "Edit1", $SAP_CONNECTION_FILTER)
Sleep(666)
LogDebug("SELECTING_CONNECTION")
ControlClick($sapLogonWin, "", "SysListView321", "left", 1, 50, 30)
Sleep(333)
LogDebug("CONNECTING")
ControlClick($sapLogonWin, "", "Button1")
WinWait($SAP_LOGIN_FORM_WINDOW_TITLE, "", $SAP_TIMEOUT_FRONTEND / 10)
If IsWinLocked($sapLogonWin) Then
ControlClick("[REGEXPTITLE:^SAP Logon$; CLASS:#32770]", "", "Button3")
LogError("ERR_NO_CONNECTTION", $ERR_NO_CONNECTTION)
EndIf
WinWait("[REGEXPTITLE:^SAP$; CLASS:SAP_FRONTEND_SESSION]", "", $SAP_TIMEOUT_FRONTEND - ($SAP_TIMEOUT_FRONTEND / 10))
EndIf
LogDebug("CHECKING_SAP_FRONTEND_SESSION_2")
If Not WinExists($SAP_LOGIN_FORM_WINDOW_TITLE) Then
If IsWinLocked($sapLogonWin) Then
ControlClick("[REGEXPTITLE:^SAP Logon$; CLASS:#32770]", "", "Button3")
LogError("ERR_NO_CONNECTTION", $ERR_NO_CONNECTTION)
EndIf
LogError("ERR_NO_FRONTEND_WINDOW", $ERR_NO_FRONTEND_WINDOW)
EndIf
LogDebug("CHECKING_WRAPPER_2")
$wrapper = ObjCreate("SapROTWr.SAPROTWrapper")
If Not IsObj($wrapper) Then
LogError("ERR_NO_ROT_WRAPPER", $ERR_NO_ROT_WRAPPER)
EndIf
LogDebug("CHECKING_SAPGUI_2")
$sapgui = $wrapper.GetROTEntry("SAPGUI")
If Not IsObj($sapgui) Then
LogError("ERR_NO_SAPGUI", $ERR_NO_SAPGUI)
EndIf
LogDebug("CHECKING_SCRIPTING_ENGINE_2")
$application = $sapgui.GetScriptingEngine()
If Not IsObj($application) Then
LogError("ERR_NO_SCRIPTING_ENGINE", $ERR_NO_SCRIPTING_ENGINE)
EndIf
$application.AllowSystemMessages = False
$application.HistoryEnabled = False
LogDebug("CHECKING_CONNECTION_2")
$connection = $application.Children(0)
If Not IsObj($connection) Then
LogError("ERR_NO_CONNECTION", $ERR_NO_CONNECTION)
EndIf
LogDebug("CHECKING_SESSION_2")
$session = $connection.Children(0)
If Not IsObj($session) Then
LogError("ERR_NO_SESSION", $ERR_NO_SESSION)
EndIf
EndIf
$session.TestToolMode = 1
LogDebug("CHECKING_SAP_FRONTEND_SESSION_3")
If WinExists($SAP_LOGIN_FORM_WINDOW_TITLE) Then
TryToLogIn()
EndIf
Unlock()
LogDebug("SEARCHING_FREE_SESSION")
$freeSession = FindFreeSession()
If Not IsObj($freeSession) Then
LogDebug("NO_FREE_SESSION")
If $connection.Children.Length = 6 Then
LogError("ERR_NO_FREE_SESSIONS", $ERR_NO_FREE_SESSIONS)
EndIf
LogDebug("CREATING_FREE_SESSION")
$session.CreateSession()
$checks = 0
$count = 0
While $checks < $SAP_TIMEOUT_SESSION
LogDebug("CHECKING_FREE_SESSION_" & $checks)
$windows = WinList("[REGEXPTITLE:" & $SAP_FREE_SESSION_REGEX & "; CLASS:SAP_FRONTEND_SESSION]")
$count = $windows[0][0]
if $count > 1 Then
ExitLoop
EndIf
$checks = $checks + 250
Sleep(250)
WEnd
LogDebug("SEARCHING_NEW_FREE_SESSION")
For $i = 1 To 6
$freeSession = FindFreeSession()
If IsObj($freeSession) Then ExitLoop
LogDebug("FREE_SESSION_HIJACKED")
Sleep(2000)
Next
If Not IsObj($freeSession) Then
LogError("ERR_NO_FREE_SESSIONS", $ERR_NO_FREE_SESSIONS)
EndIf
LogDebug("CREATED_FREE_SESSION")
Else
LogDebug("FOUND_FREE_SESSION")
EndIf
$session = $freeSession
$session.TestToolMode = 1
$session.LockSessionUI()
#Region
#AutoIt3Wrapper_Change2CUI=y
#EndRegion
#include <StringConstants.au3>
#include "_Common.au3"
$outputPath = ReadIni("T_MD04", "OutputPath", ReadIni("Transactions", "OutputPath", "C:\SAP\Output"))
$outputFile = ReadIni("T_MD04", "OutputFile", "T_MD04.txt")
$codePage = ReadIni("T_MD04", "OutputEncoding", ReadIni("Transactions", "OutputEncoding", "4110"))
$plant = ReadIni("T_MD04", "Plant", "")
$material = ""
If $CmdLine[0] = 1 And $CmdLine[1] = "--help" Then
LogDebug("T_MD04.exe <arguments>")
LogDebug()
LogDebug("Arguments:")
LogDebug(" --output-path")
LogDebug(" --output-file")
LogDebug(" --code-page")
LogDebug(" --plant")
LogDebug(" --material")
Exit(0)
EndIf
If $CmdLine[0] > 0 And Mod($CmdLine[0], 2) = 0 Then
For $i = 1 To ($CmdLine[0] - 1) Step 2
$k = $CmdLine[$i]
$v = $CmdLine[$i + 1]
Switch $k
Case "--output-path"
$outputPath = $v
Case "--output-file"
$outputFile = $v
Case "--code-page"
$codePage = $v
Case "--plant"
$plant = $v
Case "--material"
$material = $v
EndSwitch
Next
EndIf
LogDebug("T_MD04")
LogDebug("--output-path=" & $outputPath)
LogDebug("--output-file=" & $outputFile)
LogDebug("--code-page=" & $codePage)
LogDebug("--plant=" & $plant)
LogDebug("--material=" & $material)
#include "_Logon.au3"
LogDebug("STARTING_TRANSATION")
; Start transaction
$session.StartTransaction("MD04")
; Set material 12NC
$session.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-MATNR").Text = $material
; Set plant
$session.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-WERKS").Text = $plant
; Execute
$session.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-WERKS").SetFocus()
$session.FindById("wnd[0]").sendVKey(0)
; Expand material
$session.FindById("wnd[0]/usr/btnBUTTON_GROKO").Press()
; Read planist
$planist = $session.FindById("wnd[0]/usr/tabsTABTC/tabpTB01/ssubINCLUDE7XX:SAPLM61K:0101/txtT024D-DSNAM").Text
; Back to main screen
$session.FindById("wnd[0]/tbar[0]/btn[15]").Press()
$session.FindById("wnd[0]/tbar[0]/btn[15]").Press()
LogDebug("ENDING_TRANSACTION")
CloseSession()
LogDebug('planist="' & $planist & '"')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment