Function collect PDF's with PDFTK crashes once in a while

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mrdigitalis
Posts: 67
Joined: 23 May 2022, 01:17

Function collect PDF's with PDFTK crashes once in a while

Post by mrdigitalis » 03 Jul 2022, 14:38

Dear FormMembers,

I have a litiitle script to collect PDFs and merge it to one PDF file. Once in a while PDFTK crashes and I get the message:

PDFTK has stopped working. And then I have two choices one is close the program and there is another option, which I can't remember right now.

My question is, is it possible to catch an exit-code and with the result of that code to restart over the funtion again, until there is a good result without failure.

How do I do that? Thanks in advance!

Code: Select all

collectPDFs()
{
  Loop, Files, M:\HNG\_Shared\Breng-Planning\muurkranten\Muurkranten + Opkomststaten 308 en 151\AutoModus\*.*
  {
    FileGetTime, OutputVar, M:\HNG\_Shared\Breng-Planning\muurkranten\Muurkranten + Opkomststaten 308 en 151\AutoModus\*.*, M 
    FormatTime, DateCreated, %A_LoopFileTimeCreated%, dd-M-yyyy
    FormatTime, CurrentDateTime,, dd-M-yyyy
    
    if (CurrentDateTime = DateCreated)
    {
      PDF_IN =   "%A_LoopFileFullPath%" 
      PDF_IN_Total .= PDF_IN A_Space
    }
  }
  PDFKT_Path := "H:\Settings\Desktop\AutoHotkey_1.1.33.10\PDFTK\App\pdftkbuilder\pdftk.exe"
  DateTomorrow := Add_Days_to_CurrentDate_ShortDate(1)
  NewFile := "M:\HNG\_Shared\Breng-Planning\muurkranten\Muurkranten + Opkomststaten 308 en 151\AutoModus\" . "_Alle-PDFs-Samen-" . DateTomorrow . "_.pdf"
  RunWait, %comspec% /c ""%PDFKT_Path%" %PDF_IN_Total% "output" "%NewFile%"", , hide
}

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Function collect PDF's with PDFTK crashes once in a while

Post by mikeyww » 03 Jul 2022, 14:52

Ideas:
1. Look for the error window that you have described. WinWait, WinExist, etc.
2. UseErrorLevel
3. Use the ErrorLevel from RunWait.
If the launch succeeds, RunWait sets ErrorLevel to the program's exit code.
4. Check for proper existence of the output file.

Code: Select all

test()
MsgBox, 64, Done, Done!

test() {
 Static app := A_ScriptDir "\test.cmd"
 Loop {
  SoundBeep, 1500
  RunWait, %app%,, Hide
  MsgBox, 64, ErrorLevel, % err := ErrorLevel
 } Until !err
}
If the program crashes, you may want to alert the developer.

I think that your FileGetTime command would use a single file from your loop, rather than using a mask like *.*, because FileGetTime works for one file or one directory, not a set.

A parameter such as output would often be followed by a space, but I did not check the PDFTK syntax.

https://superuser.com/questions/773343/validate-verify-pdf-files-integrity

https://stackoverflow.com/questions/3448519/execution-of-pdftk-from-php-script-is-failing-with-error-code-11

mrdigitalis
Posts: 67
Joined: 23 May 2022, 01:17

Re: Function collect PDF's with PDFTK crashes once in a while

Post by mrdigitalis » 04 Jul 2022, 14:02

@mikeyww

Thank for your answer. I am quite new to Autohotkey, and I do not know exactly what to with your big answer.

So I can use WinWait to detect if the error window shows up and then use ErrorLevel to determin the the
specific Error that occurs and then let the program decide what to do with the error.

I am sorry for my English.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Function collect PDF's with PDFTK crashes once in a while

Post by mikeyww » 04 Jul 2022, 14:14

Yes. As a first step, I would check the ErrorLevel after RunWait. Test it to see whether the ErrorLevel is set when the error occurs. If so, then that is your solution. If not, then you can try WinWait or WinExist after your RunWait or Run. My script shows an example of how to check the ErrorLevel.

mrdigitalis
Posts: 67
Joined: 23 May 2022, 01:17

Re: Function collect PDF's with PDFTK crashes once in a while

Post by mrdigitalis » 07 Jul 2022, 12:54

@mikeyww

Does this work? (Code after RunWait)
And can I call the function again, until the result is positiv?

Code: Select all

collectPDFs()
{
  Loop, Files, M:\HNG\_Shared\Breng-Planning\muurkranten\Muurkranten + Opkomststaten 308 en 151\AutoModus\*.*
  {
    FileGetTime, OutputVar, M:\HNG\_Shared\Breng-Planning\muurkranten\Muurkranten + Opkomststaten 308 en 151\AutoModus\*.*, M 
    FormatTime, DateCreated, %A_LoopFileTimeCreated%, dd-M-yyyy
    FormatTime, CurrentDateTime,, dd-M-yyyy
    
    if (CurrentDateTime = DateCreated)
    {
      PDF_IN =   "%A_LoopFileFullPath%" 
      PDF_IN_Total .= PDF_IN A_Space
    }
  }
  PDFKT_Path := "H:\Settings\Desktop\AutoHotkey_1.1.33.10\PDFTK\App\pdftkbuilder\pdftk.exe"
  DateTomorrow := Add_Days_to_CurrentDate_ShortDate(1)
  NewFile := "M:\HNG\_Shared\Breng-Planning\muurkranten\Muurkranten + Opkomststaten 308 en 151\AutoModus\" . "_Alle-PDFs-Samen-" . DateTomorrow . "_.pdf"
  RunWait, %comspec% /c ""%PDFKT_Path%" %PDF_IN_Total% "output" "%NewFile%"", , hide
  if ErrorLevel > 0
  {
    SoundBeep, 1500
    msgbox, something went wrong
    collectPDFs()
  }
}

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Function collect PDF's with PDFTK crashes once in a while

Post by mikeyww » 07 Jul 2022, 13:14

I do not have a way to test-- I was showing an example of something that you can try-- but I would add that you would want to gain the answers to these questions by running these scripts to test them.

Post Reply

Return to “Ask for Help (v1)”