views:

5061

answers:

7

is there any local file manipulation that's been done with javascript? i'm looking for a solution that can be accomplished with no install footprint like requiring AIR.

specifically, i'd like to read the contents from a file and write those contents to another file. at this point i'm not worried about gaining permissions, just assuming i already have full permissions to these files.

+2  A: 

If you're deploying on Windows, the Windows Script Host offers a very useful JScript API to the file system and other local resources. Incorporating WSH scripts into a local web application may not be as elegant as you might wish, however.

Traphicone
I'd like the solution to be os-independent (at least between windows and mac), so the windows script host doesn't satisfy it, unless there is a comparable solution for the mac platform
Jared
+7  A: 

on Firefox you (the programmer) can do this from within a javascript file:

netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");

and you (the browser user) will be prompted to allow access. (for Firefox you just need to do this once every time the browser is started)

If the browser user is someone else, they have to grant permission.

Jason S
+4  A: 

In general, this is not allowed by design. It's a violation of the sandbox.

From Wikipedia -> Javascript -> Security:

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files.

Chase Seibert
Damn. This is stupid, of course. Javascript is supposedly an application-agnostic scripting language. Not every application is a web browser. I came here because I'm interested in scripting Photoshop, for instance. Even if some applications don't provide file access classes, it makes sense to standardize them for those applications where they are appropriate - a standard but optional feature, so experience from one app is transferable even if not universally applicable. What I learn in Photoshop won't be portable even to other Javascript hosts that allow file access.
Steve314
+3  A: 

You may want to take a look at the source code of tiddlywiki.
It uses browser specific methods to save it's modified instance into the local filesystem.

s0laris
+1  A: 

I am currently looking into the best way to do this. The code supplied by Jason S seems to be valid but the script needs to be digitally signed. I'm not sure if a self-signed cert will work here, or if it will generate some kind of error. Only digitally signed scripts can elevate permissions, otherwise it is just denied (you get something like "A script from "http://darktower.eondream.com" was denied UniversalXPConnect privileges.")

I haven't looked into this on IE yet. I wouldn't be terribly surprised to find a similar situation where only digitally signed code can elevate privileges.

Here is the firefox page about digitally signed scripts: http://www.mozilla.org/projects/security/components/signed-scripts.html

RealityGone
A: 

http://www.mozilla.org/projects/security/components/signed-scripts.html

You need not digitally sign the script, if the user enables signed.applets.codebase_principal_support (In about:config set the value to true)

This is explained in the same page under the heading Codebase Principals