Jump to content
Sign in to follow this  
AstronomerDaz

Issue with corrupt text with ControlSend

Recommended Posts

AstronomerDaz

Hi

I am having an issue with an occasional corruption of text being sent to a control with ControlSend or Send.

The application is an astronomical ephemeris program that I am using to calculate the ephemeris for the solar system for the next full month. The report is then saved as a HTML output and then printed as a PDF file.

The basic operation of my script is:

Define Solar System Array

Run program

Outer Loop for Basic report, and then Detailed report

Open report (1st iteration is for basic report, 2nd is for detailed)

Set the report date for next month

Inner loop for solar system object

Select System Object in report

Calculate

Export as HTML

Export as PDF

End Inner Loop

End Outer Loop

Close Report

Close program

The two exports are functions.

This all seems to work, but occasionally, I will get an error saying that the path does not exist and then I have to stop the script and reset everything.

There is no pattern to the corruption - it could be on the 2nd inner loop iteration, it could be on the 8th. The corruption is always the same - the drive letter and colon are changed from "D:" to "d;".

I have attached the entire script below - if anyone has any insight I'd be grateful! I've been inserting 'sleep' at various places to see if slowing things down helps, but it doesn't seem to....

Cheers!

Global $openPath = "C:\Users\Daz\Documents\Deep-Sky Planner 6\BAS\"
Global $varDSP = "Deep-Sky Planner 6"
Global $varDET = "Define Ephemeris Time"
Global $varPrint = "Print"
Global $SolarSystem = StringSplit("Mercury,Venus,Mars,Jupiter,Saturn,Neptune,Uranus,Pluto,Moon", ",", 2)
Global $Reports = StringSplit("bas_simple.plncalc,bas_detailed.plncalc", ",", 2)
Global $iLoop
Global $Loop1

; Clear out any files that may have been run already for this month
if FileExists("D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON) & StringRight(@YEAR,2) & "*.htm") Then
   FileDelete("D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON) & StringRight(@YEAR,2) & "*.htm")
   FileDelete("D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON) & StringRight(@YEAR,2) & "*.pdf")
EndIf

; Open DSP
if not WinExists($varDSP) Then
   run("C:\Users\Daz\AppData\Local\Knightware\Deep-Sky Planner 6\Dsp6.exe")
   Sleep(5000)
Else
   WinActivate($varDSP)
EndIf

; Report Loop
For $Loop1 = 0 to 1
   
   ; Open the report
   Send("!fo")
   WinWaitActive("Open")
   Send($openPath & $Reports[$Loop1] & " {ENTER}")
   if $Loop1 = 0 Then
      send("!b")
   Else
      send("!d")
   endif
   ; Wait for the report to open
   sleep(2000)

   ; Select the 'Other' button
   send("!r")
   WinWaitActive($varDET)

   ; Click This Month, then OK, then Calculate
   ControlClick($varDET,"","TButton9")
   ControlClick($varDET,"","TButton3")

   ; Loop through the solar system objects
   for $iLoop = 0 to 8
      ControlSend($varDSP, "", "[CLASS:TListBox; INSTANCE:1]", $SolarSystem[$iLoop])
      CalculateReport()
      if $Loop1 = 0 Then
         ExportHTML($SolarSystem[$iLoop], "B")
         Sleep(1000)
         ExportPDF($SolarSystem[$iLoop], "B")
      Else
         ExportHTML($SolarSystem[$iLoop], "D")
         ExportPDF($SolarSystem[$iLoop], "D")
      EndIf
   Next
Next

; Save and Close the report
Send("!fs")
sleep(1000)
Send("!fl")

;Close DSP
Send("!fi")

Exit

Func CalculateReport ()

   ; Click the calculate button
   ControlClick($varDSP,"","TButton4")
   Sleep(2000)

EndFunc

Func ExportHTML ($objectName, $type)
   
   local $saveHTML = "D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON) & StringRight(@YEAR,2)
   send("!feh")
   WinWaitActive("Save As")
   if $type = "B" Then
      send($saveHTML & $objectName & ".htm {ENTER}")
   Else 
      send($saveHTML & $objectName & "_det.htm {ENTER}")
   EndIf

EndFunc

Func ExportPDF ($object, $type)
   
   Send("!fp")
   WinWaitActive($varPrint)
   ControlCommand($varPrint, "", "[ID:1139]", "ShowDropDown")
   ControlSend($varPrint, "", "[ID:1139]", "CutePDF Writer")
   ControlClick($varPrint, "", "[ID:1]")
   WinWaitActive("Save As")
   if $type = "B" then
      ControlSend("Save As", "", "[ID:1152]", "D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON) & StringRight(@YEAR,2) & $object & ".pdf")
   Else
      ControlSend("Save As", "", "[ID:1152]", "D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON) & StringRight(@YEAR,2) & $object & "_det.pdf")
   EndIf
   Sleep(1000)
   send("!s")
   
   ; Wait for the save to finish
   sleep(1000)
   
EndFunc

Share this post


Link to post
Share on other sites
Edano

an idea: before sending enter, read the control again and check if the string is correct, if not then repeat.

Edit: does it happen in the pdf or html part ?

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites
AstronomerDaz

Ah good idea Edano...

It can happen in either part :(

Share this post


Link to post
Share on other sites
Edano

well then try it and tell me :)

i would prefer ControlSend on both dialog windows.


[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites
AstronomerDaz

Well, a combination of several things seemed to have solved the issue:

1, The export functions now use $object ByRef

2, Implemented Edano's suggestion of checking the text, and resending

3, Changed the ControlSend to ControlSetText

Number 3 had a huge speed impact on the script as well!

I have run several times, changed the number of objects and all seems to be OK!

Share this post


Link to post
Share on other sites
AstronomerDaz

Updated code...

Global $openPath = "C:\Users\Daz\Documents\Deep-Sky Planner 6\BAS\"
Global $varDSP = "Deep-Sky Planner 6"
Global $varDET = "Define Ephemeris Time"
Global $varPrint = "Print"
Global $varSave = "Save As"
Global $varOpen = "Open"
Global $SolarSystem = StringSplit("Sun,Mercury,Venus,Mars,Jupiter,Saturn,Neptune,Uranus,Pluto,Moon", ",", 2)
Global $Reports = StringSplit("bas_simple.plncalc,bas_detailed.plncalc", ",", 2)
Global $iLoop
Global $Loop1

; Clear out any files that may have been run already for this month
if FileExists("D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON + 1) & StringRight(@YEAR,2) & "*.htm") Then
   FileDelete("D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON + 1) & StringRight(@YEAR,2) & "*.htm")
   FileDelete("D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON + 1) & StringRight(@YEAR,2) & "*.pdf")
EndIf

; Open DSP
if not WinExists($varDSP) Then
   run("C:\Users\Daz\AppData\Local\Knightware\Deep-Sky Planner 6\Dsp6.exe")
   Sleep(5000)
Else
   WinActivate($varDSP)
EndIf

; Report Loop
For $Loop1 = 0 to 1
   
   ; Open the report
   Send("!fo")
   WinWaitActive($varOpen)
   ControlSetText($varOpen, "", "[ID:1148]", $openPath & $Reports[$Loop1])
   ControlClick($varOpen, "", "[ID:1]")
   if $Loop1 = 0 Then
      send("!b")
   Else
      send("!d")
   endif
   
   ; Select the 'Other' button
   Sleep(500)
   ControlClick("", "Othe&r", "TButton6")
   WinWaitActive($varDET)

   ; Click Next Month, then OK, then Calculate
   ControlClick($varDET,"","TButton8")
   ControlClick($varDET,"","TButton3")

   ; Loop through the solar system objects
   for $iLoop = 0 to 8
      ControlSend($varDSP, "", "[CLASS:TListBox; INSTANCE:1]", $SolarSystem[$iLoop])
      CalculateReport()
      if $Loop1 = 0 Then
         ExportHTML($SolarSystem[$iLoop], "B")
         Sleep(1000)
         ExportPDF($SolarSystem[$iLoop], "B")
      Else
         ExportHTML($SolarSystem[$iLoop], "D")
         ExportPDF($SolarSystem[$iLoop], "D")
      EndIf
   Next
Next

; Save and Close the report
Send("!fs")
sleep(1000)
Send("!fl")
send("!n")

;Close DSP
Send("!fi")

Exit

Func CalculateReport ()

   ; Click the calculate button
   ControlClick($varDSP,"","TButton4")
   Sleep(2000)

EndFunc

Func ExportHTML (ByRef $object , $type)
   
   local $saveHTML = "D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON + 1) & StringRight(@YEAR,2)
   send("!feh")
   WinWaitActive($varSave)
   if $type = "B" Then
      ControlSetText($varSave, "" , "[ID:1001]", $saveHTML & $object & ".htm")
      If ControlGetText($varSave, "", "[ID:1001]") <> ($saveHTML & $object & ".htm") Then
         ConsoleWrite("HTML path does not match - rewriting" & @CRLF)
         ControlSetText($varSave, "" , "[ID:1001]", $saveHTML & $object & ".htm")
      EndIf
   Else 
      ControlSetText($varSave, "" , "[ID:1001]", $saveHTML & $object & "_det.htm")
      If ControlGetText($varSave, "", "[ID:1001]") <> ($saveHTML & $object & "_det.htm") Then
         ConsoleWrite("HTML path does not match - rewriting" & @CRLF)
         ControlSetText($varSave, "" , "[ID:1001]", $saveHTML & $object & "_det.htm")
      EndIf
   EndIf
   ControlClick($varSave, "", "[ID:1]")

EndFunc

Func ExportPDF (ByRef $object, $type)
   
   local $savePDF = "D:\Web\bedsastro\assets\reports\ephemeris\" & StringFormat("%02s",@MON + 1) & StringRight(@YEAR,2)
   Send("!fp")
   WinWaitActive($varPrint)
   ControlCommand($varPrint, "", "[ID:1139]", "ShowDropDown")
   ControlSend($varPrint, "", "[ID:1139]", "CutePDF Writer")
   ControlClick($varPrint, "", "[ID:1]")
   WinWaitActive($varSave)
   if $type = "B" then
      ControlSetText($varSave, "", "[ID:1152]", $savePDF & $object & ".pdf")
      if ControlGetText($varSave, "", "[ID:1152]") <> ($savePDF & $object & ".pdf") Then
         ConsoleWrite("PDF path does not match - rewriting" & @CRLF)
         ControlSetText($varSave, "", "[ID:1152]", $savePDF & $object & ".pdf")
      EndIf
   Else
      ControlSetText($varSave, "", "[ID:1152]", $savePDF & $object & "_det.pdf")
      if ControlGetText($varSave, "", "[ID:1152]") <> ($savePDF & $object & "_det.pdf") Then
         ConsoleWrite("PDF path does not match - rewriting" & @CRLF)
         ControlSetText($varSave, "", "[ID:1152]", $savePDF & $object & "_det.pdf")
      EndIf
   EndIf
   send("!s")
   
   ; Wait for the save to finish
   sleep(1000)
   
EndFunc

Share this post


Link to post
Share on other sites
Edano

yes that looks much much better, more professional. one improvement to adapt and implement at several (4) locations:

.

Local $in=$savePDF & $object & "_det.pdf" , $out=""
    Do
        ControlSetText($varSave, "", "[ID:1152]", $in)
        $out=ControlGetText($varSave, "", "[ID:1152]")
        If $in<>$out Then ConsoleWrite("PDF path does not match - rewriting" & @CRLF)
    Until $in=$out

.

E :)

 

or:

.

Local $in=$savePDF & $object & "_det.pdf"
    While Sleep(20)
        ControlSetText($varSave, "", "[ID:1152]", $in)
        If ControlGetText($varSave, "", "[ID:1152]")=$in Then ExitLoop
        ConsoleWrite("PDF path does not match - rewriting" & @CRLF)
    WEnd

........

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites
AstronomerDaz

Hi Edano

Yes, I had thought of that but wanted to make sure I had working syntax before I hit am infinite loop!

Thanks again for your help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×