WScript.Sleep() ~ WSScript is undefined - javascript

I'm trying to follow some example code from microsofts mdn site..
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("calc");
while (oExec.Status == 0)
{
WScript.Sleep(100);
}
WScript.Echo(oExec.Status);
I'm currently writing a javascript/JScript to run a few batch commands. Everything works fine if I don't use WScript.Sleep(). However, If i try to use it, to prevent locking up the browser, i'm getting an error that WScript is not defined.
I figured that I needed to define it myself. However, I have been searching mdn website all day with no luck. This is my first time using any "windows only" products I could be coompletly overlooking something.
If you want to see the documentation I'm looking through it is located here.
http://msdn.microsoft.com/en-us/library/ateytk4a(v=vs.85).aspx
Any help is greatly appreciated.
Thanks,
Freddy

WScript is an object that is defined when the Javascript is run within the Windows Script Host, aka WSH. The object is not available within the Javascript engine in a web browser.
If you are really trying to produce "batch like" files, then you don't need a browser, and I'd say you probably don't want a browser. You can write your code into a .js file and just run it from the cmd.exe prompt or Explorer window with a double-click.

Change the script language from JavaScript to JScript

Related

Google Apps Script: Referencing the ActiveXObject

I wish to create an Excel object from within App Script. The following resource:
http://www.webdeveloper.com/forum/showthread.php?249479-Javascript-excel-object
shows how to do so using Javascript - I was therefore hopeful that I could use this code within Google Apps Script:
function xlTest() {
var xls = new ActiveXObject ( "Excel.Application" );
var newBook = xls.Workbooks.Add;
newBook.Worksheets.Add;
newBook.Worksheets(1).Activate;
}
When I try to run this function I get the error message:
ReferenceError: "ActiveXObject" is not defined
Hopefully I simply need to add a reference to ActiveXObject, if so could somebody please advise how I add a reference to ActiveXObject from Google Apps Script? Many thanks!
The question has nothing to do with Google Apps Script (or even JavaScript) and should not be tagged as such. You can't simply throw this code into Google Script Editor and expect things to work.
GAS is a JavaScript-based runtime that lives on Google servers, not in your browser. It doesn't have access to BOM (Browser Object Model) Furthermore, ActiveX is not supported in most browsers, including Microsoft Edge. Of course, VBA methods like Sheets.Add won't work for obvious reasons.
If you log the global object in Script Editor, you'll see all of its properties available to you as a developer.
In your browser, the properties of global object will be different:
Please take the time to go through GAS documentation and understand features & limitations https://developers.google.com/apps-script/

How to invoke uft script present in ALM using java script?

My requirement is to execute the UFT scripts based on the input given in html page. I came to know from some of the blogs that Microsoft has stopped vbs script usage in html. for that reason i want to invoke the qtp scripts using java script. I searched for the same and didnt find much information. If anyone can provide the equivalent code in javascript will be helpful. I am in short of time to experiment as the client demo is near.
Dim objQtpApp
Set objQtpApp=Createobject("QuickTest.Application")
objQtpApp.Launch
objQtpApp.Visible=False 'True
varUserName=document.getElementsByName('Username').value
varPassword=document.getElementsByName('Password').value
call QC_Connect(varUserName,varPassword)
Sub QC_Connect(varUserName,varPassword,varCustomsON)
'
objQtpApp.TDConnection.Connect "http://dddd.fed.test.com:7117/qcbin", "Domain", "Project", varUserName, varPassword,False
If objQtpApp.TDConnection.IsConnected Then
objQtpApp.Open "[QualityCenter] Subject\Automated Test Case Development\Test",False
objQtpApp.Test.Environment.Value("varUserName")=varUserName
objQtpApp.Test.Environment.Value("varPassword")=varPassword
objQtpApp.Test.Environment.Value("varCustomsON")=varCustomsON
objQtpApp.Test.Run
objQtpApp.Quit
Else
MsgBox "Cannot connect to Quality Center"
End If
End Sub
Thanks,
Rakesh
Your main problem will be that JavaScript doesn't have an equivalent of CreateObject.
The only browser that supports Microsoft's alternative (new ActiveXObject("...")) is IE (not Edge) and it also supports VBScript so there's no point in porting your code to JavaScript.

Android, Javascript, Rhino, JSON

After long search in repo folders I found rhino1_7R2.jar for Android at http://code.google.com/p/android-scripting/source/browse/rhino/rhino1_7R2.jar Unfortunately 1_7R3 is not there.
The script I'm using uses JSON.stringify function which is not present in 1_7R2. There is the JSON2.js file for Rhino but I don't know the proper way to load it at run time. Documentation and example codes are weak. Should I load it as a string and prepend on the running script? Or there is a better way?
I'm using JavaScript to dynamically evaluate some calculations in a loop. I really want to avoid prepending the JSON2.js every time I call a javascript function. Spent almost one day to find out Rhino has supported JSON object at late version and nobody bothered to port it to Android. Looks like another open source project lacking support.
Should i give up and consider using WebView method? Any ideas?
As I understand, you hava some JavaScript script that you want to run by Rhino. If you want to load another JavaScript file, you can use load function:
load("/your/path/json2.js");
After that call your script can use json2 library.
var testStr = '{"test" : {"a": "aval", "b" : "bval"}}';
var jsonObj = JSON.parse(testStr);
var a = jsonObj.test.a;

Send Javascript code to browser

Is there a way to send javascript commands to an open web running in a browser from the shell?
Let's say I have stackoverflow.com open with Chrome. Well, I'd like to send something like
alert('hi!');
from the shell, with something similar to the following:
$ send -t Chrome -w "stackoverflow.com" -c "alert('hi!')"
I was wondering this, because if I can write alert('hi!') on the javascript console of Chrome, I should be able to do the same with a call somewhere, right?
I've seen node.js but I think is not possible with that, please, let me know if I'm wrong.
I know the question could seem weird but I'm curious, thanks in advance :)
You can send JavaScript to Firefox through the jssh extension.
http://www.croczilla.com/bits_and_pieces/jssh/
This is what the Watir testing framework uses to automate Firefox.
I don't know of an equivalent for Chrome.
For IE seems like you can use good old VBScript: http://www.autohotkey.com/forum/topic7642.html
Worked for me just fine now with IE8. :)
Edit: to open this very question and alert JS value have this code as .vbs file and run it:
Dim oIE
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Visible = 1
oIE.Navigate "http://stackoverflow.com/questions/4992552/send-javascript-code-to-browser/4992812"
Do While (oIE.Busy)
Wscript.Sleep 10
Loop
oIE.Navigate "javascript:alert(fkey);"
I think it is possible if you write and install some browser addon that would receive your signal, and do the job. Interesting question, ill be tracking it.
It is entirely up to the browser to provide this sort of functionality. In fact, the form of your problem isn't specific even to browsers:
I was wondering this, because if I can [provide some sort of input] to [a program] to make it [do something], I should be able to do the same with a call somewhere, right?
Some programs do indeed provide hooks for scripting, but if they don't, you're out of luck. There is certainly no guarantee that if you can do something via the GUI, then you can trigger the same action from a command line call.
Now the fact that most browsers provide some sort of plugin architecture, makes it much more likely that such a plugin exists that will listen for external input in this way, if this functionality is missing in the base product.
However, this is still going to be very specific to the particular model and even version of the browser you want to control - so if it's something you wanted to release into the wild, you'll need to be very specific with your requirements.
You can send arbitrary code to browser through the address bar! For example in AutoHotKey style,
send F6 //focus the address bar
send ctrl+v //given that your code is in clipboard
send enter
Now there is another question. Can we get the return value of the inject code? The answer is YES!
Assign your return value to document.title, and retrieve the window title from your application.
If the return value is too long (eg. a JSON format string), do the trick that
document.title='calculating...';
document.title=returnValue.subString(0,20);
sleep(10);
document.title=returnValue.subString(20,40);
sleep(10);
document.title=returnValue.subString(40,60);
...
document.title='finished';
Hope it works.

Tutorial for using JavaScript on a Desktop

I need to do some scripts in java script.
I am working on it but couldn't find a few solutions to a few problems.
First of all I need a GOOD tutorial, but not for an internet page but for a DESKTOP script.
Things couldn't find out like :
1) I wanted a simple message box in order to debug my program, I used:
var name = prompt("What is your name","Type Name Here");
When running it I get error of "Object expected"
2) Couldn't find how to open a file
Based on your comments, I guess that you are attempting to run a JavaScript file directly on Windows. Double-clicking on a .js file in windows will (probably) run it in Windows Script Host.
The prompt() function will not work this way, since WSH provides a completely different API than browser-embedded engines.
The following code should accomplish your intentions. However if you want anything more than a simple popup, HTAs are the only way to do complex GUIs with JScript on the desktop.
var fso, ws, ts;
fso = new ActiveXObject('Scripting.FileSystemObject');
ws = WScript.CreateObject('WScript.Shell');
var ForWriting= 2;
ts = fso.OpenTextFile('foo.txt', ForWriting, true);
ts.WriteLine(new Date().getTime());
ts.Close();
ws.Popup('Wrote to file!');
var ForReading= 1;
ts = fso.OpenTextFile('foo.txt', ForReading, false);
var fileContents = ts.ReadLine();
ts.Close();
ws.Popup('The file contained: ' + fileContents);
WScript.Quit();
I have to ask: why is JavaScript the right tool for the job? Why not use a scripting language intended to be used this way, such as Python, Ruby, Lua, ... etc?
If you are using Microsoft's JScript (and it sounds like you are), look to the MSDN web site for help. The page here looks fairly good. Google can also help with that.
Assuming you don't mind using Java, you could also use the Mozilla Rhino shell. But it doesn't look like there is a standard way of reading from the console in JavaScript. (presumably since this is not something typically required in a JavaScript application...) The built in JavaScript functions in the shell seem fairly basic, but you can read a file.
There area also examples of using Rhino, which may be helpful. You can interface with the Java API to do whatever else you need to do.
Edit: I wrote this answer a long time ago; today I would use node.js. See their learning page.
The latest prerelease of Opera acts as a runtime for JS applications.
They have tutorials describing how to use it.
I used: var name = prompt("What is your name","Type Name Here");
When running it I get error of "Object expected"
Presumably your runtime doesn't implement prompt that in a way that is compatible with those arguments.
2) Couldn't find how to open a file
This depends on the runtime you use. JS itself doesn't have anything built in to read files (or display a prompt). You need an environment that provides those objects.

Categories

Resources