SAP GUI Scripting: Irritating Rotation
If you connect a SAP GUI for Windows session via the SAP GUI Scripting API a little barber pole begins to rotate on the lower right side of the window. This signalizes that this session is busy with a SAP GUI script.
It begins to rotate with the execution of the command
$session = Get-Property $connection "Children" @(0)
and it stops to rotate with the exectuion of the command
Free-Object $session
Here my test script:
#-Begin-----------------------------------------------------------------
#-Includes------------------------------------------------------------
."$PSScriptRoot\COM.ps1"
#-Main----------------------------------------------------------------
$SapGuiAuto = Get-Object( , "SAPGUI")
If ($SapGuiAuto -isnot [__ComObject]) {
Exit
}
$application = Invoke-Method $SapGuiAuto "GetScriptingEngine"
If ($application -isnot [__ComObject]) {
Free-Object $SapGuiAuto
Exit
}
$connection = Get-Property $application "Children" @(0)
If ($connection -eq $Null) {
Free-Object $application
Free-Object $SapGuiAuto
Exit
}
$session = Get-Property $connection "Children" @(0)
If ($session -eq $Null) {
Free-Object $connection
Free-Object $application
Free-Object $SapGuiAuto
Exit
}
Throw [System.ApplicationException] "Error"
Free-Object $session
Free-Object $connection
Free-Object $application
Free-Object $SapGuiAuto
#-Error routine-------------------------------------------------------
Trap {
Free-Object $session
Free-Object $connection
Free-Object $application
Free-Object $SapGuiAuto
Exit
}
#-End-------------------------------------------------------------------
If you execute in the same session an ABAP report which calls the method IS_SCRIPTING_ACTIVE from the class CL_GUI_FRONTEND_SERVICES it delivers 1 if the barber pole rotates and 0 if doesn’t.
Here my test report:
"-Begin-----------------------------------------------------------------
REPORT zsapguiscript.
DATA:
lg_result TYPE i
.
lg_result = cl_gui_frontend_services=>is_scripting_active( ).
WRITE: lg_result.
"-End-------------------------------------------------------------------
So far so well, all as expected.
And now I throw an exception and the trap routine doesn’t frees the session object. Now the barber pole rotates for the rest of the window life. Also delivers the IS_SCRIPTING_ACTIVE method the result that scripting is active – but it is definitely not. That is very irritating. It seems that the method IS_SCRIPTING_ACTIVE, which calls IsScriptingActive from the library sapfewin.ocx, only check if the barber pole is active or not.
It is very important to free the session object, on the one hand if the script terminates normal and on the other hand if the script terminates via an error. In this case the barber pole stops the rotating immediately.
Hi Stefan,
I'm having the same issues when doing this with Ruby (using win32ole). The barber pole keeps rotating after the script was finished. I've tried setting the session object to nil, but no luck.
Does the above work for you? I can't find the Ruby alternative.
Best, Sebastjan
Hello Sebastjan,
thanks for your reply.
I use the method
to release the session object. It could be possible that a set to Null doesn't destroy the object and therewith the barber pole rotates further more.
Please try it with a release method in the context of Ruby if possible and let us know your results.
Best regards
Stefan
Hello Stefan,
unfortunately inside Ruby the garbage collector runs on its own. After an object, in this case session, I guess, is no longer in use (not referenced anywhere) it gets garbage collected. This should take care of the barbers pole, but it doesn't.
I believe the excel VBA takes care of this by Exit Sub command as this issue is not present when scripting via Excel VBA.
For example, I'm setting the objects below to nil in the reverse order as they were created and the barbers pole is still there.
What side effects can these have, if any?
Hello Sebastjan,
I don't know Ruby, but maybe you could use the method ole_free.
Best regards
Stefan
Hello again, Stefan,
I've tried a few things with ole_free, none of them worked though.
The scripting icon is still running.
It doesn't look promising.
Thank you for your efforts, as always.
regards,
Sebastjan