Does anyone know how to set the working directory in JavaScript before?
Code I use for launching an application is this:
// Create an object script
oL = new ActiveXObject("WScript.Shell");
oFile = '"C:/Application.exe"';
oL.run(oFile);
According to MSDN, you should be able to use:
var oL = new ActiveXObject("WScript.Shell");
oL.CurrentDirectory = "C:\\Foo\\Bar";
oFile = '"C:\\Application.exe"';
oL.run(oFile);
...assuming you're running this script in Windows Script Host, in which case you probably ought to make that clear in your question - about 99% of JavaScript programmers only ever use the language in a web browser, where this kind of stuff is only possible under extremely unusual circumstances.
Javascript typically runs in a sandbox that means it doesn't have access to the filesystem anyway, thus setting the cwd is meaningless.
What context are you trying to do this in (website javascript, local script running with Rhino etc.) and what are you trying to achieve?
Javascript dosent have access to your harddrive so why should you be able to set the working directory?
Related
I have the following problem:
We are currently using a script to export data from CAD assemblies. This script is running in the Creo browser, which is currently IE. To access the correct directory the following code is used:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.CreateTextFile(session.GetCurrentDirectory() + ComponentName + ".xml", true);
f.Write(iht.join("\n"));
f.Close();
The Creo bowser is going to be switched to Chrome. Because of this ActiveX is no longer going to work. Is there a way to archive the same result with different code in Chrome?
Creo is not supporting Chrome Plugins, so IE Tab is not an option.
Any help is greatly appreciated!!
There is a non-standard feature:
https://developer.mozilla.org/en-US/docs/Web/API/FileSystem
But again, it is non-standard.
Edit: as written in that link, "This interface will not grant you access to the users filesystem. Instead you will have a "virtual drive" within the browser sandbox."
No. No more ActiveX.
In the past, most (auto)CAD programs came with a builtin LISP editor you could write scripts in. Maybe that is usable to rewrite the export if you find a LISP programmer.
Myself, I would install node.js on a server so you can use their file system module, which is a good replacement for the old active x object. This probably will require you to copy the files to that server though, so your current workflow might change a bit.
I am working on calling a .exe file with a WScript.shell activeX. The file is wkhtmltopdf.exe and it is used to convert a HTML page to a .pdf. Everything is working well when I am just calling C:\wkhtmltopdf.exe in the code. It runs and then closes correctly. But my issue is you need to run it from cmd with the program name then the HTML file name you are reading followed by the .pdf name you want it to be created as.
For example:
c:\wkhtmltopdf.exe c:\PDFTestPage.html c:\TEST.pdf
This will call wkhtmltopdf.exe, read c:\PDFTestPage.html, then create c:\TEST.pdf. Works fine when I type it into cmd.
Does anyone know an activeX that will not just run and .exe but actually execute a command line?
Here is my code that I am currently using.
function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"c:\wkhtmltopdf.exe"');
}
Would really like it to be the following.
function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"c:\wkhtmltopdf.exe c:\PDFTestPage.html c:\TEST.pdf"');
}
Also side note. For some reason I cant launch the .exe from an absolute path. I have to move to the directory and then just type in wkhtmltopdf.exe. The fill path is:
C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe
I really only work with UNIX so I'm not sure about spaces in the path. I can do a chdir with the spaces but I cant use the fill path when executing it. Any information would be helpful. Thank you in advance.
According to the following:
http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx
You should be able to pass the commands directly as part of the strCommand param, you'd probably be better off getting rid of the extra quotes wrapping the entire command and arguments:
function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('c:\wkhtmltopdf.exe c:\PDFTestPage.html c:\TEST.pdf');
}
Also you should be able to handle spaces in paths by wrapping each item in quotes, like so:
function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe" "c:\PDFTestPage.html" "c:\TEST.pdf"');
}
You should also keep in mind whether you want to bWaitOnReturn or not, and which intWindowStyle you need (some executables may benefit from a particular style).
Also just as a cautionary note — it's been a while since I've used WScript.shell — but you may need to escape your backslashes in your paths. So a path may need to look like the following:
objShell.run('"C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf.exe"');
For anyone else that comes across this issue, I had a similar (but slightly different) problem that I thought I'd share.
I too wanted to run a command using the ActiveXObject("WScript.shell. I needed to run a .bat script that would launch Google Chrome to a specific URL.
The JS I had was as follows:
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"C:\\Scripts\\MyChromeBat.bat" MY_URL');
This would properly launch my .bat script which was very simple:
start "" chrome.exe %1
The issue I came across was that MY_URL contained some query parameters and when I used the above JS, the query params would be stripped to an extent. So when I was trying to open
http://localhost:8080/webapp/mypage.html?param1=test¶m2=test2
it would actually open
http://localhost:8080/webapp/mypage.html?param1
The fix turned out to be simple - I had to surround MY_URL in quotes. So I modified the line
objShell.run('"C:\\Scripts\\MyChromeBat.bat" MY_URL');
to be
objShell.run('"C:\\Scripts\\MyChromeBat.bat" "MY_URL"');
i want to ask that if i want to rename a file in javascript, what can i do ? i have try a function which i see it online but cannot work.
function ChangeFileName()
{
var fso, f;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFile("FilePath/MyFile.txt");
f.name = "MyFile.htm";
}
i search online and it says that the ActiveXObject is only available for IE and i intended to use it on mozilla because mozilla comes with ubuntu.
beside this, is there any method that i can rename a file inside the javascript ? thanks in advance for your help .
It is Javascript (in the browser), right?
If you run in the browser it is not allowed for security reasons. I think there is some way to do this using IE and ActiveX but using Pure Javascript I think it is not possible.
But you can do in JScript in the console, for example to delete a single file:
function MoveFile2Desktop(filespec)
{
var fso;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.MoveFile(filespec, "newname");
}
No, you cannot rename a file with javascript. Javascript is not able to interact with the user's computer in any way - it is only intended to be used to interact with the content of the web page it is rendered on.
JavaScript has no built in means to interact with a file system.
A host object might provide such a means.
The host object (window) that is available to JavaScript loaded from a web page in a typical web browser exposes no such object. Web pages are not allowed to edit the disks of people visiting their sites. (The exception is IE, with ActiveX, and some security warnings).
If you were running JavaScript in a browser extension or in a different environment (such as node.js) then it might be possible.
if (window.ActiveXObject) {
try {
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile("C:\\Program Files\\GM4IE\\scripts\\source.txt","C:\\Program Files\\GM4IE\\scripts\\target.txt", 1);
fso = null;
}
catch (e) {
alert (e.message);
}
}
I am getting error :
"Automation server can not create object" on the line where I am creating ActiveXObject instance.
I understand that it's considered very bad to access hard-drive data using javascript but I just need it.
I am using IE8 , Greasemonkey4IE to run my javascript.
Thank you,
Mohit
******************************
function WriteFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile("C:\\source.txt","C:\\target.txt", 1);
}
I've put the above code inside a simple HTML page and it worked perfect.
http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm
You can find the similar code on above mentioned location.
I modified it a bit, tough.
But when I am trying to run it through GreaseMonkey4IE it simply spitting the same error I specified earlier.
I did it guys, but thanks a lot for your quick and helpful replies.
All I did is :
Go to Tools > Internet options > Security > Custom Level
Under the ActiveX controls and plug-ins, select Enable for Initializing and Script ActiveX controls not marked as safe.
Using native JavaScript, no, it is not generally possible to access a local file. Using plugins and extensions like ActiveX, Flash, or Java you can get around this rule, generally with some difficulty.
For some browser and OS specific exceptions to this general rule, you might want to have a look here:
Local file access with javascript
Note that as of late 2012, the FileReader API has been supported in all major browsers and provides a native JavaScript mechanism for accessing local files that the user nominates (via an input element or by dropping them into the browser).
This still cannot be used to access an arbitrary file by name/path as in the examples in the original question.
HTML5 File API has multiple ways to access local files.
window.requestFileSystem allows you to request access to the filesystem. Browser support is very poor on this (Chrome only).
FileReader is the HTML5 FileReader API that allows you to programatically read files that users select through a <input type='file' /> Browser support is better on this.
You should use fallbacks like flash and POST to a server for full file access.
Generally reading arbitary files is considered "cheating the browser" so I you'll either have to use secure HTML5, ActiveX or Flash. All 3 of those require user permissions.
After some research I have found:
var fso = new ActiveXObject("Scripting.FileSystemObject");
//This line will create a xml file on local disk, C drive
fh = fso.CreateTextFile( "C:\\fileName.xml", true);
fh.WriteLine("this is going to be written in fileName.xml");
This is how we can do it.There are other methods also.
Accessing local file system is very bad thing to do but yes we can do it.
Automation server can not create object
To get rid of this error go to Tools → Internet Options → Security → select Internet icon → click Custom level → select Enable for Initialize and script ActiveX controls not marked as safe for scripting.
I have not tested this on any other berowser except IE8, but I am sure it will work.
I asked how to check modify timestamps with BAT files and launch a command based on an if statement and Wimmel asked if I could use VBScript instead of Batch Files. I think this is a grand idea. This leads to another question
Can I access the VBScript functionality with JavaScript, while still being compatible Windows XP to Current? (specifically checking file modify timestamp and running a command depending on how recently modified)
Not sure that it is a good idea, but yes, you can use JavaScript (actually, JScript) instead of VBScript. Just use ActiveXObject class instead of CreateObject function that is used in VBScript to create objects.
Here is a code that reads the file modify timestamp using Windows Scripting and JScript:
var o = new ActiveXObject("Scripting.FileSystemObject");
var file = o.GetFile("c:\\temp\\test.js");
WScript.Echo(file.DateLastModified);
For more information, see JScript documentation and Windows Script Host documentation
Even though there are probably easier ways to achieve what you want to do, I had a go at trying the more theoretical part of your question, and apparently all the things we need are there.
Here is what I tried:
test.js:
WshShell = WScript.CreateObject("WScript.Shell");
var result = WshShell.Run("test.vbs", 0, true);
WSH.Echo(result);
test.vbs:
WSH.Echo "test.vbs"
WSH.Quit 5