diff options
-rw-r--r-- | Master/texmf-dist/scripts/psviewer/psviewer.ps1 | 78 | ||||
-rw-r--r-- | Master/texmf-dist/scripts/psviewer/psviewer.vbs | 66 |
2 files changed, 78 insertions, 66 deletions
diff --git a/Master/texmf-dist/scripts/psviewer/psviewer.ps1 b/Master/texmf-dist/scripts/psviewer/psviewer.ps1 new file mode 100644 index 00000000000..9976970f42f --- /dev/null +++ b/Master/texmf-dist/scripts/psviewer/psviewer.ps1 @@ -0,0 +1,78 @@ +# Written by Siep Kroonenberg in 2024 and placed in the Public Domain. +# This is a Powershell version of psviewer.vbs, created because vbscript +# Since vbscript is no longer a guaranteed component of windows, +# I created this powershell version. + +Add-Type -AssemblyName PresentationFramework + +# note. win10 has PS version >= 5 + +if ($args.count -lt 1) { + $msg = @" +Psviewer is a simple script which converts its argument to a +temporary pdf and displays it in the default pdf viewer. + +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 pick psviewer and +set it as default program for .[e]ps files. +"@ + [System.Windows.MessageBox]::Show($msg, 'Missing argument', 'OK', 'Hand') + exit 1 +} +# powershell makes it difficult to enter parameters with spaces, +# e.g. double quotes won't do. So I simply assume all the pieces +# are intended as one filename: +$f = $args -join ' ' +if (-not (test-path -path $f -pathtype leaf)) { + [System.Windows.MessageBox]::Show( + "$f does not exist", 'Error', 'OK', 'Hand') + exit 1 +} +$f = $f.replace("\", "/") +$p = split-path -path $f -leaf -resolve + +# find suitable name for to be generated pdf. +# do it manually; no options for filename templates +# with the new-temporaryfile cmdlet +$i=0 +$success = $false +do { + $i++ + $r = get-random -min 0 -max 1000000 + $pdf = $("{0:000000}" -f $r) + # we want to see the original filename in the viewer's titlebar + $pdf = $p + '___' + $pdf + '.pdf' + # new-item will fail if $pdf already exists + $success = (new-item -path "$env:temp" -name $pdf -ea "ignore") +} until ($success -or ($i -ge 500)) +if (-not $success) { + [System.Windows.MessageBox]::Show( + "Could not create temporary pdf", 'Error', 'OK', 'Hand') + exit 1 +} +$pdf = "${env:temp}\$pdf" +$pdf = $pdf.replace("\", "/") + +# create the temporary pdf, which is as yet just an empty file +# exit codes are not always reliable; test instead for non-empty output +if ($p -ilike "*.eps") { + if (kpsewhich -format texmfscripts epstopdf.pl) { + epstopdf "$f" "$pdf" + } + if ((get-item $pdf).length -eq 0) { + rungs -q -dNOPAUSE -dBATCH -P- -dSAFER -sDEVICE#pdfwrite -dEPSCrop -sOutputFile#"$pdf" -f "$f" + } +} +if ((get-item $pdf).length -eq 0) { + # eps treatment has failed or has not been attempted + rungs -q -dNOPAUSE -dBATCH -P- -dSAFER -sDEVICE#pdfwrite -sOutputFile#"$pdf" -f "$f" +} + +if ((get-item $pdf).length -eq 0) { + [System.Windows.MessageBox]::Show( + "Could not convert to temporary pdf; invalid PostScript?", 'Error', 'OK', 'Hand') + exit 1 +} +invoke-item -path $pdf +exit 0 diff --git a/Master/texmf-dist/scripts/psviewer/psviewer.vbs b/Master/texmf-dist/scripts/psviewer/psviewer.vbs deleted file mode 100644 index bd5f4a39394..00000000000 --- a/Master/texmf-dist/scripts/psviewer/psviewer.vbs +++ /dev/null @@ -1,66 +0,0 @@ -' Written by Siep Kroonenberg in 2020 and placed in the Public Domain -' 2023: Adaptation to 64bit - -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 "gswin64c -q -dNOPAUSE -dBATCH -P- -dSAFER -sDEVICE#pdfwrite -dEPSCrop ""-sOutputFile#" & tf & """ -f """ & f & """", 0, true - End if -Else - oWsh.run "gswin64c -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 |