Search code examples
pythonaspen

How to close a pop-up window from other application with python script


I am working in automate Aspen Plus simulation and post-processing of the results with Python. In order to do scale-up analysis, sensitivity studies and solving optimization problems; I have to iterate several times running the aspen simulation.

This way, I am using win32com.client to manage it. It works great, but sometimes aspen shows a pop-up windows telling that all licenses are in use, interrupting the program flow:

issue

If I close it manually the program continues working. So I am thinking in writing a script to automate this, but I do not have any clue of how to do it.

I tried to kill, terminate, send a signal, to the process. But nothing works because kill the AspenPlus.exe process, stops the program.


Solution

  • Here is some sample powershell code you can use to check if the Aspen process is running and if the window is open then to close it by sending the appropriate key via the sendKey function

    $isAspenOpen = Get-Process Aspen*
    if($isAspenOpen = $null){
        # Aspen is already closed run code here:
        }
    else {
         $isAspenOpen = Get-Process AspenPlus*
    
         # while loop makes sure all Aspen windows are closed before moving on to other code:
             while($isAspenOpen -ne $null){
                Get-Process aspen* | ForEach-Object {$_.CloseMainWindow() | Out-Null }
                sleep 5
                If(($isAspenOpen = Get-Process aspen*) -ne $null){
                Write-Host "Aspen is Open.......Closing Aspen"
                    $wshell = new-object -com wscript.shell
                    $wshell.AppActivate("Aspen Plus")
                    $wshell.Sendkeys("%(Esc)")
                $isAspenOpen = Get-Process Aspen*
                }
            }
            #Aspen has been closed run code here:
        }