McNugget Posted March 1, 2018 Share Posted March 1, 2018 Hello everyone, I hope, my english is good enough to explain my problem... i´m very new to autoit, but already trying to solve a problem. To tell it in advance: no commercial interest is connected to my request. I´m trying to solve this problem for private reasons. I need a free tool, that monitors a directory C:\print) for new PDF-files. If a new file appears, the tool shall print it to the default printer and move it to another folder (c:\gedruckt). Due to the circumstance that i´m trying to print to a datacard sd260 Card-Printer, which needs a native windows driver, i have no choice but use acrobat reader 11 under windows XP. Printings from Ghostscript/ghostgum are black/white and wrong scaled... Unfortunately, acrord32 won´t close after printing which stops the function i already have. I searched via google and tried to merge some code snippets. But it oesn´t work. The code i already have leads to an endless loop of acrord32 instances opening and trying to print without moving the files. expandcollapse popup#NoTrayIcon ; Hier kannst du einen Hotkey zum Beenden des Programms festlegen ; in Anführungszeichen vorm Komma, z. Z. ALT+SHIFT+Q HotKeySet("+!q", "Quit") ;Anpassungen ;Pfad zur Acrord32.exe $acrobat = "C:\Programme\Adobe\Reader 11.0\Reader" ; Pfad der auf neue Dateien überwacht werden soll $source = "c:\print\" ; Pfad, in den die Dateien nach dem Drucken verschoben werden sollen $dest = "c:\gedruckt\" ; Dauer des Prüfintervalls in Millisekungen (10 Sek = 10000) $interval = 10000 While 1 Sleep($interval) $file = FileFindFirstFile ($source & "\*.pdf") If $file <> -1 then Print() FileClose($file) Wend Func Quit() Exit EndFunc Func Print() While 1 $pdffile = FileFindNextFile($file) If @error then ExitLoop Local $iPID = RunWait('"' & $acrobat & '\AcroRd32.exe" /t "' & $source & '\' & $pdffile & '" ') WinWait ("[CLASS:Acrord32]", "", 1) ;Sleep(2000) FileMove('"' & $source & '\' & $pdffile & '"', '"' & $dest & "'", 1) ProcessClose ($iPID) WEnd EndFunc Can anyboy help me with my problem and tell me what to do to get it working? Thank everyone in advance for reading to here. Best regards McNugget Link to comment Share on other sites More sharing options...
Danp2 Posted March 1, 2018 Share Posted March 1, 2018 Your FileMove is likely failing because the file is still open in acrord32. Make sure the process is closed before attempting to move the file. McNugget 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Subz Posted March 1, 2018 Share Posted March 1, 2018 Try the following tool http://www.biopdf.com/guide/print_pdf_from_command_line.php and look at the FileListToArray functionality removes While/Wend loop McNugget 1 Link to comment Share on other sites More sharing options...
Gianni Posted March 1, 2018 Share Posted March 1, 2018 (edited) a tool I use to silently print pdf to the default (or also another) printer is sumatraPDF.exe --> https://www.sumatrapdfreader.org/free-pdf-reader.html You can print without opening the file as simple as something like this: have a look to the Printing options in the command-line arguments page: https://www.sumatrapdfreader.org/docs/Command-line-arguments.html Run('.\SumatraPDF.exe -silent -print-to "PrinterName" ' & $sFilename, "", @SW_HIDE) (you could move the file before printing and print it after the move so it's should not locked by the print process...) Edited March 1, 2018 by Chimp syntax... McNugget 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
McNugget Posted March 1, 2018 Author Share Posted March 1, 2018 Thanks to you all for your input. I´ll try if i´m able to get it running. One question: How should i move the file before i print it??? In this case there wouldn´t be a file to print before i started the print process... Or do i have a problem in understanding? Best regards McNugget Link to comment Share on other sites More sharing options...
McNugget Posted March 1, 2018 Author Share Posted March 1, 2018 Hello again... Update: I don´t get it to work. This is the actual code snippet i am using: expandcollapse popup#NoTrayIcon ; Hier kannst du einen Hotkey zum Beenden des Programms festlegen ; in Anführungszeichen vorm Komma, z. Z. ALT+SHIFT+Q HotKeySet("+!q", "Quit") ;Anpassungen ;Pfad zur Acrord32.exe $acrobat = "C:\Programme\Adobe\Reader 11.0\Reader" ; Pfad der auf neue Dateien überwacht werden soll $source = "c:\print\" ; Pfad, in den die Dateien nach dem Drucken verschoben werden sollen $dest = "c:\gedruckt\" ; Dauer des Prüfintervalls in Millisekungen (10 Sek = 10000) $interval = 10000 While 1 Sleep($interval) $file = FileFindFirstFile ($source & "\*.pdf") If $file <> -1 then Print() FileClose($file) Wend Func Quit() Exit EndFunc Func Print() While 1 $pdffile = FileFindNextFile($file) If @error then ExitLoop Run('C:\Programme\SumatraPDF\SumatraPDF.exe -silent -print-to "PDFCreator" ' & $file, "") ;Local $iPID = RunWait('"' & $acrobat & '\AcroRd32.exe" /t "' & $source & '\' & $pdffile & '" ') ;WinWait ("[CLASS:Acrord32]", "", 1) ;Sleep(2000) FileMove('"' & $source & '\' & $pdffile & '"', '"' & $dest & "'", 1) ;ProcessClose ($iPID) WEnd EndFunc The result is, that nothing happens. Sumatra is installed... Why doen´t it work? Best regards McNugget Link to comment Share on other sites More sharing options...
funkey Posted March 1, 2018 Share Posted March 1, 2018 Why did you use $file instead of $pdffile? McNugget 1 Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
Developers Jos Posted March 1, 2018 Developers Share Posted March 1, 2018 Also: You do need that runwait() in stead of run(). Consider running this with the x64 version of Autoit3 as it looks like you are using a x64 program? Jos McNugget 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
McNugget Posted March 2, 2018 Author Share Posted March 2, 2018 13 hours ago, funkey said: Why did you use $file instead of $pdffile? Oh... That was already fixed by me. Thank you for the question. 13 hours ago, Jos said: Also: You do need that runwait() in stead of run(). Consider running this with the x64 version of Autoit3 as it looks like you are using a x64 program? Jos Thank you for the hint with runwait(). It runs better now. I think, i only run x86 applications. How can i check i use the right version of autoit3? This is the actual code snippet i use. My last problem to be solved is: Why doesn´t it move the file? It still remains in the source folder... #NoTrayIcon ; Hier kannst du einen Hotkey zum Beenden des Programms festlegen ; in Anführungszeichen vorm Komma, z. Z. ALT+SHIFT+Q HotKeySet("+!q", "Quit") ;Anpassungen ;Pfad zur Acrord32.exe $acrobat = "C:\Programme\Adobe\Reader 11.0\Reader" ; Pfad der auf neue Dateien überwacht werden soll $source = "c:\print\" ; Pfad, in den die Dateien nach dem Drucken verschoben werden sollen $dest = "c:\gedruckt\" ; Dauer des Prüfintervalls in Millisekungen (10 Sek = 10000) $interval = 10000 While 1 Sleep($interval) $file = FileFindFirstFile ($source & "\*.pdf") If $file <> -1 then Print() FileClose($file) Wend Func Quit() Exit EndFunc Func Print() While 1 $pdffile = FileFindNextFile($file) If @error then ExitLoop RunWait ('C:\Programme\bioPDF\Acrobat Wrapper\acrowrap.exe /acceptlicense /t "' & $source & '\' & $pdffile & '" ') ;Sleep(5000) FileMove('"' & $source & '\' & $pdffile & '"', '"' & $dest & "'", 1) WEnd EndFunc Do you have any ideas/suggestions to fix/optimize the script? Thank you all for your kind assistance. I´m glad to get your help. Best regards McNugget Link to comment Share on other sites More sharing options...
Danp2 Posted March 2, 2018 Share Posted March 2, 2018 1 hour ago, McNugget said: My last problem to be solved is: Why doesn´t it move the file? Does it work if you remove the surrounding quotes, like this? FileMove($source & '\' & $pdffile, $dest, 1) If not, then I suggest that you check to value of @error after that line to see if that helps you diagnose the issue. McNugget 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
McNugget Posted March 2, 2018 Author Share Posted March 2, 2018 13 minutes ago, Danp2 said: Does it work if you remove the surrounding quotes, like this? FileMove($source & '\' & $pdffile, $dest, 1) If not, then I suggest that you check to value of @error after that line to see if that helps you diagnose the issue. IT WORKS! Thank you all so much... Best regards McNugget Link to comment Share on other sites More sharing options...
McNugget Posted March 2, 2018 Author Share Posted March 2, 2018 One last question: Are there any suggestions to improve "my" code snippet? Best regards McNugget Link to comment Share on other sites More sharing options...
Bilgus Posted March 2, 2018 Share Posted March 2, 2018 #NoTrayIcon Global Const $_FSF_CREATEBUTTON = 1 ;Xp Only ; Hier kannst du einen Hotkey zum Beenden des Programms festlegen ; in Anführungszeichen vorm Komma, z. Z. ALT+SHIFT+Q HotKeySet("+!q", "Quit") Local Const $sQuot = "'" ;Anpassungen ;Pfad zur Acrord32.exe $acrobat = "C:\Programme\bioPDF\Acrobat Wrapper\acrowrap.exe" ; Pfad der auf neue Dateien überwacht werden soll $source = FileSelectFolder("Pfad der auf neue Dateien überwacht werden soll", "") ; Pfad, in den die Dateien nach dem Drucken verschoben werden sollen $dest = FileSelectFolder("Pfad, in den die Dateien nach dem Drucken verschoben werden sollen", "", $_FSF_CREATEBUTTON) ; Dauer des Prüfintervalls in Millisekungen (10 Sek = 10000) $interval = 100 Global $hSearch = FileFindFirstFile($source & "\*.pdf") While Not @error And $hSearch <> -1 Sleep($interval) Print(FileFindNextFile($hSearch)) WEnd If $hSearch < 0 or @error Then MsgBox(0, "PDF", @extended == 1024 ? "FINISHED!" : "EMPTY!") Quit() Func Quit() FileClose($hSearch) Exit EndFunc ;==>Quit Func Print($pdffile) if $pdffile = "" then Return SetError(1,1024) ;RunWait ConsoleWrite('C:\Programme\bioPDF\Acrobat Wrapper\acrowrap.exe /acceptlicense /t "' & $source & '\' & $pdffile & '" ' & @CRLF) FileMove($source & "\" & $pdffile, $dest, 1) EndFunc ;==>Print Link to comment Share on other sites More sharing options...
McNugget Posted March 5, 2018 Author Share Posted March 5, 2018 Thank you, Bilgus. The script is intended to work without user inputs. Best regards McNugget Link to comment Share on other sites More sharing options...
SlackerAl Posted March 5, 2018 Share Posted March 5, 2018 Trivial comment really... I would suggest you move the Sleep($interval) from the beginning of your first while loop to the end of the loop. With it in the first line, your script will not do anything for 10 seconds after you start it. Depends how often you start the script and how immediate a response you expect from it. Bilgus 1 Problem solving step 1: Write a simple, self-contained, running, replicator of your problem. Link to comment Share on other sites More sharing options...
Bilgus Posted March 5, 2018 Share Posted March 5, 2018 Good Point Link to comment Share on other sites More sharing options...
Bilgus Posted March 5, 2018 Share Posted March 5, 2018 And now it runs without interaction after the first time expandcollapse popup#NoTrayIcon Global $gs_INI = @ScriptDir & "/config.ini" Global Const $gs_cfg = "CFG" Global Const $_FSF_CREATEBUTTON = 1 ;Xp Only Global Const $FD_FILEMUSTEXIST = (1) ; Hier kannst du einen Hotkey zum Beenden des Programms festlegen ; in Anführungszeichen vorm Komma, z. Z. ALT+SHIFT+Q HotKeySet("+!q", "Quit") Local Const $sQuot = "'" ;Anpassungen ;Pfad zur Acrord32.exe Local $acrobat = IniRead($gs_INI, $gs_cfg, "Execute", "") ;"C:\Programme\bioPDF\Acrobat Wrapper\acrowrap.exe" ; Pfad der auf neue Dateien überwacht werden soll Local $source = IniRead($gs_INI, $gs_cfg, "Source", "") ; Pfad, in den die Dateien nach dem Drucken verschoben werden sollen Local $dest = IniRead($gs_INI, $gs_cfg, "Dest", "") ; Dauer des Prüfintervalls in Millisekungen (10 Sek = 10000) Local $interval = IniRead($gs_INI, $gs_cfg, "Interval", 10000) ; if $acrobat = "" then $acrobat = FileOpenDialog("Choose App", @WindowsDir & "\", "App (*.exe)", $FD_FILEMUSTEXIST) if $source = "" then $source = FileSelectFolder("Pfad der auf neue Dateien überwacht werden soll", "") if $dest = "" then $dest = FileSelectFolder("Pfad, in den die Dateien nach dem Drucken verschoben werden sollen", "", $_FSF_CREATEBUTTON) ; IniWrite($gs_INI, $gs_cfg, "Execute", $acrobat) IniWrite($gs_INI, $gs_cfg, "Source", $source) IniWrite($gs_INI, $gs_cfg, "Dest", $dest) IniWrite($gs_INI, $gs_cfg, "Interval", $interval) ; Global $hSearch = FileFindFirstFile($source & "\*.pdf") While Not @error And $hSearch <> -1 ;Sleep($interval) Print(FileFindNextFile($hSearch)) Sleep($interval) WEnd If $hSearch < 0 Or @error Then MsgBox(0, "PDF", @extended == 1024 ? "FINISHED!" : "EMPTY!") Quit() Func Quit() FileClose($hSearch) Exit EndFunc ;==>Quit Func Print($pdffile) If $pdffile = "" Then Return SetError(1, 1024) ;RunWait ConsoleWrite($acrobat & ' /acceptlicense /t "' & $source & '\' & $pdffile & '" ' & @CRLF) FileMove($source & "\" & $pdffile, $dest, 1) EndFunc ;==>Print Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now