How to make an input from command line in JScript? - wsh

How can I read input from the command line in JScript, similar to Pascal's readln?

It sounds like you're asking about Windows Script Host. If you're using cscript.exe to run your scripts, you can work with WScript.StdIn:
WScript.Echo("Enter something");
WScript.Echo("You entered " +WScript.StdIn.ReadLine());
http://msdn.microsoft.com/en-us/library/skwz6sz4(v=VS.85).aspx

Assuming cscript the.js a1 a2 ... you can;
var args = WScript.Arguments;
for (var i= 0; i < args.length; i++) {
WScript.Echo(args(i))
}

It has been like forever that I have looked into Pascal, so I'm not quite sure what ReadLn() exactly does. If you just want to get a line from the user in the command line you may use the WScript.StdIn.ReadLine() method as described here.
But if you want to read from a file, then you may try:
var myFileSysObj = new ActiveXObject("Scripting.FileSystemObject");
var myInputTextStream = myFileSysObj.OpenTextFile("c:\\temp\\test.txt", 1, true);
var myString = myInputTextStream.ReadLine();
myInputTextStream.Close();
from here.

Related

How to put BEGIN PROGRAM-END PROGRAM structure inside a macro?

I wrote a Python script for SPSS that I need to use a couple of times in a syntax. Thus, I would like to avoid repeating BEGIN PROGRAM-END PROGRAM. structure more than once. I decided to place it within a macro but sadly it doesn't work even though there are no bugs neither in a macro nor a script. Code:
define mac_export (!positional !tokens(1))
OUTPUT EXPORT
/CONTENTS EXPORT = visible LAYERS = printsetting
MODELVIEWS = printsetting
/XLSX DOCUMENTFILE = "tables1.xlsx"
OPERATION = createsheet
sheet = !quote(!unquote(!1))
LOCATION = lastcolumn NOTESCAPTIONS = no
!enddefine.
define clean ()
begin program.
import spss, SpssClient
SpssClient.StartClient()
OutputDoc = SpssClient.GetDesignatedOutputDoc()
OutputDoc.SelectAllText()
OutputDoc.Delete()
OutputDoc.SelectAllLogs()
OutputDoc.Delete()
OutputDoc.SelectAllNotes()
OutputDoc.Delete()
OutputDoc.SelectAllTitles()
OutputDoc.Delete()
OutputDoc.SelectAllWarnings()
OutputDoc.Delete()
end program.
!enddefine.
*** REPORT.
ctables
/table
(att1 + att2 + att3)
[s][mean f1.1]
/slabels position = column visible = no
/titles title = "Attitudes".
clean.
mac_export "Attitudes".
ctables
/mrsets countduplicates = no
/table gender > $STATEMENTS [colpct.responses.count f40.0] by age
/slabels position = column
/titles title = "Statements".
clean.
mac_export "Statements".
* AND OTHER TABLES...
When I run this code SPSS stops at the first call of clean macro. What can I do to simplify my code and avoid repeating BEGIN PROGRAM structure?
First of all it's explicitly said at the bottom of the DEFINE-!ENDDEFINE. description that scripts' codes cannot be put inside a macro: https://www.ibm.com/support/knowledgecenter/pl/SSLVMB_25.0.0/statistics_reference_project_ddita/spss/base/syn_define_overview.html. Second, you can use SCRIPT command to run a script from an external file. You can also place this command inside a macro. Concerning BEGIN PROGRAM structure there is an interesting workaround:
First, you have to copy Python code with BEGIN PROGRAM-END PROGRAM clause to a different, new syntax file. Let's assume this file is named Python_cleaning.sps.
Second, you need to replace BEGIN PROGRAM structure inside your clean macro with a simple INSERT line. This command allows to run an external syntax from another. Code:
define clean ()
insert file = "C:\path\Python_cleaning.sps"
syntax = interactive error = stop cd = no encoding = "utf8".
!enddefine.
Finally, you can run Python script in your syntax using clean macro.

How to get a JS script console output in TWebbrowser?

I want to run a javascript code in TWebbrowser and get the console output of it. For instance, if i run this code in the console :
var a = 2; var b = 3; var c = a +b ; console.log('The result is '+ c);
I get this output on console :
The result is 5
The code i use to run a JS script with TWebbrowser is this :
twebbrowser1.navigate('javascript:var a = 2; var b = 3; var c = a +b ; console.log('The result is '+ c);');
It works, but i don't know how to get the console output. Is there a way to do it ?
Thanks in advance !
According to MSDN, it looks like your (Delphi) app would need to implement the IDeveloperConsoleMessageReceiver interface.
Then you can connect an object instance of your IDeveloperConsoleMessageReceiver to the browser's current Document. Query the TWebBrowser.Document property for its IOleCommandTarget interface and then call its Exec() method to issue a IDM_ADDCONSOLEMESSAGERECEIVER command.
Try that, and if you get stuck, try asking again.

What is causing this mvc razor syntax error?

var openWos = #Model.MyMaintenanceDashboard.OpenWorkOrders;
Why is Visual Studio saying this is a syntax error? It runs fine, but what would the proper syntax be?
Update: OpenWos is a javascript variable
If this is a javascript code, then you need to quote it so that you do not get the 'syntax' error.
var openWos = '#Model.MyMaintenanceDashboard.OpenWorkOrders';
If you're setting a C# var, the correct syntax would be without the # in front of Model as you're already in a C# code block:
#{
var openWos = Model.MyMaintenanceDashboard.OpenWorkOrders;
}
Edit:
You've clarified that this is a JavaScript variable. There shouldn't be a syntax error at all, as this is valid Razor. There is a distinct difference between quoting the value as suggested by Stephen, and the code that you posted originally. The type of the JavaScript variable is a string when quoted instead of a number.
For example, including this in a page:
<script>
#{ int z = 10; }
var x = #z;
console.log(x);
console.log(typeof x);
var y = '#z';
console.log(y);
console.log(typeof y);
</script>
Results in the following being logged to the console:
10
number
10
string
And there is no syntax error reported using the variable #z in either instance.
I've found that you can get around this by adding zero:
var openWos = #Model.MyMaintenanceDashboard.OpenWorkOrders + 0;

Appending to lists inside ADT

I am trying to append to a list inside an ADT as follows:
data MyADT = myadt(list[str] s);
m = myadt([]);
m.s += "test";
Which causes an error:
|prompt:///|(0,3,<1,0>,<1,3>): Expected list[str], but got str
?[Advice](http://tutor.rascal-mpl.org/Errors/Static/UnexpectedType/UnexpectedType.html)
Which seems like it should work because this works:
x = [];
x += "test";
Maybe I am missing something here?
Thanks!
Try this as a workaround:
rascal>m.s += ["test"];
MyADT: myadt(["test"])
It looks like a bug.
By the way we will remove the overloading of + to add both elements and concatenate lists and introduce a special operator for adding elements. It's confusing as it is now.

How to get the target path of a shortcut in windows scripting?

From what I have read until now, you can get the TargetPath property of an objet of class Shortcut, which is the result of the method CreateShortcut of WScript.Shell.
But I have not found any way to get the target path of an existing shortcut.
In javascript:
var sh = WScript.CreateObject("WScript.Shell");
var sc = sh.CreateShortcut(shortcutPath);
var targePath = sc.TargetPath;
It took me some time to understand it. So I guess there will be at least one person happy to find the answer here.
by using .path property?
http://www.bigresource.com/VB-Get-the-stored-path-from-a-shortcut--UWlkOW9XKz.html

Resources