Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Determining if file is being executed by IE or mshta.exe (hta issue)

14 views
Skip to first unread message

Scott Marquardt

unread,
May 1, 2004, 12:07:36 AM5/1/04
to
I'm writing some files that I'd like to run either with IE or MSHTA. I'm
using them for dual purposes, and I'd like users to be able to launch them
with mshta.exe when I need them to write to the registry.

But my question is this: what code can I use to identify which context it's
running in? What's the dirt simplest way -- short of just trying something
that would fail in IE but succeed in mshta and trap the error -- to do
this?

--

Scott

Almost summer! http://snipurl.com/trebuchet

Michael Harris (MVP)

unread,
May 1, 2004, 1:19:35 PM5/1/04
to
> I'm writing some files that I'd like to run either with IE or MSHTA.
> I'm using them for dual purposes, and I'd like users to be able to
> launch them with mshta.exe when I need them to write to the registry.
>
> But my question is this: what code can I use to identify which
> context it's running in? What's the dirt simplest way -- short of
> just trying something that would fail in IE but succeed in mshta and
> trap the error -- to do this?

Check the hta:application object for the commandLine property which will be
present only if mshta.exe is the host...

The check is much more elegant is JScript...

<html>
<head>
<hta:application id=myHta />
<script language="javascript">

isHTA = myHta.commandLine?true:false;
alert(isHTA);

</script>
</head>
<body>
</body>
</html>

than it is in VBScript...

<html>
<head>
<hta:application id=myHta />
<script language="vbscript">

isHTA = not isnull(myHta.getAttribute("commandLine"))
msgbox isHTA

</script>
</head>
<body>
</body>
</html>

--
Michael Harris
Microsoft.MVP.Scripting
Sammamish WA US

Scott Marquardt

unread,
May 1, 2004, 2:13:52 PM5/1/04
to
Michael Harris (MVP) opined thusly on May 1:

>> I'm writing some files that I'd like to run either with IE or MSHTA.
>> I'm using them for dual purposes, and I'd like users to be able to
>> launch them with mshta.exe when I need them to write to the registry.
>>
>> But my question is this: what code can I use to identify which
>> context it's running in? What's the dirt simplest way -- short of
>> just trying something that would fail in IE but succeed in mshta and
>> trap the error -- to do this?
>
> Check the hta:application object for the commandLine property which will be
> present only if mshta.exe is the host...
>
> The check is much more elegant is JScript...
>
> <html>
> <head>
> <hta:application id=myHta />
> <script language="javascript">
>
> isHTA = myHta.commandLine?true:false;
> alert(isHTA);

That was precisely what I needed, and as simple as I'd hoped. Thank you
very much!

So often I've observed that your posts are on the money. What resources
would you recommend for anyone wanting to get on top of his game with
scripting -- including all the administrative uses to which we put it in
our ADSI, WMI, ADO (etc.) worlds? Do you have a definitive list of top
sites, books, and so forth?

And hey -- what's your favorite script editor/debugger?
[if he says "powered by notepad" I shall despair and retire from all
matters technical ;-]

Michael Harris (MVP)

unread,
May 1, 2004, 2:50:01 PM5/1/04
to
> That was precisely what I needed, and as simple as I'd hoped. Thank
> you very much!

You are very welcome ;-)...

>
> So often I've observed that your posts are on the money. What
> resources would you recommend for anyone wanting to get on top of his
> game with scripting -- including all the administrative uses to which
> we put it in our ADSI, WMI, ADO (etc.) worlds? Do you have a
> definitive list of top sites, books, and so forth?
>

I don't have a list of books I could post, but I have a little library of
about 30-40 titles that I've read over the last 5-6 years. Most are titles
from Wrox Press (published in their better days). I don't really use any
for reference anymore - I bought/read them to grasp concepts, not to
memorize syntax...

Two that do a really good job of painting the 'big picture' are the VBScript
Programmer's Reference (Wrox) and the Microsoft® Windows®2000 Scripting
Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

My most visited site...

MSDN Library
http://msdn.microsoft.com/library/

My most used local resources...

WSH 5.6 documentation download
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en

TechNet Script Center Sample Scripts
Download in HTML Help format (searchable)
http://www.microsoft.com/downloads/release.asp?ReleaseID=38942


> And hey -- what's your favorite script editor/debugger?
> [if he says "powered by notepad" I shall despair and retire from all
> matters technical ;-]

I use EditPlus for the one-off 'quick-and-dirty' (personal) stuff, and
PrimlaScript for the important stuff (anything needing production
quality)...

Here's a list of the script editors I've used (or at least tried out) at one
time or another...

ConTEXT http://www.fixedsys.com/context freeware
CREdit http://www.praven3.com/credit/ freeware
EditPad http://www.editpadpro.net freeware/shareware
EditPlus http://www.editplus.com shareware
NoteTab http://www.notetab.com freeware/shareware
Notepad2 http://www.flos-freeware.ch/notepad2.html freeware
PrimalScript http://www.sapien.com/primalscript.htm commercial
PSPad www.pspad.com freeware
TextPad is http://www.textpad.com shareware
UltraEdit http://www.ultraedit.com shareware

VbsEdit/JsEdit http://www.adersoft.com shareware
WEB-ED http://www.jsware.net shareware

As for debuggers, I use the Microsoft Script Editor's debugger. MSE comes
as an optional component install with Office applications. I also have both
VS6 and VS.Net installed, but don't use them for writing/debugging script.

Scott Marquardt

unread,
May 1, 2004, 3:58:15 PM5/1/04
to
Michael Harris (MVP) opined thusly on May 1:

> As for debuggers, I use the Microsoft Script Editor's debugger. MSE comes


> as an optional component install with Office applications. I also have both
> VS6 and VS.Net installed, but don't use them for writing/debugging script.

Doggonit, I used to know how to execute that sucker, I swear. Where the
deuce...

What I lament is the lack of anything that does breakpoints well AND
exposes the values of variables and arrays. I guess that would be the VS
IDE. ;-) I refuse to use that for scripting!

Thanks for the references. I've done most of my scripts in vbscript over
the years (to match being a VB guy only lately converted to C#), but late
last year I swore I'd only write in jscript from now on -- not that
exigencies haven't sometimes thrown me back on familiar resources. You
cited a definitive text on vbscript -- anything for a guy who wants to
seriously master jscript? Sure seems potent by comparison.

Michael Harris (MVP)

unread,
May 1, 2004, 5:05:55 PM5/1/04
to
> ... . You cited a definitive text on vbscript -- anything for a

> guy who wants to seriously master jscript? Sure seems potent by
> comparison.


I've personally learned JScript strictly from the documentation --- never
had a book on JavaScript.

IMHO, choice of language is secondary to mastering the object models. No
matter what language you choose, you'll always find that some other language
has syntactic sugar that makes some specific task easier, or more elegant,
or more <insert favorite superlative adjective here>.

If you don't understand the object models, it matters little which language
you are using...

But then, if you *do* understand the object models, it matters little which
language you are using...

Scott Marquardt

unread,
May 1, 2004, 8:43:57 PM5/1/04
to
Michael Harris (MVP) opined thusly on May 1:

> If you don't understand the object models, it matters little which language


> you are using...
>
> But then, if you *do* understand the object models, it matters little which
> language you are using...

A geek koan! LOL

--

Scott "off topic now" Marquardt

Almost summer! http://snipurl.com/trebuchet

0 new messages