Launching winword with a file using ActiveX - javascript

How can I launch Word with a document path from activeX like for example:
function RunWord(cmdline, args){
var v;
v = new ActiveXObject("Shell.Application");
v.ShellExecute(cmdline+" "+args);
}
where cmdline is 'Word' that is a shortcut link in one of the system folder and args is the path to file like 'C:\Projects\Schedule.doc'
The problem is it combine the string like 'Word C:\Projects\Schedule.doc' and then give an error saying it does not find this application, however when I launch word alone without args it works ok.
Any ideas ?

Do you need to escaped the backslashes in your filename? I.e. use
'C:\\Projects\\Schedule.doc'
instead of
'C:\Projects\Schedule.doc'

Related

how to download file using jquery in asp.net core?

I use this code to download file using jquery.
$('#dwnlod').click(function (ee) {
e.preventDefault();
var currentFile = $('#SlideContainer .actv').css('background-image').replace(/^url|[\(\)]/g, '');
alert(currentFile)
document.location.href = currentFile;
});
However, it always does nothing. the reason for this is when I remove e.preventDefault to read the URL I find it distorted.
it rather than the result from alert >>
"https://localhost:660066/Uploads/5c92f430-4aef-41c8-b201-853597935771.jpg"
it displays in a new browser tab >>
https://localhost:660066/Documents/Index/"https://localhost:660066/Uploads/5c92f430-4aef-41c8-b201-853597935771.jpg"
I don't know if I need extra steps with asp.net core routing !!?
It looks like the issue is that currentFile returns a string that is surrounded by quotes and thus the browser is interpreting it as a relative URL (since the scheme isn't at the beginning of the string). Removing the start and end quotes if they exist should get you the behavior you are looking for.

Is it possible to run a JavaScript file from a C# file?

When I say JavaScript file, I mean the whole file. Not a function. I've seen ways to run a JavaScript function from c# but nothing about running a file.
According to this question: https://stackoverflow.com/a/1469790/13105088, one could run the command with any normal shell.
string strCmdText;
strCmdText= "/C node myscript.js"; // the command to run from the command prompt
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Note that this will show the Command Prompt on Windows.
This following script prevents that.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C node myscript.js";
process.StartInfo = startInfo;
process.Start();
(these example scripts were both provided by the answer I linked to above.)
In addition, you should probably also note:
Important is that the argument begins with /C otherwise it won't work. How Scott Ferguson said: it "Carries out the command specified by the string and then terminates."
Note: this is all assuming you are referring to NodeJS when you are saying a "JavaScript file", but any other interpreter (eg. /C python3 myfile.py) should also work.

How can I escape a string to ensure that it is a valid string literal in JS source?

I have a Qt application that embeds a web browser (QWebEngineView). I would like to call a javascript function with a string argument from the C++ application. The means of doing this is calling
page()->runJavaScript("setContent(\"hello\");");
This works in simple cases. However, if I try and load, say, a C++ source file and use that as the parameter of setContent, this will break, because I can't simply assemble the string like this:
auto js = QString("setContent(\"%1\");").arg(fileStr);
I tried the following:
fileStr = fileStr.replace('"', "\\\"");
fileStr = fileStr.replace("\n", "\\n");
But apparently this could not escape the string, I get an error when I call this javascript. How can I universally escape a long string with newlines and possible special characters so that I can construct a valid js fragment like this?
So, after some research, I came across QWebChannel which is meant for bi-directional communication between the application and the hosted webpage. The imported qwebchannel.js in the examples can be found here. From there, this is what I did:
In C++:
auto channel = new QWebChannel(this);
page()->setWebChannel(channel);
channel->registerObject("doc", Doc);
In HTML/JS:
new QWebChannel(qt.webChannelTransport,
function(channel) {
var doc = channel.objects.doc; // this is "doc" from the registerObject call
editor.setValue(doc.text);
doc.textChanged.connect(updateText); // textChanged is a signal of the class of doc.
}
);
So, even though this does not directly answer the question, what is presented here can be used to achieve the same effect.

How to Run cmd.exe with parameters from javascript

I am trying to write javascript which should run cmd.exe with a specified command line in it like this docs.google.com/file/d/0B7QHCoQDlEvKWUZSX3oxUDI2SDg/edit:
I prepare a code after reading shellexecute method on microsoft site:
var objShell = new ActiveXObject("Shell.Application");
objShell.ShellExecute("cmd.exe", "C: cd C:\\pr main.exe blablafile.txt auto", "C:\\WINDOWS\\system32", "open", "1");
but it does not insert command line in cmd.exe.
Could anybody help me? Thank you in advance.
Maybe you don't have this ActiveX-control installed (or registered) in your computer.
WScript.Shell should be found in every Windows:
var run=new ActiveXObject('WSCRIPT.Shell').Run("commands to run");
If there are spaces in commands to run, you need to use double quotes.
Edit
The content below is mainly from MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx
iRetVal = Shell.ShellExecute(
sFile,
[ vArguments ],
[ vDirectory ],
[ vOperation ],
[ vShow ]
)
Let's take [vDirectory]. The documentation says: "The fully qualified path of the directory that contains the file specified by sFile. If this parameter is not specified, the current working directory is used."
This means that you have an invalid path for this argument (having .cmd.exe at the end of it). Also all examples for creating the ActiveX are like this:
var objShell = new ActiveXObject("shell.application");
Notice the lowercase in "shell.application".
And May12, thank's for asking this. I didn't know about this ActiveX control before, it seems to be very useful to me.
EDIT II
But have you understood it? Your example works perfect in my app:
objShell.ShellExecute("cmd.exe", "cd C: C:\\cd c:\\ext_file main.exe test.txt", "C:\\WINDOWS\\system32", "open", 1);
With three exceptions:
1) The one I mentioned early in this answer about the path
2) Escaped \ used also in arguments.
3) The last argument is type of number, not a string.
If I understood correctly, you are only interested in calling another file with parameters. This is my example of calling another file from a shortcut or batch file
If there are no spaces in the path
mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /c start /max C:\\Windows\\Notepad.exe',0,false);close()"
With spaces in the path. The double quote is replaced with #
mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /v /c set a=""&call set #=!a:~0,1!&start /max C:\\!#!Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe!#!',1,true);close()"
var objShell = new ActiveXObject("Shell.Application");
objShell.ShellExecute("cmd.exe", "C: cd C:\\pr main.exe blablafile.txt auto", "C:\\WINDOWS\\system32", "open", "1");
is usable

Javascript wshell.run not working properly

I'm using HTA and in it I have a function that should run a command line with wshell.run , If I'm writing this line in Windows 'Run' util it is working fine, I want it to work also in the HTA with wshell.run.
The line is:
C:\xxxx\xxx\xxx.EXE aaa.psl abc
( The names are xxx just in here - not in the real code.. )
In the javascript code I'm using:
function runCmd()
{
wshShell.exec( "C:\xxxx\xxx\xxx.EXE aaa.psl abc" );
}
The error I got is in the xxx.EXE application says "couldn't open aaa.psl File not found".
Thanks,
Rotem
I'm surprised the xxx.EXE program is running at all. You need to escape those backslashes in the command:
wshShell.Exec( "C:\\xxxx\\xxx\\xxx.EXE aaa.psl abc" );
// ^-----^----^--- here
If you're doing the same thing in the aaa.psl filename, that's your problem.
If you're not passing a full path to the aaa.psl file, then most programs (not all) will expect it to be in the current directory, so you'll want to make sure you've set the current directory correctly (although using absolute paths may be a better option).
Here's an example, for instance, of telling Notepad to edit a file:
shell = WScript.CreateObject("WScript.Shell");
shell.Exec("c:\\windows\\system32\\notepad.exe c:\\temp\\temp.txt");
...or via the current directory:
shell = WScript.CreateObject("WScript.Shell");
shell.CurrentDirectory = "c:\\temp";
shell.Exec("c:\\windows\\system32\\notepad.exe temp.txt");
Okkkk T.J. is the man!! :)
I finnaly made it with your help by replacing exec to run:
This is the final (and working) code:
function runCmd()
{
wshShell.CurrentDirectory = "G:\\xxx\\xxx";
wshShell.run( "xxx.EXE xxx.psl abc" );
}

Categories