On Sun, 5 Mar 2023 13:28:54 -0800 (PST), John Stockton wrote:
>
> So : is there an obscure but standard small part of Windows 10+ that can
> display a text file in a simple non-editable Window that supports Ctrl-C
> and vertical scroll? Notepad would do nicely, if it could be opened in a
> non-edit mode.
You can use HTML Application (mshta.exe).
> Can VBScript or JScript called from batch create such a Window _easily_?
If you meant VBScript/JScript code, then no. Because thowe code may contain
batch file's special characters. Any of those special characters may need to
be quoted and/or escaped.
> Ideally, but certainly not essentially, I would like the displayed text
> to ge on a light-coloured background !
This is using HTA with JScript.
@echo off
setlocal
rem pass a text file's name as batch file argument
if "%~1" == "" goto :eof
set "tf=%temp%\viewer.hta"
set "fn=%~f1"
>"%tf%" (
echo ^<title^>%fn%^</title^>^<style^>
echo body{margin:0;height:100%%;overflow:hidden}
echo #t{border:0;padding:.2em .4em;width:100%%;height:100%%}
echo ^</style^>^<textarea id=t readonly^>^</textarea^>^<script^>
echo t.innerText=(new ActiveXObject('scripting.filesystemobject'
echo ^)^).openTextFile('%fn:\=\\%'^).readAll(^)
echo ^</script^>
)
start /wait mshta.exe "%tf%"
del "%tf%"
As you can see, the `<`, `>`, `)`, and literal `%` characters in both the
JScript and HTML code, are escaped since they're part of batch file's
special characters.
The `)` doesn't actually need to be escaped if the character is not within
batch file's command group (which uses parentheses). But the `)` in that
code is within a command group.
The above batch file uses temporary file for the .hta file. Without using
any temporary file is possible, but the whole HTML code must be specified in
MSHTA's command line as one line - which can be quite long. e.g.
@echo off
setlocal
rem pass a text file's name as batch file argument
if "%~1" == "" goto :eof
set "fn=%~f1"
start /wait mshta.exe "javascript:'<title>%fn:\=\\%</title><style>body{margin:0;height:100%%;overflow:hidden}#t{border:0;padding:.2em .4em;width:100%%;height:100%%}</style><textarea id=t readonly></textarea><script>t.innerText=(new ActiveXObject(\'scripting.filesystemobject\')).openTextFile(document.title).readAll()</script>'"
The `<`, `>`, and `)` characters in above code doesn't need to be escaped,
because they're already wrapped in double-quotes. However, the literal `%`
characters still need to be escaped.
The `'` (single-quote) and `\` characters need to be escaped for the JScript
code's context - which I think you already know why, since you're
occasionally dwells in comp.lang.javascript.
MSHTA is the most direct way to excute VBScript/JScript code. Windows Script
Host (WSH/wscript.exe/cscript.exe) requires a file to execute
VBScript/JScript code. MSHTA JScript e.g.:
mshta.exe "javascript:alert('hello world')"