1

I have a hyperlink in my html document as

<a href="file://c:/directory/file.txt">click</a>

When I click on the hyperlink, the specified file opens in the browser itself.

Is it possible to open that file in notepad or sublime text or any text editor which is the default program for the file type (txt in this case). Can I do it programmatically in javascript or jquery. Is there any way to do it?

Note: This is just a stand alone application. I have developed a plugin which searches for patterns, that I have given as input, in all the files in a given source directory and print out all the file names in that directory which contains that pattern. Its printed to a output report html file. I just want to link the file path name to the default editor, so that he can edit it and save. No issue of security. Can it be done?

If not, is there any other way to accomplish this task? I can generate the output report in any format. Pls help.

Aniket
  • 4,926
  • 12
  • 41
  • 54
  • 1
    you can't do this without compromising the browser's security. You'd have to modify the browsers MIME type for TXT and default file associated. – apollosoftware.org Jul 02 '13 at 14:48
  • This is an intranet site or just a local stand-alone app? – Teemu Jul 02 '13 at 14:53
  • If it is not local, you could consider why you need it to open in Notepad. Could it instead open in a textarea? Additionally, this could be advanced upon by allowing the user to make changes then save it - either back to file.txt or, for example, fileN.txt. – redditor Jul 02 '13 at 15:00
  • if you point to it via a instead of a link, you can grab the contents and then open it as a weird mime type using a client-side downloader, allowing the user to pick an opener like notepad to handle the file... – dandavis Jul 02 '13 at 15:17
  • @Teemu. Its just a stand alone application. No issue of security. I have also edited the description of the question. Please look at it and suggest me an approach. – Aniket Jul 03 '13 at 01:50
  • @Aniket If Windows and IE only is OK, parzival's code will do that fine, just replace the Notepad-path with your filepath (variable). If you have other types of files (like .htm) to open in an texteditor, you can give any editorpath and then a filepath as an argument. [`Run()`](http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=VS.84%29.aspx) just executes its arguments at command line. – Teemu Jul 03 '13 at 04:15

4 Answers4

3

As others mentioned, you may not able to do this without compromising your browser's security. But I came across a link that may help you.

<script type="text/javascript" language="javascript">
    function RunFile() {
        WshShell = new ActiveXObject("WScript.Shell");
        WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
    }
</script>

Edit: This opens an empty notepad, and it'll only work on windows. You may use js to distinct between different OS and run their default text editors accordingly, but I wouldn't on that route. Why not use online editors instead?

  • This opens an empty Notepad, why not use the filepath instead? Also for IE only. – Teemu Jul 02 '13 at 14:57
  • Good catch! I didn't realize I had enough rep to comment on the ques. Oops! It should have been a comment. –  Jul 02 '13 at 14:58
  • 1
    Actually I planned to answer something like this, but I was waiting OP's answer to my comment above... If it was a web-site, this probably is not going to work, not even in IE. – Teemu Jul 02 '13 at 15:04
  • @parzival Thank you for the answer. This is a stand alone application. I have developed a plugin which searches for patterns, that I have given as input, in all the files in a given source directory and print out all the file names which contains that pattern. Its printed to a output report html file. I just want to link the file path name to the default editor. No issue of security. Can it be done? – Aniket Jul 03 '13 at 01:17
2

Due to security reasons HTML/JavaScript does'nt have allow to access local file system.

http://en.wikipedia.org/wiki/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. Second, scripts are constrained by the same origin policy: scripts from one web site do not have access to information such as usernames, passwords, or cookies sent to another site

You have to make a server request in order to get the resource.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

You can't do this without compromising the browser's security. You'd have to modify the browsers MIME type for TXT and default program's associated. If it's on your local network, and you're on a domain, and each user has admin privileges on their machines, you could theoretically do a registry hack and change the default settings for opening text files in external editors, but this compromises security, and chances are if you're in a domain network, your admin privileges are stripped from local machines.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • Thanks amit for the answer. This is just a stand alone application. So no issue of security. I have edited the description of the question. Please look at it and suggest me the approach. – Aniket Jul 03 '13 at 01:31
0

I was looking for something similar and found a solution. Maybe someone will find it useful. If you only want to do this on your own computer, it works.

tino
  • 518
  • 5
  • 18