Skip to content

Instantly share code, notes, and snippets.

@aimfireready
Last active March 28, 2019 20:24
Show Gist options
  • Select an option

  • Save aimfireready/7a19e2856f6feecb710cdd069655636b to your computer and use it in GitHub Desktop.

Select an option

Save aimfireready/7a19e2856f6feecb710cdd069655636b to your computer and use it in GitHub Desktop.
'Autoprint Script
'(C) 2019 Gandalf Farnam
'Licensed under the MIT license.
'v1.0 2019-03-28
'REQUIREMENTS:
''Requires Adobe Reader: https://get.adobe.com/reader/
'Make sure the path below matches your path to the Adobe Reader .exe file.
'USAGE:
'You can run this file manually or Schedule a Task
'I made a Dropbox folder called "Autoprint" and I auto-run it every 5 minutes.
'-------------------------
'Static variables
Dim objScriptShell, objShellApp, objFSO, objWMIService, colPrinters 'System
Dim targetFolder, doneFolder 'Script Variables
Set objScriptShell = CreateObject("WScript.Shell")
Set objShellApp = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
targetFolder = objFSO.GetAbsolutePathName(".")
doneFolder = TargetFolder & "\Done\"
'Create Done Folder If It Doesn't Exist
'------------------------------------
If not objFSO.FolderExists(doneFolder) then
objFSO.CreateFolder doneFolder
End If
'Assign Target Printer
'------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
For Each objPrinter in colPrinters
If objPrinter.Default Then
'Log Info: MsgBox("Default Printer is: " & objPrinter.Name)
Set targetPrinter = objPrinter
End If
Next
'Get Files From Target Folder
'------------------------------------
Set objFolder = objShellApp.Namespace(targetFolder)
Set colFiles = objFolder.Items
If objFolder.Items.Count > 2 Then
'Log Info: MsgBox("Folder contains " & objFolder.Items.Count -2 & " printable files.")
For Each objFile in colFiles
If objFSO.GetExtensionName(objFile) = "pdf" Then
programPath = """C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"""
programArguments = " /t "
objFilePath = Chr(34) & objFile.Path & Chr(34)
'Log Info: MsgBox("Full Path = " & programPath & programArguments & objFilePath)
objScriptShell.Run(programPath & programArguments & objFilePath)
WScript.Sleep(5000) 'Wait 5 seconds for Acrobat Reader to print the File
objScriptShell.Run("taskkill /f /im AcroRd32.exe")
WScript.Sleep(1000) 'Wait 1 second until Acrobat Reader releases the File
objFSO.MoveFile objFile, doneFolder
Else
'Log Info: MsgBox("File name '" & objFile.Name & "' is not a PDF.")
End If
Next
Else
'Log Info: MsgBox("There are no files to print.")
End If
'Permission is hereby granted, free of charge, to any person obtaining a copy of this software
' and associated documentation files (the "Software"), to deal in the Software without
' restriction, including without limitation the rights to use, copy, modify, merge,
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
' persons to whom the Software is furnished to do so, subject to the following conditions:
'
'The above copyright notice and this permission notice shall be included in all copies or substantial
' portions of the Software.
'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
'NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
'DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment