Jump to content

Problems with automated printing


Recommended Posts

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.

#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

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 by Chimp
syntax...

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

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

Hello again...

 

Update: I don´t get it to work. This is the actual code snippet i am using:

#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

Why did you use $file instead of $pdffile?

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

  • Developers

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

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

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

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.

Link to comment
Share on other sites

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

#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

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.

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

And now it runs without interaction after the first time

#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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...