summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/psviewer/psviewer.vbs
blob: 651093c46562fda3dbbaa0919e4866b20710d411 (plain)
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
' Written by Siep Kroonenberg in 2020 and placed in the Public Domain

option explicit
On Error Resume next

dim oWsh, oFS, sTmp, oArgs, f, fname, tf, i, msg

set oWsh = wscript.createobject( "wscript.Shell" )
Set oFS = CreateObject("Scripting.FileSystemObject")
sTmp=oWsh.ExpandEnvironmentStrings("%Temp%")

Set oArgs = wscript.arguments
If oArgs.count = 0 Then
  msg = "Psviewer is a simple script which converts its argument to a " & _
  " temporary pdf and displays it in the default pdf viewer." _
  & vbcrlf & vbcrlf & _
  "Double-clicking an .eps- or .ps file should result in viewing the " & _
  "converted file. If this does not work, then try right-click and " & _
  "'Open with', which should give you the option to set psviewer as " & _
  "default program for .[e]ps files."
  MsgBox msg, 0, "Psv: no argument"
  wscript.quit
End If
f = oArgs( 0 )
fname = oFS.getfile( f ).Name

Randomize

' find a name for a new temporary pdf file
i = 0
do
  tf = sTmp & "\" & fname & "-" & Int(100000 * Rnd) & ".pdf"
  i = i + 1
  If Not oFS.FileExists( tf ) then
    Exit do
  else
    tf = ""
    if i >= 500 Then
      Exit Do
    End If
  End If
Loop
If tf = "" Then
  wscript.echo "Cannot create temporary pdf"
  wscript.quit
End If

' create temporary pdf
If LCase( Right( fname, 4 )) = ".eps" Then
  If oWsh.run( "kpsewhich -format texmfscripts epstopdf.pl", 0, true ) = 0 Then
    oWsh.run "epstopdf """ & f & """ """ & tf & """", 0, true
  Else
    oWsh.run "gswin32c -q -dNOPAUSE -dBATCH -P- -dSAFER -sDEVICE#pdfwrite -dEPSCrop ""-sOutputFile#" & tf & """ -f """ & f & """", 0, true
  End if
Else
  oWsh.run "gswin32c -q -dNOPAUSE -dBATCH -P- -dSAFER -sDEVICE#pdfwrite ""-sOutputFile#" & tf & """ -f """ & f & """", 0, true
End If

' open temporary pdf
If oFS.fileexists(tf) Then
  oWsh.run( """" & tf & """" )
Else
  MsgBox f & " could not be converted," & vbcrlf & _
  "is probably not valid PostScript", 0, "Error"
End If