#NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=PrintFilePrinter.ico #AutoIt3Wrapper_Res_Comment=PrintFilePrinter #AutoIt3Wrapper_Res_Description=PrintFilePrinter #AutoIt3Wrapper_Res_Fileversion=1.0.0.96 #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y #AutoIt3Wrapper_Run_After=d:\dropbox\signfilesem.exe "%out%" #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #pragma compile(AutoItExecuteAllowed, True) ; #include #include #include #include #include #include #include #include #include #include #include #include #include #include ; #include #include #include #include Global $printername, $printer_list[1], $printer_list[1], $printer_list_ext[1], $printer_radio_array[1] Global $gh, $defPtr Global $watchPath Global $select, $format, $print, $afour Global $exitItem, $aboutItem, $pageTip Global $pageSize, $pjlString, $dosPtrString, $txtPdfString Global $pdfPath, $doNotOpen Global $procCmd, $doProcCmd Global $pdfonly $pdfonly = 0 If StringInStr(@ScriptName, "PDF") Then $pdfonly = 1 EndIf Global $noprintdrop $noprintdrop = 0 Global $t If $pdfonly = 0 Then $t = "PrintFilePrinter" Else $t = "PDFWatchPrint" EndIf Global $qt = Chr(34) Global $space = " " If $pdfonly = 0 Then If _Singleton("PrintFilePrinter", 1) = 0 Then MsgBox(48, "PrintFilePrinter", "PrintFilePrinter.exe is already running.", 2) Exit EndIf Else If _Singleton("PDFWatchPrint", 1) = 0 Then MsgBox(48, "PDFWatchPrint", "PDFWatchPrint is already running.", 2) Exit EndIf EndIf Global $utilDir = @ScriptDir & "\Utility" ; NB no backslash in @ScriptDir Global $pclExe = $utilDir & "\pcl6.exe" Global $gsExe = $utilDir & "\gswin32c.exe" Global $pdfPtr = $utilDir & "\PDFtoPrinter.exe" Global $rpExe = $utilDir & "\RawPrint.exe" Global $txtExe = $utilDir & "\txt2pdf.exe" Global $epsonExe = $utilDir & "\DOSPrinter.exe" Global $ptrIco = $utilDir & "\ptr.ico" If Not FileExists($pclExe) Then FileMissing() If Not FileExists($gsExe) Then FileMissing() If Not FileExists($pdfPtr) Then FileMissing() If Not FileExists($rpExe) Then FileMissing() If Not FileExists($epsonExe) Then FileMissing() If Not FileExists($ptrIco) Then FileMissing() $doNotOpen = 0 $pdfPath = @DesktopDir Local $cmln = $CmdLine[0] Local $watchPathSet = 0 Local $pdfPathSet = 0 If $cmln > 6 Then UsageBox() EndIf $procCmd = "" If $cmln >= 1 Then For $i = 1 To $CmdLine[0] If StringInStr($CmdLine[$i], "/") Then If StringInStr($CmdLine[$i], "/pdfpath=") Then $pdfPath = StringMid($CmdLine[$i], 10) If StringRight($pdfPath, 1) = "\" Then $pdfPath = StringLeft($pdfPath, (StringLen($pdfPath) - 1)) EndIf If Not FileExists($pdfPath) Then MsgBox(0, $t, "I can't find the specified PDF output path: " & @CRLF & @CRLF _ & $pdfPath & @CRLF & @CRLF _ & "Either it doesn't exist or you didn't put quotation marks around a path with spaces." & @CRLF & @CRLF _ & "Please try again.") Exit EndIf $pdfPathSet = 1 ElseIf StringInStr($CmdLine[$i], "/donotopen") Then $doNotOpen = 1 ElseIf StringInStr($CmdLine[$i], "/pdfonly") Then $pdfonly = 1 ElseIf StringInStr($CmdLine[$i], "/noprintdrop") Then $noprintdrop = 1 ElseIf StringInStr($CmdLine[$i], "/proccmd=") Then $procCmd = StringMid($CmdLine[$i], 10) If Not FileExists($procCmd) Then MsgBox(0, $t, "I can't find the specified command file: " & @CRLF & @CRLF _ & $procCmd & @CRLF & @CRLF _ & "Either it doesn't exist or you didn't put quotation marks around a file path with spaces." & @CRLF & @CRLF _ & "Please try again.") Exit EndIf If Not @Compiled Then ConsoleWrite("Command file: " & $procCmd & @CRLF) $doProcCmd = 1 ElseIf StringInStr($CmdLine[$i], "/?") Then UsageBox() ElseIf StringInStr($CmdLine[$i], "/help") Then UsageBox() Else UsageBox() EndIf Else $watchPath = ($CmdLine[$i]) If Not @Compiled Then ConsoleWrite("watchpath=" & $watchPath & @LF) $watchPathSet = 1 EndIf Next EndIf If $pdfPathSet = 0 Then $pdfPath = RegRead("HKCU\Software\WPDOS.org", "PdfPath") If ($pdfPath = "" Or $pdfPath = " ") Then $pdfPath = @DesktopDir RegDelete("HKCU\Software\WPDOS.org", "PdfPath") Else If StringRight($pdfPath, 1) = "\" Then $pdfPath = StringLeft($pdfPath, (StringLen($pdfPath) - 1)) EndIf DirCreate($pdfPath) If Not FileExists($pdfPath) Then MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), "Error", "Unable to create the folder for saving PDFs:" _ & @CRLF & @CRLF & $pdfPath) Exit EndIf RegWrite("HKCU\Software\WPDOS.org", "PdfPath", "REG_SZ", $pdfPath) EndIf EndIf If Not @Compiled Then ConsoleWrite("pdfPath: " & $pdfPath & @LF) If $watchPathSet = 0 Then Local $msgStr If $pdfonly = 0 Then $watchPath = RegRead("HKCU\Software\WPDOS.org", "FilePrintPath") $msgStr = "printer output" Else $watchPath = RegRead("HKCU\Software\WPDOS.org", "PDFWatchPath") $msgStr = "PDF files for printing" EndIf If $watchPath = "" Then $drive = EnvGet("systemdrive") $firstTemp = $drive & "\TEMP" $watchPath = InputBox($t, "Enter the path that I will watch for " & $msgStr & " (default " _ & $firstTemp & "):", $firstTemp) If $watchPath = "" Then Exit Else If StringRight($watchPath, 1) = "\" Then $watchPath = StringLeft($watchPath, (StringLen($watchPath) - 1)) EndIf EndIf EndIf If $doProcCmd = "" Then If $noprintdrop = 1 Then MsgBox(0, $t, "You have set the option NOT to print a dropped PDF (/noprintdrop), but you have not " & _ "specified a command or program that would process the dropped PDF (/proccmd=)." & _ @CRLF & @CRLF & "This means that any PDF file dropped into " & _ $watchPath & " would simply be deleted. Nothing would be done with it." & @CRLF & @CRLF & _ "This is probably not what you want, so I'll quit when you press Enter or click OK.") Exit EndIf EndIf DirCreate($watchPath) If Not FileExists($watchPath) Then MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), "Error", "Unable to create the watched printfile output folder:" _ & @CRLF & @CRLF & $watchPath) Exit EndIf If $pdfonly = 0 Then RegWrite("HKCU\Software\WPDOS.org", "FilePrintPath", "REG_SZ", $watchPath) Else RegWrite("HKCU\Software\WPDOS.org", "PDFWatchPath", "REG_SZ", $watchPath) EndIf $totalArray = _FileListToArrayRec($watchPath, "*", 1, 0, 0, 0) If Not $totalArray = "" Then Local $fileCount = $totalArray[0] If $fileCount > 3 Then $folderWarning = RegRead("HKCU\Software\WPDOS.org", "WatchedFolderWarning") If $folderWarning <> "True" Then Local $g = MsgBox(1 + 48 + 256, $t, "Your chosen watch folder " & $watchPath _ & " already contains " & $fileCount & " files." _ & @CRLF & @CRLF & "Are you sure you want to use " & $watchPath & " as your watched folder? " _ & "If so, click OK, and I won't warn you again." _ & @CRLF & @CRLF & _ "If not, click Cancel. Then reinstall me, choosing a different folder for watching.") If $g = 2 Then Exit RegWrite("HKCU\Software\WPDOS.org", "WatchedFolderWarning", "REG_SZ", "True") EndIf EndIf EndIf $pdfText = $watchPath & "\PDFNAME.TMP" FileDelete($pdfText) $sendClip = $watchPath & "\SENDCLIP.TXT" FileDelete($sendClip) $send1252 = $watchPath & "\SEND1252.TMP" FileDelete($send1252) FileDelete($watchPath & "\@") FileDelete($watchPath & "\%") FileDelete($watchPath & "\#") FileDelete($watchPath & "\&") $pageSize = RegRead("HKCU\Software\WPDOS.org", "PDFsize") If $pageSize = "" Then $pageSize = "letter" Else $pageSize = StringLower($pageSize) If Not ($pageSize = "a4" Or $pageSize = "legal") Then $pageSize = "letter" EndIf EndIf $pageTip = _StringTitleCase($pageSize) If $pageTip = "Letter" Then $pageTip = "US Letter" ;~ ; this may be redundant ;~ If $pageSize = "a4" Then ;~ A4Settings() ;~ ;$pjlString = $space & "-J" & $qt & "@PJL SET PAPER = A4" & $qt & $space ;~ ;$dosPtrString = $space & "/PageA4" & $space ;~ ;$txtPdfString = $space & "-pps:7" & $space ;~ ElseIf $pageSize = "legal" Then ;~ LegalSettings() ;~ Else ;~ DefaultPageSettings() ;~ EndIf $defPtr = _GetDefaultPrinter() $defPtr = $qt & $defPtr & $qt Global $procDir = @TempDir & "\PrintFileTemp" DirCreate($procDir) $warnList = "" $warnNumber = 0 If $pdfonly = 0 Then $listArray = _FileListToArrayRec($watchPath, "@;#;%;&;*.pcl;*.prn;*.ps;*.raw;*.tmp;*.pdf", 1, 0, 0, 0) Else $listArray = _FileListToArrayRec($watchPath, "*.pdf", 1, 0, 0, 0) EndIf For $i = 1 To UBound($listArray) - 1 Local $sFileName = $listArray[$i] Local $sOrigPath = $watchPath & "\" & $sFileName Local $sFilePath = $procDir & "\" & $sFileName If FileExists($sOrigPath) Then If Not @Compiled Then ConsoleWrite("Warning about: " & $sOrigPath & @LF) $warnList = $warnList & $sOrigPath & @CRLF $warnNumber = $warnNumber + 1 EndIf Next $warnString = "these files" If $warnList <> "" Then If $warnNumber = 1 Then $warnString = "this file" Local $g = MsgBox(1 + 48 + 256, $t, "If you let me continue, I will convert or print and then DELETE " & $warnString & ":" _ & @CRLF & @CRLF & $warnList & @CRLF & "If you really want me to do that, click OK." & @CRLF & @CRLF & _ "If not, click Cancel and move " & $warnString & " elsewhere. Or reinstall me to watch a different folder.") If $g = 2 Then Exit EndIf OnAutoItExitRegister('OnAutoItExit') If $doProcCmd = 1 Then MsgBox(4096, $t, "Watching " & $watchPath & @CRLF & @CRLF & "PDF process cmd: " & $procCmd, 1.4) Else MsgBox(4096, $t, "Watching " & $watchPath, 0.7) EndIf If $pdfonly = 0 Then _CreateChild() ;; create the ToolTip as a separate script Else _PDFCreateChild() ;; create the ToolTip as a separate script EndIf While 1 Sleep(500) Local $pdfTemp = 0 If $pdfonly = 0 Then $listArray = _FileListToArrayRec($watchPath, "@;#;%;&;*.pcl;*.prn;*.ps;*.raw;*.tmp;*.pdf", 1, 0, 0, 0) Else $listArray = _FileListToArrayRec($watchPath, "*.pdf", 1, 0, 0, 0) EndIf For $i = 1 To UBound($listArray) - 1 Local $sFileName = $listArray[$i] Local $sOrigPath = $watchPath & "\" & $sFileName Local $sFilePath = $procDir & "\" & $sFileName If FileExists($sOrigPath) Then Local $fileUsed $fileUsed = 0 $fileUsed = _FileIsUsed($sOrigPath) If $fileUsed = 1 Then While 1 Sleep(100) $fileUsed = _FileIsUsed($sOrigPath) If $fileUsed = 0 Then ExitLoop WEnd EndIf FileMove($sOrigPath, $sFilePath, 1) If Not @Compiled Then ConsoleWrite($sOrigPath & " Moved to: " & $sFilePath & @LF) If FileExists($sFilePath) Then Local $fileUsed $fileUsed = 0 $fileUsed = _FileIsUsed($sFilePath) If $fileUsed = 1 Then While 1 Sleep(100) $fileUsed = _FileIsUsed($sFilePath) If $fileUsed = 0 Then ExitLoop WEnd EndIf ;; capture shortnames, including "@" If (StringLen($sFileName)) = 1 Then If ("@" = $sFileName) Or ("#" = $sFileName) Or ("%" = $sFileName) Or ("&" = $sFileName) Then OneCharName($sFileName) EndIf ;~ ElseIf StringUpper($sFileName) = "LPV1" Then ;~ FileMove($sFilePath, $watchPath & "\OUTPUTLV.PRN") ;~ ElseIf StringUpper($sFileName) = "LPV2" Then ;~ FileMove($sFilePath, $watchPath & "\SELECTLV.PRN") ;~ ElseIf StringUpper($sFileName) = "LPV3" Then ;~ FileMove($sFilePath, $watchPath & "\PDFLV.PRN") Else ;; start everything that isn't one-character If (StringLower(StringRight($sFileName, 4))) = ".tmp" Then If $sFileName = "CLIPOUT.TMP" Then Local $handle0 = FileOpen($sFilePath, 0) Local $textfrom = FileRead($handle0) FileClose($handle0) FileDelete($sFilePath) If StringRight($textfrom, 2) = @CRLF Then $textfrom = StringLeft($textfrom, (StringLen($textfrom) - 2)) EndIf ClipPut($textfrom) ElseIf $sFileName = "CLIP850.TMP" Then Local $handle0 = FileOpen($sFilePath, 0) Local $text850 = FileRead($handle0) FileClose($handle0) FileDelete($sFilePath) Local $iCodePage = 850 Local $clipUnicode = _WinAPI_MultiByteToWideChar($text850, $iCodePage, 0, True) $clipUnicode = StringTrimRight($clipUnicode, 1) If StringRight($clipUnicode, 2) = @CRLF Then $clipUnicode = StringLeft($clipUnicode, (StringLen($clipUnicode) - 2)) EndIf ClipPut($clipUnicode) ElseIf $sFileName = "CLIP437.TMP" Then Local $handle0 = FileOpen($sFilePath, 0) Local $text437 = FileRead($handle0) FileClose($handle0) FileDelete($sFilePath) Local $iCodePage = 437 Local $clipUnicode = _WinAPI_MultiByteToWideChar($text850, $iCodePage, 0, True) $clipUnicode = StringTrimRight($clipUnicode, 1) If StringRight($clipUnicode, 2) = @CRLF Then $clipUnicode = StringLeft($clipUnicode, (StringLen($clipUnicode) - 2)) EndIf ClipPut($clipUnicode) ElseIf $sFileName = "CLIP1252.TMP" Then Local $handle0 = FileOpen($sFilePath, 0) Local $text1252 = FileRead($handle0) FileClose($handle0) FileDelete($sFilePath) If StringRight($text1252, 2) = @CRLF Then $text1252 = StringLeft($text1252, (StringLen($text1252) - 2)) EndIf ClipPut($text1252) EndIf If $sFileName = "GETCLIP.TMP" Then FileDelete($sFilePath) If FileExists($sendClip) Then FileDelete($sendClip) Local $textto = ClipGet() Local $iCodePage = 1 ;; uses OEM string of current system Local $clipUnicode = _WinAPI_WideCharToMultiByte($textto, $iCodePage, True) $clipUnicode = StringTrimRight($clipUnicode, 1) Local $textto = ChrW(32) & $clipUnicode If @error Then $textto = " " EndIf Local $handle0 = FileOpen($sendClip, 2) FileWrite($handle0, $textto) FileClose($handle0) EndIf EndIf If StringInStr($sFileName, "raw") Then If StringInStr($sFileName, "select") Then $select = 1 Else $select = 0 EndIf PrintRawFile($sFilePath, $select) Else If StringInStr($sFileName, "lg.") Then $pageSize = "legal" ElseIf StringInStr($sFileName, "a4.") Then $pageSize = "a4" ElseIf StringInStr($sFileName, "us.") Then $pageSize = "letter" EndIf $pdfTemp = 0 ;MsgBox(0, '', $pdfTemp & @CRLF & StringLeft($sFileName, 7)) If StringInStr($sFileName, "pdftemp", 0, 1, 1, 7) Then $pdfTemp = 1 Else $pdfTemp = 0 EndIf If Not @Compiled Then ConsoleWrite("Debug source pdfTemp= " & $pdfTemp & @LF) If StringInStr($sFileName, ".pcl") Then $format = "pcl" $print = 1 $select = 0 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) ElseIf StringInStr($sFileName, ".ps") Then $format = "ps" $print = 1 $select = 0 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) ElseIf StringInStr($sFileName, ".esc") Then Sleep(200) $format = "epson" $print = 1 $select = 0 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) ElseIf StringInStr($sFileName, ".prn") Then $format = GetFileFormat($sFilePath) $print = 1 $select = 0 If StringLeft($sFileName, 6) = "select" Then $select = 1 $print = 1 ElseIf StringLeft($sFileName, 3) = "pdf" Then $select = 0 $print = 0 EndIf If Not @Compiled Then ConsoleWrite("PRN test format: " & $format & @CRLF) MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) ElseIf StringInStr($sFileName, ".pdf") Then PrintPDF($sFilePath, $select, $doProcCmd, $procCmd, $noprintdrop) EndIf EndIf EndIf ;; not one-char EndIf ;; If FileExists($filePath) EndIf ;; If FileExists($origPath) ;ProcessFiles($sFileName, $sFilePath) Next WEnd Func PrintPDF($sFilePath, $select, $doProcCmd, $procCmd, $noprintdrop) If $noprintdrop = 0 Then Local $usePtr = "" If $select = 1 Then Local $ptrselmsg = "Select a printer:" GetPrinter($ptrselmsg) Do Sleep(100) Until $printername $usePtr = " " & $qt & $printername & $qt Else $usePtr = "" EndIf If Not @Compiled Then ConsoleWrite("PDF file: " & $sFilePath & @CRLF) ; Local $pdfProcFile = $procDir & "\" & $sFilePath ; MsgBox(0,'', $sFilePath) ; Exit If Not @Compiled Then ConsoleWrite(@ComSpec & " /c " & $qt & $qt & $pdfPtr & $qt & " " & _ $qt & $sFilePath & $qt & $usePtr & $qt & @CRLF) RunWait(@ComSpec & " /c " & $qt & $qt & $pdfPtr & $qt & " " & $qt & $sFilePath & _ $qt & $usePtr & $qt, $utilDir, @SW_HIDE) EndIf ; MsgBox(0, '', $sFilePath) If $doProcCmd = 1 Then If Not @Compiled Then ConsoleWrite(@ComSpec & " /k " & $qt & $qt & $procCmd & $qt & " " & $qt & $sFilePath & $qt & $qt) RunWait(@ComSpec & " /c " & $qt & $qt & $procCmd & $qt & " " & $qt & $sFilePath & $qt & $qt, $procDir, @SW_HIDE) EndIf FileDelete($sFilePath) EndFunc ;==>PrintPDF Func _FileIsUsed($sFilePath) ;; By Nessie. Modified by guinness. Local Const $hFileOpen = _WinAPI_CreateFile($sFilePath, $CREATE_ALWAYS, (DriveGetType($sFilePath)) = 'NETWORK' ? $FILE_SHARE_READ : $FILE_SHARE_WRITE) Local $fReturn = True If $hFileOpen Then _WinAPI_CloseHandle($hFileOpen) $fReturn = False EndIf If $fReturn Then $fReturn = _WinAPI_GetLastError() = $ERROR_SHARING_VIOLATION EndIf Return $fReturn EndFunc ;==>_FileIsUsed Func _GetDefaultPrinter() ; CyberSlug - 18 Nov 2004 Local $key, $default, $defPrtNm $key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers" $default = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device") $defPrtNm = StringLeft($default, StringInStr($default, ",") - 1) Return $defPrtNm EndFunc ;==>_GetDefaultPrinter Func PrintRawFile($sFilePath, $select) If Not @Compiled Then ConsoleWrite("rawprint printfile: " & $sFilePath & @LF) Local $usePtr = "" If $select = 1 Then Local $ptrselmsg = "Select a printer compatible with the output:" GetPrinter($ptrselmsg) Do Sleep(100) Until $printername $usePtr = $qt & $printername & $qt Else $usePtr = $defPtr ; already has quotes EndIf RunWait(@ComSpec & " /c " & $qt & $qt & $rpExe & $qt & " " & $usePtr & " " & _ $qt & $sFilePath & $qt & $qt, @ScriptDir, @SW_HIDE) FileDelete($sFilePath) EndFunc ;==>PrintRawFile Func MakePDF($sFilePath, $format, $print, $select, $pageSize, $pdfTemp) If Not @Compiled Then ConsoleWrite($sFilePath & @LF & $format & @LF & $print) If $pageSize = "a4" Then A4Settings() ElseIf $pageSize = "legal" Then LegalSettings() ElseIf $pageSize = "letter" Then USLetterSettings() ElseIf $pageSize = "" Then USLetterSettings() EndIf If Not @Compiled Then ConsoleWrite("Debug from MakePdf pdfTemp = " & $pdfTemp & @LF) $dt = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC Local $finalPath $finalPath = "" If $print = 0 Then If FileExists($pdfText) Then Local $tPDF = FileOpen($pdfText, 0) $finalPath = FileReadLine($tPDF, 1) FileClose($tPDF) FileDelete($pdfText) If StringLower(StringRight($finalPath, 4)) <> ".pdf" Then $finalPath = $finalPath & ".pdf" FileDelete($pdfText) ElseIf EnvGet("pfpPdf") <> "" Then Else If $pdfTemp = 1 Then $finalPath = @TempDir & "\tmp-" & $dt & ".pdf" Else $finalPath = IncrementFilename($sFileName, $sFilePath) ; $finalPath = $pdfPath & "\" & $dt & ".pdf" EndIf EndIf ; pdfText ElseIf $print = 1 Then $finalPath = @TempDir & "\" & $dt & ".pdf" ElseIf $print = 2 Then ; no need to do anything? EndIf ; if $print 1/0 FileDelete($pdfText) If FileExists($finalPath) Then $o = MsgBox(1 + 256, $t, "The output file " & $finalPath & " already exists. Overwrite it?") If $o = 2 Then FileDelete($sFilePath) Exit EndIf EndIf Local $runCmd If $format = "pcl" Then ;MsgBox(0, '', $qt & $pclExe & $qt & " -dNOPAUSE -lPCL5C -sDEVICE=pdfwrite " & $pjlString & _ ; "-sOutputFile=" & $qt & $finalPath & $qt & $space & $qt & $sFilePath & $qt) $runCmd = $qt & $pclExe & $qt & " -dNOPAUSE -lPCL5C -sDEVICE=pdfwrite " & $pjlString & _ "-sOutputFile=" & $qt & $finalPath & $qt & $space & $qt & $sFilePath & $qt ElseIf $format = "ps" Then If Not @Compiled Then ConsoleWrite("Sending PS to this final path: " & $finalPath & @CRLF) $runCmd = $qt & $gsExe & $qt & " -sBATCH -dNOPAUSE -sDEVICE=pdfwrite " & _ " -sOutputFile=" & $qt & $finalPath & $qt & " " & $qt & $sFilePath & $qt ElseIf $format = "epson" Then Local $epsonString = StringLeft($finalPath, StringLen($finalPath) - 4) $runCmd = $qt & $epsonExe & $qt & $dosPtrString & "/PDF /FILE'" & $epsonString & "' " & $qt & $sFilePath & $qt ElseIf $format = "text" Then $runCmd = $qt & $txtExe & $qt & " " & $qt & $sFilePath & $qt & " " & $qt & $finalPath & $qt & _ $txtPdfString & "-pfs10" EndIf RunWait($runCmd, $utilDir, @SW_HIDE) Sleep(100) FileDelete($sFilePath) Sleep(100) If Not @Compiled Then ConsoleWrite("Should have written " & $finalPath & @LF) If $print = 0 Then If Not FileExists($finalPath) Then While 1 Sleep(100) If FileExists($finalPath) Then ExitLoop WEnd EndIf If $doNotOpen = 0 Then ShellExecute($finalPath) ElseIf $print = 1 Then Local $usePtr = "" If $select = 1 Then Local $ptrselmsg = "Select a printer:" GetPrinter($ptrselmsg) Do Sleep(100) Until $printername $usePtr = " " & $qt & $printername & $qt Else $usePtr = "" EndIf Sleep(100) If Not FileExists($finalPath) Then While 1 Sleep(100) If FileExists($finalPath) Then ExitLoop WEnd EndIf Local $fileUsed $fileUsed = 0 $fileUsed = _FileIsUsed($finalPath) If $fileUsed = 1 Then While 1 Sleep(100) $fileUsed = _FileIsUsed($finalPath) If $fileUsed = 0 Then ExitLoop WEnd EndIf ; If Not @Compiled Then ConsoleWrite(@ComSpec & " /c " & $pdfPtr & " " & $qt & $finalPath & $qt & $usePtr) ; RunWait(@ComSpec & " /c " & $pdfPtr & " " & $qt & $finalPath & $qt & $usePtr, $utilDir, @SW_HIDE) RunWait(@ComSpec & " /c " & $qt & $qt & $pdfPtr & $qt & " " & $qt & $finalPath & $qt & $usePtr & $qt, $utilDir, @SW_HIDE) Sleep(50) If $fileUsed = 1 Then While 1 Sleep(50) $fileUsed = _FileIsUsed($finalPath) If $fileUsed = 0 Then ExitLoop WEnd EndIf FileDelete($finalPath) Else ; print = 2 ; do something here EndIf EndFunc ;==>MakePDF Func GetPrinter($ptrselmsg) Global $printer_list[1] Global $printer_list_ext[1] Global $printer_radio_array[1] ; $printer_list = 0 ; $printer_list_ext = 0 ; $printer_radio_array = 0 $regprinters = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices" $currentprinter = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\", "Device") $defaultprinter = StringLeft($currentprinter, StringInStr($currentprinter, ",") - 1) Dim $i = 1 Dim $erreur_reg = False While Not $erreur_reg $imprimante = RegEnumVal($regprinters, $i) $erreur_reg = @error If Not $erreur_reg Then _ArrayAdd($printer_list, $imprimante) _ArrayAdd($printer_list_ext, $imprimante & "," & RegRead($regprinters, $imprimante)) EndIf $i = $i + 1 WEnd _ArrayDelete($printer_list, 0) _ArrayDelete($printer_list_ext, 0) If UBound($printer_list) >= 2 Then ;if 2 or more printers available, we show the dialog Dim $groupheight = (UBound($printer_list) + 1) * 25 ;30 Dim $guiheight = $groupheight + 50 Dim $buttontop = $groupheight + 20 Opt("GUIOnEventMode", 1) $gh = GUICreate($ptrselmsg, 400, $guiheight) Dim $font = "Verdana" GUISetFont(10, 400, 0, $font) GUISetOnEvent($GUI_EVENT_CLOSE, "CloseClicked") GUISetFont(10, 400, 0, $font) GUICtrlCreateGroup("Available printers:", 10, 10, 380, $groupheight) Dim $position_vertical = 5 ; 0 For $i = 0 To UBound($printer_list) - 1 Step 1 GUISetFont(10, 400, 0, $font) $position_vertical = $position_vertical + 25 ;30 $radio = GUICtrlCreateRadio($printer_list[$i], 20, $position_vertical, 350, 20) _ArrayAdd($printer_radio_array, $radio) If $currentprinter = $printer_list_ext[$i] Then GUICtrlSetState($radio, $GUI_CHECKED) EndIf Next _ArrayDelete($printer_radio_array, 0) GUISetFont(10, 400, 0, $font) $okbutton = GUICtrlCreateButton("OK", 10, $buttontop, 50, 25) GUICtrlSetOnEvent($okbutton, "OKButton") Local $AccelKeys[2][2] = [["{ENTER}", $okbutton], ["^O", $okbutton]] GUISetAccelerators($AccelKeys) GUISetState() EndIf EndFunc ;==>GetPrinter Func OKButton() For $i = 0 To UBound($printer_radio_array) - 1 Step 1 If GUICtrlRead($printer_radio_array[$i]) = 1 Then $printername = StringLeft($printer_list_ext[$i], StringInStr($printer_list_ext[$i], ",") - 1) EndIf Next GUIDelete($gh) ; If Not @Compiled Then ConsoleWrite($printername & @LF) EndFunc ;==>OKButton Func CloseClicked() ;$printername = $defaultprinter GUIDelete($gh) $printername = "nul" ; If Not @Compiled Then ConsoleWrite($printername & @LF) ;Cancelled() ; MsgBox(0, "", $printername) ; Return $printername EndFunc ;==>CloseClicked Func Cancelled() MsgBox(262144, $t, "Script cancelled.") Exit EndFunc ;==>Cancelled Func GetFileFormat($sFilePath) $w = FileOpen($sFilePath, 16) $b = FileRead($w, 128) FileClose($w) $bx = StringLeft($b, 6) If StringInStr($b, "1B25") Then ; esc-% If StringInStr($b, "2521") Then ; %!PS ; was "25215043" $format = "ps" Else $format = "pcl" EndIf ElseIf StringInStr($bx, "1B45") Then ; esc-E $format = "pcl" ElseIf StringInStr($b, "2521") Then ; was "25215053" $format = "ps" ElseIf StringInStr($bx, "1B40") Then $format = "epson" ;;ElseIf StringInStr($b, "0425") Then ;; figure out what that means Else $format = "text" EndIf If Not @Compiled Then ConsoleWrite("GetFileFormat: " & $format & @CRLF) Return ($format) EndFunc ;==>GetFileFormat Func FileMissing() MsgBox(0, $t, "At least one required file is missing. Please reinstall this system.") EndFunc ;==>FileMissing Func ExitScript() TrayItemSetState($exitItem, 4) $e = MsgBox(4356, $t, "Do you want to stop watching the folder " & $watchPath & "?" & @CRLF & @CRLF & _ "If you choose Yes, the automatic printing feature won't work until you restart me.") If $e = 6 Then Exit EndIf EndFunc ;==>ExitScript Func A4Settings() $pjlString = $space & "-J" & $qt & "@PJL SET PAPER = A4" & $qt & $space $dosPtrString = $space & "/PageA4" & $space $txtPdfString = $space & "-pps:7" & $space EndFunc ;==>A4Settings Func LegalSettings() $pjlString = $space & "-J" & $qt & "@PJL SET PAPER = LEGAL" & $qt & $space $dosPtrString = $space & "/PageLEG" & $space $txtPdfString = $space & "-pps:1" & $space EndFunc ;==>LegalSettings Func USLetterSettings() $pjlString = $space $dosPtrString = $space $txtPdfString = $space EndFunc ;==>USLetterSettings Func Lines66Settings() $pjlString = $space & "-J" & $qt & "@PJL SET FORMLINES = 66" & $qt & $space $dosPtrString = $space & "/66" & $space $txtPdfString = $space & "-lpp:66" & $space EndFunc ;==>Lines66Settings Func OneCharName($sFileName) If $sFileName = "@" Then ;; treat as PRN FileMove($sFilePath, $watchPath & "\OUTPUTLV.PRN") If Not @Compiled Then ConsoleWrite("Moved " & $sFilePath & " to " & $watchPath & "\OUTPUTLV.PRN" & @LF) ElseIf $sFileName = "%" Then ;; treat as SELECT.PRN FileMove($sFilePath, $watchPath & "\SELECTLV.PRN") If Not @Compiled Then ConsoleWrite("Moved " & $sFilePath & " to " & $watchPath & "\SELECTLV.PRN" & @LF) ElseIf $sFileName = "#" Then ;; create PDF FileMove($sFilePath, $watchPath & "\PDFLV.PRN") If Not @Compiled Then ConsoleWrite("Moved " & $sFilePath & " to " & $watchPath & "\PDFLV.PRN" & @LF) ElseIf $sFileName = "&" Then ;; clipboard FileMove($sFilePath, $watchPath & "\CLIPOUT.TMP") If Not @Compiled Then ConsoleWrite("Moved " & $sFilePath & " to " & $watchPath & "\CLIPOUT.TMP" & @LF) EndIf EndFunc ;==>OneCharName Func _CreateChild() If $doProcCmd = 1 Then $tipProcString = @CRLF & $procCmd & @CRLF & @CRLF Else $tipProcString = "" EndIf EnvSet("pageTip", $pageTip) EnvSet("parentPID", @AutoItPID) EnvSet("pdfSavePath", $pdfPath) EnvSet("procStr", $procCmd) If Not @Compiled Then ConsoleWrite("ToolTip watching: " & $watchPath & @LF) FileCopy($ptrIco, @TempDir & "\ptr.ico") Global $sTempFile = _WinAPI_GetTempFileName(@TempDir, "~") Local $sScript = _ "$procStr = EnvGet('procStr')" & @CRLF & _ "$procStr = $procStr & @CRLF & @CRLF " & @CRLF & _ "$watchPath = $CmdLine[2]" & @CRLF & _ "$pageTip = EnvGet('pageTip')" & @CRLF & _ "$parentPID = EnvGet('parentPID')" & @CRLF & _ "$pdfPath = EnvGet('pdfSavePath')" & @CRLF & _ "$t = 'PrintFilePrinter'" & @CRLF & _ "Opt('TrayAutoPause', 0)" & @CRLF & _ "Opt('TrayOnEventMode', 1)" & @CRLF & _ "Opt('TrayMenuMode', 1+2)" & @CRLF & _ "TraySetToolTip('Watching folder: ' & $watchPath)" & @CRLF & _ "TraySetIcon(@TempDir & '\ptr.ico')" & @CRLF & _ "OnAutoItExitRegister (_Exit)" & @CRLF & _ "$aboutItem = TrayCreateItem('Information...')" & @CRLF & _ "TrayItemSetState($aboutItem, 4)" & @CRLF & _ "TrayItemSetOnEvent(-1, 'AboutScript')" & @CRLF & _ "TrayCreateItem('')" & @CRLF & _ "$openItem = TrayCreateItem('Open ' & $watchPath)" & @CRLF & _ "TrayItemSetState($openItem, 4)" & @CRLF & _ "TrayItemSetOnEvent(-1, 'OpenFolder')" & @CRLF & _ "TrayCreateItem('')" & @CRLF & _ "$exitItem = TrayCreateItem('Exit')" & @CRLF & _ "TrayItemSetState($exitItem, 4)" & @CRLF & _ "TrayItemSetOnEvent(-1, 'ExitScript')" & @CRLF & _ "While 1" & @CRLF & _ "If Not ProcessExists($parentPID) Then Exit" & @CRLF & _ "Sleep (100)" & @CRLF & _ "WEnd" & @CRLF & _ "Func AboutScript()" & @CRLF & _ "MsgBox(0, $t, 'When active, this program watches the folder ' & @CRLF & @CRLF & $CmdLine[2] & @CRLF & @CRLF & _" & @CRLF & _ "'for these filenames and extensions:' & @CRLF & @CRLF & _" & @CRLF & _ "'*.RAW - sends raw data to default printer' & @CRLF & _" & @CRLF & _ "'*.PCL - converts PCL and prints to default printer' & @CRLF & _" & @CRLF & _ "'*.PS - converts PostScript and prints to default printer' & @CRLF & _" & @CRLF & _ "'*.ESC - converts Epson ESC/P and prints to default printer' & @CRLF & _" & @CRLF & _ "'*.PRN - checks format and prints to default printer' & @CRLF & @CRLF & _" & @CRLF & _ "'*.PDF - prints dropped PDF file and optionally processes with cmd file ' & $procStr & _" & @CRLF & _ "'SELECT*.[RAW|PCL|PS|ESC|PRN] - opens select-printer menu' & @CRLF & _" & @CRLF & _ "'PDF*.[PCL|PS|ESC|PRN] - creates/opens PDF on desktop' & @CRLF & _" & @CRLF & _ "'PDFTEMP*.[PCL|PS|ESC|PRN] - creates/opens PDF in temp folder' & @CRLF & _" & @CRLF & _ "'(PDF options not available with .RAW output)' & @CRLF & @CRLF & _" & @CRLF & _ "'PDFs saved to disk will be saved to: ' & $pdfPath & @CRLF & @CRLF & _" & @CRLF & _ "'For a non-default page size, use these filenames:' & @CRLF & @CRLF & _" & @CRLF & _ "'*A4.[PCL|PS|ESC|PRN] - use A4 size (if default is US Letter or Legal)' & @CRLF & _" & @CRLF & _ "'*LG.[PCL|PS|ESC|PRN] - use Legal size (if default is US Letter or A4)' & @CRLF & _" & @CRLF & _ "'*US.[PCL|PS|ESC|PRN] - use US Letter size (if default is Legal or A4)' & @CRLF & _" & @CRLF & _ "'(page size options not available with .RAW output)' & @CRLF & @CRLF & _" & @CRLF & _ "'Default page size: ' & $pageTip & '. Change by reinstalling')" & @CRLF & _ "EndFunc" & @CRLF & _ "Func ExitScript()" & @CRLF & _ "TrayItemSetState($exitItem, 4)" & @CRLF & _ "$e = MsgBox(4356, $t, 'Do you want to stop watching the folder ' & $watchPath & '?' & @CRLF & @CRLF & _" & @CRLF & _ "'If you choose Yes, the automatic printing feature will stop working until you restart me.')" & @CRLF & _ "If $e = 6 Then Exit" & @CRLF & _ "EndFunc" & @CRLF & _ "Func OpenFolder()" & @CRLF & _ "TrayItemSetState($openItem, 4)" & @CRLF & _ "ShellExecute($watchPath)" & @CRLF & _ "EndFunc" & @CRLF & _ "Func _Exit()" & @CRLF & _ "Switch @exitMethod" & @CRLF & _ "Case 0, 1, 2" & @CRLF & _ "FileDelete (@ScriptFullPath)" & @CRLF & _ "RunWait('TaskKill /PID ' & $CmdLine[1] & ' /F', '', @SW_HIDE)" & @CRLF & _ "EndSwitch" & @CRLF & _ "EndFunc" FileWrite($sTempFile, $sScript) Run(@AutoItExe & " /AutoIt3ExecuteScript " & $sTempFile & " " & @AutoItPID & ' "' & $watchPath & '"', @TempDir, @SW_HIDE) EndFunc ;==>_CreateChild Func _PDFCreateChild() If $doProcCmd = 1 Then $tipProcString = @CRLF & $procCmd & @CRLF & @CRLF Else $tipProcString = "" EndIf EnvSet("pageTip", $pageTip) EnvSet("parentPID", @AutoItPID) EnvSet("pdfSavePath", $pdfPath) EnvSet("procStr", $procCmd) If Not @Compiled Then ConsoleWrite("ToolTip watching: " & $watchPath & @LF) FileCopy($ptrIco, @TempDir & "\ptr.ico") Global $sTempFile = _WinAPI_GetTempFileName(@TempDir, "~") Local $sScript = _ "$procStr = EnvGet('procStr')" & @CRLF & _ "$procStr = $procStr & @CRLF & @CRLF " & @CRLF & _ "$watchPath = $CmdLine[2]" & @CRLF & _ "$pageTip = EnvGet('pageTip')" & @CRLF & _ "$parentPID = EnvGet('parentPID')" & @CRLF & _ "$pdfPath = EnvGet('pdfSavePath')" & @CRLF & _ "$t = 'PDFWatchPrint'" & @CRLF & _ "Opt('TrayAutoPause', 0)" & @CRLF & _ "Opt('TrayOnEventMode', 1)" & @CRLF & _ "Opt('TrayMenuMode', 1+2)" & @CRLF & _ "TraySetToolTip('Watching folder: ' & $watchPath)" & @CRLF & _ "TraySetIcon(@TempDir & '\ptr.ico')" & @CRLF & _ "OnAutoItExitRegister (_Exit)" & @CRLF & _ "$aboutItem = TrayCreateItem('Information...')" & @CRLF & _ "TrayItemSetState($aboutItem, 4)" & @CRLF & _ "TrayItemSetOnEvent(-1, 'AboutScript')" & @CRLF & _ "TrayCreateItem('')" & @CRLF & _ "$openItem = TrayCreateItem('Open ' & $watchPath)" & @CRLF & _ "TrayItemSetState($openItem, 4)" & @CRLF & _ "TrayItemSetOnEvent(-1, 'OpenFolder')" & @CRLF & _ "TrayCreateItem('')" & @CRLF & _ "$exitItem = TrayCreateItem('Exit')" & @CRLF & _ "TrayItemSetState($exitItem, 4)" & @CRLF & _ "TrayItemSetOnEvent(-1, 'ExitScript')" & @CRLF & _ "While 1" & @CRLF & _ "If Not ProcessExists($parentPID) Then Exit" & @CRLF & _ "Sleep (100)" & @CRLF & _ "WEnd" & @CRLF & _ "Func AboutScript()" & @CRLF & _ "MsgBox(0, $t, 'When active, this program watches the folder ' & @CRLF & @CRLF & $CmdLine[2] & @CRLF & @CRLF & _" & @CRLF & _ "'for PDF files, which it prints and optionally processes.' & @CRLF & @CRLF & _" & @CRLF & _ "'Specify processing file with /proccmd= on command line.')" & @CRLF & _ "EndFunc" & @CRLF & _ "Func ExitScript()" & @CRLF & _ "TrayItemSetState($exitItem, 4)" & @CRLF & _ "$e = MsgBox(4356, $t, 'Do you want to stop watching the folder ' & $watchPath & '?' & @CRLF & @CRLF & _" & @CRLF & _ "'If you choose Yes, the automatic printing feature will stop working until you restart me.')" & @CRLF & _ "If $e = 6 Then Exit" & @CRLF & _ "EndFunc" & @CRLF & _ "Func OpenFolder()" & @CRLF & _ "TrayItemSetState($openItem, 4)" & @CRLF & _ "ShellExecute($watchPath)" & @CRLF & _ "EndFunc" & @CRLF & _ "Func _Exit()" & @CRLF & _ "Switch @exitMethod" & @CRLF & _ "Case 0, 1, 2" & @CRLF & _ "FileDelete (@ScriptFullPath)" & @CRLF & _ "RunWait('TaskKill /PID ' & $CmdLine[1] & ' /F', '', @SW_HIDE)" & @CRLF & _ "EndSwitch" & @CRLF & _ "EndFunc" FileWrite($sTempFile, $sScript) Run(@AutoItExe & " /AutoIt3ExecuteScript " & $sTempFile & " " & @AutoItPID & ' "' & $watchPath & '"', @TempDir, @SW_HIDE) EndFunc ;==>_PDFCreateChild Func IncrementFilename($sFileName, $sFilePath) Local $prefixString = "File" If StringLeft($sFileName, 4) = "pdf-" Then Local $sDrive = "", $sDir = "", $sFileNameOnly = "", $sExtension = "" Local $aPathSplit = _PathSplit($sFilePath, $sDrive, $sDir, $sFileNameOnly, $sExtension) $nameOnly = $aPathSplit[3] If Not @Compiled Then ConsoleWrite("nameOnly: " & $nameOnly & @LF) $prefixString = (StringMid($nameOnly, 5, -1)) If Not @Compiled Then ConsoleWrite("prefixString: " & $prefixString & @LF) EndIf $totalArray = _FileListToArrayRec($pdfPath, $prefixString & "0???.pdf", 1, 0, 0, 0) If $totalArray = "" Then $finalPath = $pdfPath & "\" & $prefixString & "0000.pdf" Else Local $lastNum = UBound($totalArray) - 1 Local $topName = $totalArray[$lastNum] Local $numString = StringMid($topName, 5, 4) If $numString = 999 Then MsgBox(0, $t, "I'm sorry but I can't create a file with a number greater than 999." & @CRLF & @CRLF _ & "I'll give this file a name based on the date and time.") Local $dtbis = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC $finalPath = $pdfPath & "\" & $dtbis & ".pdf" Else $numNumber = Number($numString) $newString = StringFormat("%04d", $numNumber + 1) Local $newName = $prefixString & $newString & ".pdf" $finalPath = $pdfPath & "\" & $newName EndIf EndIf If Not @Compiled Then ConsoleWrite("Incremented name: " & $finalPath & @LF) Return $finalPath EndFunc ;==>IncrementFilename Func UsageBox() Local $d = EnvGet("systemdrive") If $pdfonly = 0 Then MsgBox(0, $t, "Usage:" & @CRLF & @CRLF _ & "PrintFilePrinter.exe" & @CRLF & @CRLF _ & "Optional command-line parameters (use one or more, or none):" & @CRLF & @CRLF _ & $d & "\Path\To\Watch\For\Printfiles (default: " & $d & "\TEMP)" & @CRLF & @CRLF _ & "/pdfpath=" & $d & "\Path\For\Converted-Saved\PDF-files (default: desktop folder)" & @CRLF & @CRLF _ & "/donotopen (do not open converted-saved PDF files for viewing)" & @CRLF & @CRLF _ & "/pdfonly (only process dropped PDFs; ignore and don't convert all other formats)" & @CRLF & @CRLF _ & "/proccmd=" & $d & "\FilePath\of\CmdOrExe for processing dropped PDFs (optional)" & @CRLF & @CRLF _ & "/noprintdrop (don't print dropped PDFs; use only with /proccmd=)" & @CRLF & @CRLF _ & "Use quotation marks around paths with spaces.") Else MsgBox(0, $t, "Usage:" & @CRLF & @CRLF _ & "PDFPrintWatch.exe" & @CRLF & @CRLF _ & "Optional command-line parameters (use one or more, or none):" & @CRLF & @CRLF _ & $d & "\Path\To\Watch\For\PDFfiles (default: " & $d & "\TEMP)" & @CRLF & @CRLF _ & "/proccmd=" & $d & "\FilePath\of\CmdOrExe for processing dropped PDFs (optional)" & @CRLF & @CRLF _ & "Use quotation marks around paths with spaces.") EndIf Exit EndFunc ;==>UsageBox Func OnAutoItExit() ProcessClose($sTempFile) FileDelete($sTempFile) If Not @Compiled Then ConsoleWrite("Exiting." & @LF) Switch @exitMethod Case 0, 1, 2 ; Exit Case 3 ; Exit Case 4 ;Exit EndSwitch EndFunc ;==>OnAutoItExit