texlive[71293] Master/texmf-dist/scripts/psviewer: Psviewer: replaced

commits+siepo at tug.org commits+siepo at tug.org
Sat May 18 22:13:29 CEST 2024


Revision: 71293
          https://tug.org/svn/texlive?view=revision&revision=71293
Author:   siepo
Date:     2024-05-18 22:13:29 +0200 (Sat, 18 May 2024)
Log Message:
-----------
Psviewer: replaced vbscript with powershell

Added Paths:
-----------
    trunk/Master/texmf-dist/scripts/psviewer/psviewer.ps1

Removed Paths:
-------------
    trunk/Master/texmf-dist/scripts/psviewer/psviewer.vbs

Added: trunk/Master/texmf-dist/scripts/psviewer/psviewer.ps1
===================================================================
--- trunk/Master/texmf-dist/scripts/psviewer/psviewer.ps1	                        (rev 0)
+++ trunk/Master/texmf-dist/scripts/psviewer/psviewer.ps1	2024-05-18 20:13:29 UTC (rev 71293)
@@ -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

Deleted: trunk/Master/texmf-dist/scripts/psviewer/psviewer.vbs
===================================================================
--- trunk/Master/texmf-dist/scripts/psviewer/psviewer.vbs	2024-05-18 20:10:37 UTC (rev 71292)
+++ trunk/Master/texmf-dist/scripts/psviewer/psviewer.vbs	2024-05-18 20:13:29 UTC (rev 71293)
@@ -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



More information about the tex-live-commits mailing list.