Solved

NON-BROWSER javascript save file to disk?

Posted on 2004-09-27
17
2,063 Views
Last Modified: 2012-08-14
Hello -

I know that you can't do this in a browser, but I'm hoping that it IS possible to use javascript to do it in. Specifically, I want to assign the script to a link in Acrobat - the files will all be on a CD.

I need to save a file from the CD to the user's hard drive.

Can it be done? 500 points on the line because I need to know ASAP. Ideally, I need a turnkey script too - I'm not wise in the ways of the java...

Thanks in advance!
0
Comment
Question by:amateur6
  • 8
  • 7
17 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 12159466
If you want to copy a file from CD to Disk, then use BAT files for that.
Why do you it to be done in JaveScript? Do you start that operation from a browser page opened from the CD?
0
 
LVL 1

Author Comment

by:amateur6
ID: 12159503
Two things - 1) I need a cross-platform solution (if I had to I could probably use one PDF for each platform) and 2) I don't know if I can get to a BAT from the PDF. Could I call one using a javascript? (Then I could use BAT for PC and AppleScript for Mac)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12159561
JavaScript alone cannot copy files. It needs always some OS side support.
Here is how you would do it on Windows:

var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("CMD /C copy D:\\temp\\file.txt C:\\");

0
 
LVL 1

Author Comment

by:amateur6
ID: 12159701
cool, cool - I'm obviously wrong, because I always thought ActiveX was a browser thing. How far back in Windows versions can I use it?

And presumably that's just a snippet - or is that all the code I'd need? THANKS!
0
 
LVL 1

Author Comment

by:amateur6
ID: 12159726
Oh, sorry - and are CD drives always "D:\\", and/or do I need to reference the name of the CD anywhere? Thanks again!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12159788
The upper snippet is the complette content of a file with the extension .js which can be executed from command line.
The executor of that batch JavaScripts is the WindowsScriptHost engine.
The WSH is embedded since Win98 in the system.

To decide which drive letter is the CD is not trivial because you have to enumerate all drives and look for media type. Also you can have two or more CD drives.
Please explain a bit more what you try to do.

0
 
LVL 1

Author Comment

by:amateur6
ID: 12159941
Sorry, let me try to be more clear.

I'm making a CD which will have a PDF Catalog and separate hi-res image files. Within the PDF I can make a link/button which will execute a javascript (this is part of Acrobat's functionality). I would like that javascript to enable me to allow the user to select a location to save a particular hi-res file to their hard drive from the CD.

The reason I was asking about the drive letter was because it's in the code snippet, which made me wonder "what if the CD's not IN the D drive?" and "how likely is that"? and "is there another way to reference it (ie, by the CD's name)?"

Thank you, thank you, thank you!

I still have the cross-platform problem, but I'll deal with that separately...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 63

Expert Comment

by:Zvonko
ID: 12160057
Ok, then give NO drive letter.
The Acrobat system is supposed to act on the current drive where the Application is running.
Use simply the absolute or the relative directory path on the CD.

0
 
LVL 1

Author Comment

by:amateur6
ID: 12161631
Okay, no luck. And no handy errors or anything - just click = nothing. Does that, in itself, indicate anything?

I used this (probably wrong somewhere):

var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("CMD /C copy images\\red_esquire C:\\");

What did I break? Thanks!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12161805
To get closer do first test with some knowen files. Fo example, copy some test file from one directory on C: to some other directory.
If that works, then try someting like this:

var oExec = WshShell.Exec("CMD /C DIR > C:\\dir.txt");

In the C:\dir.txt you should see the actual application working directory and use it as relative path.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12161834
Oh, also switch your Windows Explorer to show you the file Extensions also for known file types.
I assume your image file name is like this:
var oExec = WshShell.Exec("CMD /C copy images\\red_esquire.gif C:\\");

0
 
LVL 1

Author Comment

by:amateur6
ID: 12161869
Oops, it's actually "red_esquire.jpg" ... let me try that (and eat some lunch).  :-)
0
 
LVL 1

Author Comment

by:amateur6
ID: 12163034
I was hoping it was my bad name or path, but after exhaustive testing (and moving stuff), it's not. Pity.

When you say "For example, copy some test file from one directory on C: to some other directory", how do I go about doing that? As I mentioned, I'm NOT a javascript guy.

And then "var oExec = WshShell.Exec("CMD /C DIR > C:\\dir.txt");" I think I get it - that will write out a directory path? But do I put it after the "var WshShell" line?

Thanks thanks thanks!
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 12164899
First test:

var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("CMD /C copy C:\\app\\images\\red_esquire.jpg C:\\temp");


Second test:

var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("CMD /C DIR > C:\\dir.txt");

After first test you should have a copy of a file from c:\app\images\red_esquire.jpg  copied to c:\temp
Then you know that your copy syntax is correct.

Now you have to know what the drive letter and the application directory path is when you do the copy.
The DIR command tells you the actual directory. You can read it in the file C:\dir.txt
Based on the directory information you can adjust the relative copy command.

0
 
LVL 1

Author Comment

by:amateur6
ID: 12182457
Thanks for all the help!! I'm on to a different project just now, but give me a day or two. AND thanks for your patience!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now