JScript - is a file of a known Type?

  • Thread starter Dr J R Stockton
  • Start date
D

Dr J R Stockton

In comp.lang.javascript message <4924e3fd-a25a-4cbe-8410-6f1c7f6984e4@r5
g2000yqc.googlegroups.com>, Thu, 5 Jan 2012 22:01:48, "Michael Haufe
(TNO) said:
Generally not as there is not a bijection between the TYPE and its
registry entry. One would have to use the file extension as a key to
lookup the existence of an entry to get more accurate results.

Well, the extension can be obtained from the File Object, and the method
in the code that you cite is certainly direct enough. All I need do is
to take your try/catch and adapt it for the target context, in
<http://www.merlyn.demon.co.uk.programs/32-bit/seakfyle.js>.

You have a habit, I think, of getting to the core of the matter and
giving an appropriate answer.
Here is some code that creates a similar result on my machine:

<https://gist.github.com/1569247>

Hence, installed and working,

function Haufe(It) {
var fName = It.Name,
ext = fName.slice(fName.lastIndexOf(".")).toLowerCase()
try { WshShell.RegRead("HKCR\\"+ext+"\\") ; return true }
catch(e) { return false }
}

However, it returns true for extension $$$, which needs thinking about.

Also, your way of getting the extension is eleven characters shorter
than what I had.

Thanks.
 
M

Michael Haufe (TNO)

In comp.lang.javascript message <847ebf0b-4938-4a1e-a108-964535d3d853@z1
2g2000yqm.googlegroups.com>, Thu, 5 Jan 2012 22:05:53, "Michael Haufe


Yes.

The original article in the thread was concerned with one particular
point.  That example was partly from what I already have, with the third
column by hand.  It will be to produce something similar but with other
information.

So did my sample code work help at all?
But the core of the situation is that all CScript output is through

        function Right(N, Str) { N = Hush[N]
          if (N == "0") return
          if (N == "1") WScript.StdOut.Write(Str)
          if (N == "2") WScript.StdErr.Write(Str)
          if (N == "3") txtStreamOut.Write(Str)
          }

in which almost all Str end with a newline; and I want to use

          if (N == "4") SOMETHING(Str)

[ after having detected (which I can do) WScript ] to get a
corresponding display.

If that can be done, the application is no longer just a CScript
application usable only by those who can find and use a DOS-like command
line, but it is usable also by those who only use GUI routines.

So to check if I understand correctly, you want a function that will
take a collection of results in the format of:

FILENAME TYPE KNOWN

and display them as a formatted table in CScript or as an HTML table
in a generated HTA in WScript?
 
D

Dr J R Stockton

In comp.lang.javascript message <a46fb17c-5638-4b1a-8d87-034ca257435e@p1
6g2000yqd.googlegroups.com>, Sun, 8 Jan 2012 14:52:24, "Michael Haufe
(TNO) said:
So did my sample code work help at all?

Completely. But with the apparent peculiarity that extension $$$ (my
editor's backup) is known to the registry, although the corresponding
..Type string is "$$$ File", similar to that for a file with extension
OK4 which is not so known.

Aside to non-Anglos :
What would the .Type property be for an unregistered file in
systems set for other well-known Western European languages?
E.g. Fichier OK4 / OK4 Fichier, etc.? Users are required to be
able to read instructions in English, but should not be required
to have an English-speaking Windows.

But the core of the situation is that all CScript output is through

        function Right(N, Str) { N = Hush[N]
          if (N == "0") return
          if (N == "1") WScript.StdOut.Write(Str)
          if (N == "2") WScript.StdErr.Write(Str)
          if (N == "3") txtStreamOut.Write(Str)
          }

in which almost all Str end with a newline; and I want to use

          if (N == "4") SOMETHING(Str)

[ after having detected (which I can do) WScript ] to get a
corresponding display.

If that can be done, the application is no longer just a CScript
application usable only by those who can find and use a DOS-like command
line, but it is usable also by those who only use GUI routines.

So to check if I understand correctly, you want a function that will
take a collection of results in the format of:

FILENAME TYPE KNOWN

and display them as a formatted table in CScript or as an HTML table
in a generated HTA in WScript?

Not really; you are going further that I wish for.

I wanted a routine that would accept text, a line at a time (P.S. to be
strictly accurate, these lines could contain newlines), and cause it to
appear as it would in Notepad. The example is tabular, but other lines
are not. To illustrate, here's a command-prompt screen capture :

prompt>seek . n
:: SEAKFYLE.JS >= 2012-01-09, at 2012-01-09 20:21:56, see SEAKFYLE.HTM
2012-01-09 17:40:34 33275 .....a.. "SEAKFYLE.$$$"
2012-01-09 17:41:00 33291 .....a.. "SEAKFYLE.HTM"
2012-01-09 17:38:37 17603 .....a.. "SEAKFYLE.JS"
| Date/time now : 2012-01-09 20:21:56
| Find : 3 matches
| Finding in : C:\WSH\SEEK ; 31 ms


If I do the following command line, nothing appears in the screen before
the next prompt, as Q sets all output to Channel 3 which is sent To file
xyz.

prompt>seek . n Q333333 Txyz

and file xyz. can be opened in Notepad (except for needing an 0D before
each 0A, which can be dealt with (most easily by using WFVIEW instead)).

Aside : AFAICS, no GUI editor or viewer that I have can AFAIK accept
piped-in text, which is a pity.

What I want to end up with is using essentially the same code as I have
at present, but as a GUI application - instead of opening a Command
Prompt and giving it a Command Line, to open a GUI window and give it,
with some equivalent of a JavaScript prompt(), the commands. Or just
command-line in, GUI out.

I forgot to say that the GUI needs to show each line as it is output
from the current code, without waiting for the stream to complete.

The alternative seems to be to use an HTA, treating my existing code as
an include file of functions and variables, and suppressing any unwanted
parts. That should be easy, if I learn about HTAs first - - - which I
seem to have done.

It's now fundamentally working in an HTA, but the adapting needs to be
cleaned up, though not tonight.
 
T

Todd Vargo

On 1/9/2012 6:45 PM, Dr J R Stockton wrote:
Aside : AFAICS, no GUI editor or viewer that I have can AFAIK accept
piped-in text, which is a pity.

What I want to end up with is using essentially the same code as I have
at present, but as a GUI application - instead of opening a Command
Prompt and giving it a Command Line, to open a GUI window and give it,
with some equivalent of a JavaScript prompt(), the commands. Or just
command-line in, GUI out.

I forgot to say that the GUI needs to show each line as it is output
from the current code, without waiting for the stream to complete.

The vbscript code that I posted does that. To set a fixed pitch font,
change <body> tag to <body STYLE=""font:10 pt courier"">

If desired. you can easily code it to receive input through stdin, but
that usually implies user operation from the command line.
 
D

Dr J R Stockton

The alternative seems to be to use an HTA, treating my existing code as
an include file of functions and variables, and suppressing any unwanted
parts.  That should be easy, if I learn about HTAs first - - - which I
seem to have done.

It's now fundamentally working in an HTA, but the adapting needs to be
cleaned up, though not tonight.

Rather easy. Essentially, the HTA has a button and a textarea. The
existing JScript for WSH is modified so that the code is just
declarations of globals and functions, followed by a call of the main
function :-

if (typeof window == "undefined") SEAKFYLE()

In CScript, that call calls, and all still works as before; in HTA,
that call does not call, but the button does it instead.

The HTA "include"s SEAKFYLE.JS, and also contains script

Fawm = document.forms[0]

SEAKFYLE.JS uses the value of Fawm to write to the textarea, and the
truth of Fawm for minor code changes, such as reading the command line
from an "<input type=text" in the HTA.

<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.htm>
<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.js>
<http://www.merlyn.demon.co.uk/programs/32-bit/seek.hta>
<http://www.merlyn.demon.co.uk/programs/32-bit/seek.bat>
<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.zip>

but please take copies for repeated use.



Query : is there an easy way to resize the textarea vertically when
the HTA window is resized, so that the form comes within a gnat's
whisker (0.5 ex, or a few px) of the bottom of the window, and if so
what is it?
 
M

Michael Haufe (TNO)

Rather easy. Essentially, the HTA has a button and a textarea. The
existing JScript for WSH is modified so that the code is just
declarations of globals and functions, followed by a call of the main
function :-

if (typeof window == "undefined") SEAKFYLE()

In CScript, that call calls, and all still works as before; in HTA,
that call does not call, but the button does it instead.

The HTA "include"s SEAKFYLE.JS, and also contains script

Fawm = document.forms[0]

SEAKFYLE.JS uses the value of Fawm to write to the textarea, and the
truth of Fawm for minor code changes, such as reading the command line
from an "<input type=text" in the HTA.

<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.htm>
<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.js>
<http://www.merlyn.demon.co.uk/programs/32-bit/seek.hta>

Note that HTML Applications can take command like arguments as well by
using windows.dialogArguments

More info should be here:
<http://msdn.microsoft.com/en-us/library/ms536496(v=vs.85).aspx>

The WScript object is not available in an HTA. Alternative methods
must be used.

Why do you want/need a *.BAT file? If *.js is registered to open with
Cscript.exe or Wscript.exe, then it would be sufficient to simply type
SEAKFYLE.JS at the command prompt. Passing arguments to the script can
be managed by the WScript.Arguments collection:

<http://www.devguru.com/technologies/wsh/quickref/
wscript_arguments.html>
<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.zip>

but please take copies for repeated use.

Query : is there an easy way to resize the textarea vertically when
the HTA window is resized, so that the form comes within a gnat's
whisker (0.5 ex, or a few px) of the bottom of the window, and if so
what is it?

Perhaps this might help:
<https://gist.github.com/1592291>
 
D

Dr J R Stockton

In comp.lang.javascript message <67023a25-650b-454f-a915-ebeae30ccf6e@f1
1g2000yql.googlegroups.com>, Tue, 10 Jan 2012 17:04:55, "Michael Haufe
(TNO) said:
Rather easy. Essentially, the HTA has a button and a textarea. The
existing JScript for WSH is modified so that the code is just
declarations of globals and functions, followed by a call of the main
function :-

if (typeof window == "undefined") SEAKFYLE()

In CScript, that call calls, and all still works as before; in HTA,
that call does not call, but the button does it instead.

The HTA "include"s SEAKFYLE.JS, and also contains script

Fawm = document.forms[0]

SEAKFYLE.JS uses the value of Fawm to write to the textarea, and the
truth of Fawm for minor code changes, such as reading the command line
from an "<input type=text" in the HTA.

<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.htm>
<http://www.merlyn.demon.co.uk/programs/32-bit/seakfyle.js>
<http://www.merlyn.demon.co.uk/programs/32-bit/seek.hta>

Note that HTML Applications can take command like arguments as well by
using windows.dialogArguments

That's well worth knowing. I could use that to pre-load the input line;
and I can add an option, command-line only, to auto-start after pre-
load. ... Except that neither windows nor window.dialogArguments appears
to exist. ... Needs <hta:application ID="oHTA" and then
alert(oHTA.commandLine) shows the command line.


More info should be here:
<http://msdn.microsoft.com/en-us/library/ms536496(v=vs.85).aspx>

The WScript object is not available in an HTA. Alternative methods
must be used.

I think that, in HTA mode, no path of execution should now lead to a
WScript. Because of the heritage of the "design" of the code, detected
errors lead to a chain of function calls used as GOTOs, since they ended
up in the original Pascal with a Halt(N) call. In WSH, WScript.Quit(N)
does the same. I'd like to use something similar in HTA mode; but the
only way of stopping that I know is to exit all routines and fall off
the end of the script, which would be tiresome to arrange.

That problem is now circumvented.

Why do you want/need a *.BAT file? If *.js is registered to open with
Cscript.exe or Wscript.exe, then it would be sufficient to simply type
SEAKFYLE.JS at the command prompt.

I prefer to change such defaults as little as possible. I'd have to
remember to do it on other machines, and other users would also need to
do it. There could be other *.js files needing to run in WScript. But
I've added the possibility to the description.

Passing arguments to the script can
be managed by the WScript.Arguments collection:

I'm using WScript.Arguments already.


It is rather too much changed from a version that I no longer have to
import, or to be easy to follow.

Having seen it working, aesthetics demand that the textarea is sized to
fill the rest of the form, and the form is sized to only nearly reach
the bottom of the viewport. But @FrmMain margin-bottom: 5px; does
that.

Resizing then is nice, or will be once I've trapped the error that
occurs when the bottom is dragged up too far. This does it :
TA.style.height = Math.max(0, TA.parentNode.offsetHeight - 10) + "px";

The <H3> wastes height !! I'll stick with <big> or equivalent.

The ROWS control seems not to work; but it has no useful function with
auto-resizing. Removed.

The SEEK button failed, because of its re-naming, fixed.

I was somewhat surprised that Fawm = document.forms[0]; in the HEAD
works when the actual form is in the body.

There is at present one remaining infelicity: the heading (and other)
text, when wrapped because of Zoom or narrowing the window, wraps under
the following element. That's now mitigated by shortening the heading
line.

If IE gets Zoom Text Only, I suppose the vertical sizing will have to be
redone in ex or em units.


The version uploaded at about 23:40 GMT, dated 2012-01-12, includes all
this. More thanks.
 
M

Michael Haufe (TNO)

On Jan 12, 5:43 pm, Dr J R Stockton <[email protected]>
wrote:
... Except that neither windows nor window.dialogArguments appears
to exist. ... Needs   <hta:application ID="oHTA"  and then
alert(oHTA.commandLine) shows the command line.

Indeed, I was staring at the wrong msdn page (not to mention the
typo).
The version uploaded at about 23:40 GMT, dated 2012-01-12, includes all
this. More thanks.

You're welcome
 
M

Michael Haufe (TNO)

In comp.lang.javascript message <67023a25-650b-454f-a915-ebeae30ccf6e@f1
1g2000yql.googlegroups.com>, Tue, 10 Jan 2012 17:04:55, "Michael Haufe
(TNO)" <[email protected]> posted:


That's well worth knowing.  I could use that to pre-load the input line;
and I can add an option, command-line only, to auto-start after pre-
load. ... Except that neither windows nor window.dialogArguments appears
to exist. ... Needs   <hta:application ID="oHTA"  and then
alert(oHTA.commandLine) shows the command line. ....
The version uploaded at about 23:40 GMT, dated 2012-01-12, includes all
this.  More thanks.


Since I was correct only in recalling the idea and not the API, it
would be nice if seakfyle.js didn't perpetuate my bad advice at the
bottom. A generic attribution or nothing at all would be fine with me.
 
D

Dr J R Stockton

Since I was correct only in recalling the idea and not the API, it
would be nice if seakfyle.js didn't perpetuate my bad advice at the
bottom. A generic attribution or nothing at all would be fine with
me.

Removed, some while ago now!

You'll probably have noticed how I am, for the time being, indicating
what I've changed in your HTA; for example, auto-start is available
from the SEEK.HTA command line, and the layout is tighter. Also,
SEEK.BAT is no longer limited to 8 Options.

Instead of using WScript.Quit at the end of performing a non-
continuable Option, I now set a Boolean to indicate that the option is
wanted. The Boolean can then be tested in the main routine; if set,
the Option routine is called and returns, and from there it is easy to
in effect GOTO near the end of the main routine.

SEAKFYLE.HTM has a list of what that I use in CScript mode I have not
yet been able to do in HTA mode.

For some inscrutable reason, my newsreader seems to think that this
thread, or the pair of us, is kill-ruled. Unless I recall the
possibility at the right time, that delays getting the item as a News
article to where I like to reply from. Therefore, see sig.

Thanks for your continued help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,073
Messages
2,570,539
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top