Stuff
OS/400 Edition
Volume 1, Number 10 -- May 23, 2002

JavaScript and the SHELL Command


by Shannon O'Donnell

If you're an RPG programmer and you're just now getting into writing HTML, chances are you've discovered a lack of functionality that you used to have in RPG--specifically, the ability to execute commands or call other programs from HTML. Because HTML runs in a browser, and because of the inherent, built-in security of browsers, it's difficult to call other programs from within HTML code. But sometimes you need to. What's the solution? Java Script and SHELL!

The SHELL Command

Visual Basic and C++ programmers have known about the SHELL command for years. SHELL is a Microsoft Win32 API that allows Windows programmers to execute commands from a Shell or command line. The SHELL API, like all Win32 APIs, is not easy to use--at least not for non-Windows programmers. However, you don't have to understand all of its syntax and features in order to use it. You just have to know how to use it.

In this article, I'll show you how to use the SHELL API from within HTML to perform several common functions. Since you want to know more about how to actually use it from your own HTML applications, and probably couldn't care less about the syntax and background of the SHELL command, I'm going to skip the history lesson. If you want to learn more about the syntax of the SHELL command, take a look at the Microsoft's Web site. Just search on SHELLEXECUTE, and you'll find pages and pages about the SHELL API, its syntax, and how to use it.

SHELL is an ActiveX API. That is, SHELL has certain properties and methods that are exposed, or are public, which you can access from your own applications. SHELL also follows the Component Object Model, or COM, method of access, which means, among other things, that it has certain standard behaviors. You can take advantage of these standard behaviors in your own HTML applications. To do so, you'll need to write a little bit of JavaScript.

JavaScript and SHELL

If you've read some of the other JavaScript articles in Midrange Programmer, you're probably pretty proficient at using JavaScript by now. If you're not, you may want to go back and read those articles again, just to get familiar with it. For this article, we're going to write some JavaScript in order to demonstrate how to use the SHELL ActiveX object to execute commands on your PC.

To start, open up Microsoft Notepad.exe and create a new HTML document.

<HTML>
<HEAD>

</HEAD>
<BODY>
<FORM name="Form1">
<CENTER>
<BR><BR>
<H1>Execute PC Commands From HTML </H1>
<BR><BR> 
<File Name to Open:> <Input type="text" 
   name="filename"/>
<BR><BR>
<Input type="Button" name="Button1" 
   value="Run Notepad.exe" />
<BR><BR>
<Input type="Button" name="Button2" 
   value="Run Notepad.exe with Parameters" />
</CENTER>
</BODY>
</FORM>
</HTML>

Save this document with a name like DemoSHELLAPI.htm and then open it in a browser. It should look something like the one shown here:


At this point, your HTML code doesn't do anything, because you haven't added any code behind it. Let's do that now.

Adding the Power

To use the SHELL API, you'll need to write a JavaScript function and define the SHELL API parameters within it. And you'll need to execute the function for when the user clicks a button.

Modify the DemoSHELLAPI.htm file to add the following code between the opening and closing HEAD tags:

<SCRIPT type="text/javascript" LANGUAGE="JavaScript">
  function executeCommands(inputparms)
   {
  // Instantiate the Shell object and invoke 
   its execute method.

    var oShell = new ActiveXObject("Shell.Application");

    var commandtoRun = "C:\\Winnt\\Notepad.exe";
    if (inputparms != "")
     {
      var commandParms = document.Form1.filename.value;
     }

 // Invoke the execute method.  
     oShell.ShellExecute(commandtoRun, commandParms, 
	  "", "open", "1");
  }
</SCRIPT>

Modify your buttons, in the BODY of the text to add the 
   "onClick" event to each.

<input type="Button" name="Button1" 
   value="Run Notepad.exe" onClick="executeCommands()" />

<input type="Button" name="Button2" 
   value="Run Notepad.exe with Parameters" 
      onClick="executeCommands(' + hasPARMS + ')" />

What That Does

Here's what this JavaScript does.

First, it accepts a parameter from whatever HTML element calls it. In this case, the JavaScript is executed when the user clicks one of the buttons on the HTML form.

Next, the JavaScript will instantiate the ActiveX object Shell.application. This is the SHELL API. The instantiated object is then assigned to the variable named oShell. Incidentally, you can instantiate most ActiveX objects from within HTML in this manner.

Next, we'll set the value commandtoRun to the value of the command we want to execute, in this case Notepad.exe. Notice that there are two backslashes (\\) between each path parameter. This is required because, if you only used a single backslash in the path, JavaScript would interpret that single script as an operator. By making the command you wish to execute a variable, you can easily modify your HTML code so that you execute whatever command the user chooses from your HTML form. This will give you a lot of flexibility in your applications.

Next, if the user clicked the button to run NotePad.exe and to pass it a parameter of the name of a file to open, then the variable inputparms will contain the value hasParms, which is defined in the "onClick" event of the second button on this Form. If inputparms is not blank, the script will retrieve the value the user entered in the input field on the form and set the variable commandParms equal to that value.

Finally, we'll execute the command to run, Notepad.exe, by calling the ShellExecute method of the SHELL API. We'll pass the ShellExecute method several parameters, each individually enclosed within double quotation marks ("). The first parameter is the command to be executed, in this case Notepad.exe. The second parameters are the input parameters, if any, to the command. The other parameters are not important to this discussion, but if you're interested in using them, be sure to check out the SHELL documentation on Microsoft's Web site.

Save this file and reopen it in your browser and try it out.

Powerful Functions

Using ActiveX objects from within HTML gives you the power and flexibility you need to code professional and useful applications. Explore the SHELL API and some of the other Win32 APIs and see how you can expand on them to add even more power to your applications.


Sponsored By
ASNA

Why Iredell Memorial Hospital Uses ASNA Visual RPG for Web Development

In an effort to help the most patients, doctors are always looking for the quickest and easiest way to get things done. At Iredell Memorial Hospital in Statesville, NC, the green screen application that accessed patient records and information was just not good enough. Scott Philemon was charged with the task of bringing this valuable information to a browser-based system and he found that AVR was the best way for him to do that. He has created a simple point-and-click system that is intuitive rather than the cumbersome green-screen system with which most doctors were uncomfortable. The most important feature of this new application is that doctors will be able to connect, via a modem, to this application and not have to make the trip into the hospital to check on the status of patient blood tests and other medical procedures.

"I was given this project and told to just find the best way to get it done. I heard java, java, java all the time so I took a couple of introductory classes and realized that I would spend a year just learning it before I would even be able to start on this project. With AVR I can really use my RPG skills to create browser-based applications. The transition was so easy and I'm so pleased with the results that I'm recommending that we do ONLY Web applications in the future."

—Scott Philemon, Iredell Memorial Hospital


ASNA Visual RPG (AVR) for Web, Windows and .NET Development

ASNA Visual RPG (AVR) is an integrated development environment for creating enterprise Web, Windows and .NET applications. Transparent database access; an integrated editor, compiler and debugger; support for emerging standards such as XML and SOAP; and equally powerful Web or Windows deployment possibilities make ASNA Visual RPG the one application development environment you can't afford to ignore! Use your RPG skills to develop Web, Windows and .NET applications today.

Download your FREE trial of AVR today!

http://www.asna.com/downloads.asp



THIS ISSUE
SPONSORED BY:

ASNA
Aldon Computer Group
LANSA
Advanced Systems Group
Profound Logic Software
WorksRight Software


BACK ISSUES

TABLE OF CONTENTS
JavaScript and the SHELL Command

Data Queue Basics

Simplify Java Web App Deployment with WAR Files

The Art of Globbing

Keyed Data Queues: The Key to Flexible Subfiles

Work with Active Jobs from Operations Navigator

Editors
Shannon O'Donnell
Kevin Vandever

Managing Editor
Shannon Pastore

Contributing Editors:
Howard Arner
Joe Hertvik
Ted Holt
David Morris
Richard Shaler

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com



Last Updated: 5/22/02
Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.