-
Notifications
You must be signed in to change notification settings - Fork 35
/
pdf2pdfocr.vbs
executable file
·99 lines (92 loc) · 3.32 KB
/
pdf2pdfocr.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
' PDF2PDFOCR
'
' This VBS script must be used in Windows installations.
' to let the final user use the "Send To" menu option in Windows Explorer.
' Please copy it to "shell:sendto" folder or create a shortcut.
' Ref.: http://www.howtogeek.com/howto/windows-vista/customize-the-windows-vista-send-to-menu/
'
' DEPRECATED: PLEASE USE A FOLDER IN "-i" PARAMETER
' ******* FUNCTIONS ***
' Run command - see https://www.codeproject.com/Tips/507798/Differences-between-Run-and-Exec-VBScript
function execCommand(cmd)
Set WSHShell = CreateObject("Wscript.Shell")
comspec = WSHShell.ExpandEnvironmentStrings("%comspec%")
Set objExec = WSHShell.Exec(comspec & " /c " & cmd)
Do
WScript.StdOut.WriteLine(objExec.StdOut.ReadLine())
Loop While Not objExec.Stdout.atEndOfStream
WScript.StdOut.WriteLine(objExec.StdOut.ReadAll)
WScript.StdErr.WriteLine(objExec.StdErr.ReadAll)
'
execCommand = true
end function
' Credits - http://stackoverflow.com/questions/4692542/force-a-vbs-to-run-using-cscript-instead-of-wscript
Sub forceCScriptExecution
Dim Arg, Str
If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
For Each Arg In WScript.Arguments
If InStr( Arg, " " ) Then Arg = """" & Arg & """"
Str = Str & " " & Arg
Next
CreateObject( "WScript.Shell" ).Run _
"cscript //nologo //I """ & _
WScript.ScriptFullName & _
""" " & Str
WScript.Quit
End If
End Sub
' Emulate pause CMD
Sub Pause(strPause)
WScript.Echo (strPause)
z = WScript.StdIn.Read(1)
End Sub
' ******* MAIN ***
forceCScriptExecution
' Get last options used
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
strLastOptionFile = strHomeFolder & "\.pdf2pdfocr.txt"
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objFile = objFSO.OpenTextFile(strLastOptionFile, ForReading)
If Err.Number <> 0 Then
lastOptionUsed = ""
Else
lastOptionUsed = objFile.ReadAll
objFile.Close
End If
On Error Goto 0
' Set up paths
python_venv_path = strHomeFolder & "\pdf2pdfocr-venv\Scripts\python"
pdf2pdfocr_path = strHomeFolder & "\pdf2pdfocr-venv\Scripts\pdf2pdfocr.py"
' Get actual options from script to show help
helpOut = execCommand(python_venv_path & " " & pdf2pdfocr_path & " --help")
WScript.StdOut.WriteLine("Please enter options.")
default_option = "-stp -j 0.9"
WScript.StdOut.WriteLine("Use <Enter> for default [" & default_option & "] or <.> for last used option [" & lastOptionUsed & "].")
WScript.StdOut.Write(">> ")
WScript.StdIn.Read(0)
options = WScript.StdIn.ReadLine()
RewriteLastOptionFile = True
if options = "" then
options = default_option
RewriteLastOptionFile = False
end if
if options = "." then
options = lastOptionUsed
RewriteLastOptionFile = False
end if
if RewriteLastOptionFile Then
Set objFileW = objFSO.CreateTextFile(strLastOptionFile)
objFileW.Write(options)
objFileW.Close
end if
' Call pdf2pdfocr script to all files passed as arguments
set objArgs = WScript.Arguments
for i = 0 to objArgs.Count - 1
WScript.Echo "Processing " & objArgs(i) & " ..."
scriptOut = execCommand(python_venv_path & " " & pdf2pdfocr_path & " " & options & " -i """ & objArgs(i) & """")
WScript.Echo "---------------------------------------"
next
Pause("Press Enter to continue...")