Posted by slick_o_matic in SWF Studio V3 on Oct 28 2008, 11:25 pm

Hi all,

I am brand new to this app, and I am on a crunch to save a file from a CD Demo to the desktop. There will be a total of 20 files that the user will select through the Flash interface, and they will click a Download button to save. I would ultimately like the Save Dialogue to pop up, with Desktop selected as the target location, but if that is too tricky, just a Save Dialogue will be fine, or even just the copy file action.

Can someone please help me with this? I have code for the target doc to be saved set as fileTarget.

Thanks,
slick


Posted by northcode in SWF Studio V3 on Oct 29 2008, 01:28 am

This should do it. I've attached a ZIP file with a work FLA (version 8) and the SPF and SWF files. Just load the project into SWF Studio and press the Build button to see it in action.


ssCore.init();
ssDefaults.synchronousCommands = true;

save_btn.onRelease = function()
{
   var r = ssCore.App.showFolderBrowser({title:"Select a folder", path:"desktop://"});
   
   if (r.success)
   {
      if (r.result == "")
         ssDebug.trace("no folder selected");
      else
         saveFilesToLocation(r.result); 
   }
   else
   {
      ssDebug.trace("error selecting path: " + r.Error.description);
   }
}

function saveFilesToLocation(path:String)
{
   // this is where you would add code to save the files 
   // that the user selected to the path parameter
   ssCore.App.showMsgBox({prompt:"copy files to \"" + path + "\" here"});
}
The path parameter to App.showFolderBrowser allows you to select the start location. If you leave it out you'll get the Windows default. The desktop:// value I passed is a file moniker. They are like shortcuts to common folder locations that SWF Studio will resolve to the correct location for you (since they could be different on each system).

attachment removed, see newer version below...


Posted by slick_o_matic in SWF Studio V3 on Oct 29 2008, 11:37 am

Thank you, Tim

This is an elegant solution for the Save dialogue, but it doesn't include the core code to actually save the file, based on the filePath var I created within the Flash code. It just brings up the Save File requester.

You commented in your code the place to insert the function to download the file. Can you please tell me what that code would be, assuming that the path to the file to be downloaded would be stored in the variable "filePath". I believe it would be something like:

ssCore.FileSys.copyFile({from:fileTarget, to:r.result});

... but I am afraid I don't really get this code yet.

Thanks a million. I really appreciate the invested time so far, and the speed of the original reply.

slick-o


Posted by northcode in SWF Studio V3 on Oct 29 2008, 03:32 pm

Ahhh, I thought you already had the copying code working and were just trying to get the folder selection dialog working. Here's a complete solution. I added three text files to the folder that the test app will try to copy to the folder you select.


ssCore.init();
ssDefaults.synchronousCommands = true;

var files:Array = new Array;
files.push("startdir://file1.txt");
files.push("startdir://file2.txt");
files.push("startdir://file3.txt");

save_btn.onRelease = function()
{
   var r = ssCore.App.showFolderBrowser({title:"Select a folder", path:"desktop://"});
   
   if (r.success)
   {
      if (r.result == "")
         ssDebug.trace("no folder selected");
      else
         saveFilesToLocation(r.result); 
   }
   else
   {
      ssDebug.trace("error selecting path: " + r.Error.description);
   }
}

function GetFileNameFromPath(path:String)
{
   var a:Array = path.split("\\");
   var fileName:String = a[a.length-1];
   return fileName;
}

function saveFilesToLocation(path:String)
{
   var i:Number;
   var copyCount:Number = 0;
   
   for (i=0; i<files.length; i++)
   {
      var r:Object = ssCore.Shell.resolveMoniker({path:files[i]})
      var fromPath = r.result;
      var fileName = GetFileNameFromPath(fromPath);
      var toPath = path + "\\" + fileName;
      
      var copyResult:Object = ssCore.FileSys.copyFile({from:fromPath, to:toPath, failIfExists:"false"});
      
      if (copyResult.success)
      {
         copyCount++;
         ssDebug.trace("copied " + fromPath + " to " + toPath);
      }
      else
      {
         // the copy failed, find out why
         ssDebug.trace("could not copy " + fromPath + ", reason: " + copyResult.Error.description);
      }
      
   }
   
   ssCore.App.showMsgBox({prompt:"copied " + copyCount + " files to " + path});
}
What you should be able to do is just drop my code into your application and then populate the global "files" array via your UI instead of how I've done it at the top by just calling files.push() a bunch of times. Then when you call the save function (attached to the button) it will save the list of files you created.

attachments: test.zip  


Posted by slick_o_matic in SWF Studio V3 on Oct 29 2008, 09:47 pm

Thanks, Tim. I was able to pull code from your example and get the project working.

One thing that stumped me was the example in the Documentation had paths for nested directories after the startdir separated with "\\". This was causing issues, so I converted it to "//", and it worked...

I must not fully understand when to use them.

Thanks again, for the prompt replies.
Craig


Posted by northcode in SWF Studio V3 on Oct 29 2008, 11:32 pm

You need to read this article I wrote: String Literals vs String Values and find out why you need to use "\\" in your code when what you really want is just a backslash.

The \ character is the only valid path separator on Windows. After you read the article above the whole "\\" thing will makse sense.

The // is just part of the file moniker, like in "http://" the // characters aren't anything special they just have to be there. I could have picked any character(s), I just went with the URI moniker conventions.

If you changed a path from something like startdir://folder\\folder\\file.txt to startdir://folder//folder//file go change it back or you're going to have problems :)


Posted by katea99 in SWF Studio V3 on Jan 13 2010, 07:08 am


QUOTE: from northcode;45805
Ahhh, I thought you already had the copying code working and were just trying to get the folder selection dialog working. Here's a complete solution. I added three text files to the folder that the test app will try to copy to the folder you select.


ssCore.init();
ssDefaults.synchronousCommands = true;

var files:Array = new Array;
files.push("startdir://file1.txt");
files.push("startdir://file2.txt");
files.push("startdir://file3.txt");

save_btn.onRelease = function()
{
   var r = ssCore.App.showFolderBrowser({title:"Select a folder", path:"desktop://"});
   
   if (r.success)
   {
      if (r.result == "")
         ssDebug.trace("no folder selected");
      else
         saveFilesToLocation(r.result); 
   }
   else
   {
      ssDebug.trace("error selecting path: " + r.Error.description);
   }
}

function GetFileNameFromPath(path:String)
{
   var a:Array = path.split("\\");
   var fileName:String = a[a.length-1];
   return fileName;
}

function saveFilesToLocation(path:String)
{
   var i:Number;
   var copyCount:Number = 0;
   
   for (i=0; i<files.length; i++)
   {
      var r:Object = ssCore.Shell.resolveMoniker({path:files[i]})
      var fromPath = r.result;
      var fileName = GetFileNameFromPath(fromPath);
      var toPath = path + "\\" + fileName;
      
      var copyResult:Object = ssCore.FileSys.copyFile({from:fromPath, to:toPath, failIfExists:"false"});
      
      if (copyResult.success)
      {
         copyCount++;
         ssDebug.trace("copied " + fromPath + " to " + toPath);
      }
      else
      {
         // the copy failed, find out why
         ssDebug.trace("could not copy " + fromPath + ", reason: " + copyResult.Error.description);
      }
      
   }
   
   ssCore.App.showMsgBox({prompt:"copied " + copyCount + " files to " + path});
}
What you should be able to do is just drop my code into your application and then populate the global "files" array via your UI instead of how I've done it at the top by just calling files.push() a bunch of times. Then when you call the save function (attached to the button) it will save the list of files you created.



I just downloaded the demo of SWF Studio and tried this, as I want to save files to desktop etc..

I couldn't get this to work ... It just said 'Saved 0 files to desktop'

All I really need is to have a button that performs similar to:

on (release) {
ssCore.Shell.open({path:"startdir://myfile.pdf", verb:"open"});
}

but with the windows box opening so users can decide where to put it. Inititally to desktop.

The PDF opening is simple, just put this on the button and the file opens great.

Is there a way I can do this with the save to desktop?

This program does look very good, but it seems you need to be a programmer to use it. There's hardly ay documentation or examples.

Any help would be great

Thanks


Posted by northcode in SWF Studio V3 on Jan 13 2010, 10:51 am

You do need to pick up some programming skills if you want to move what can be done simply (like opening a PDF file). However, it's not that hard. The example code you grabbed should have worked. I'm guessing you didn't know that you have to select the FileSys plugin on the Plugins Tab if you want to use the FileSys functions.

That's covered in the documentation but it's not in the first couple of pages. It requires an investment of both time and effort to learn a product as complex as SWF Studio. That level of dedication obviously isn't for everyone, that's why we're here answering questions - so you don't have to become a programmer to use SWF Studio.

Do you just need to copy a single file to a user selected location or do you need to copy multiple files from a folder (like in the example code)? If it's just a single file I can simplify that example for you and send you a complete project (FLA, SPF, etc) so you can see how it works and incorporate it into your project. Be sure to mention what version of Flash you're using and whether you need the code in AS2 or AS3.


Posted by katea99 in SWF Studio V3 on Jan 13 2010, 11:28 am


QUOTE: from northcode;49342
You do need to pick up some programming skills if you want to move what can be done simply (like opening a PDF file). However, it's not that hard. The example code you grabbed should have worked. I'm guessing you didn't know that you have to select the FileSys plugin on the Plugins Tab if you want to use the FileSys functions.

That's covered in the documentation but it's not in the first couple of pages. It requires an investment of both time and effort to learn a product as complex as SWF Studio. That level of dedication obviously isn't for everyone, that's why we're here answering questions - so you don't have to become a programmer to use SWF Studio.

Do you just need to copy a single file to a user selected location or do you need to copy multiple files from a folder (like in the example code)? If it's just a single file I can simplify that example for you and send you a complete project (FLA, SPF, etc) so you can see how it works and incorporate it into your project. Be sure to mention what version of Flash you're using and whether you need the code in AS2 or AS3.


Ho Tim,

As I said, the PDF opened up great, so was looking for the same for save to desktop or folder with the box that works in the above, or with the windows save box.

I will have multiple files ... But each one I'm happy to call from a button ... Which is just a case of copying, pasting and changing file name to open.

Thanks


Posted by northcode in SWF Studio V3 on Jan 13 2010, 11:36 am

Okay, so you're only copying one file at a time, not multiple files in a single call. Is that correct? Where are the files going to be stored? In the same folder as your EXE? In a folder beside your EXE? If you can tell me that I can get you an example that's close to exactly what you need, to minimize what you have to do to use it. Don't forget the version of Flash you're using and whether you prefer AS2 or AS3!


Posted by katea99 in SWF Studio V3 on Jan 13 2010, 11:42 am

AS2 .. sorry

Yes, just one file to save to computer at a time please ...

Much like the PDF code that just opens the pdf I tell it to

Thanks!

Oh, and probably in a folder called FILES or something.


Posted by northcode in SWF Studio V3 on Jan 13 2010, 11:44 am

Where are the files going to be stored?

What version of Flash are you using (so you can open the FLA I make)?


Posted by katea99 in SWF Studio V3 on Jan 13 2010, 11:48 am


QUOTE: from northcode;49347
Where are the files going to be stored?

What version of Flash are you using (so you can open the FLA I make)?


CS4 ...


Posted by northcode in SWF Studio V3 on Jan 13 2010, 12:14 pm

Okay, this is about as simple as I can make it.

It's two lines (not one) so I hope you can live with that :)

Download katea99_copyfile.zip and have a look.

test.fla is a movie with one button called copyfile_btn that starts the copy operation.

test.swf is just a published version of the FLA so you can open the SPF and press build to see it all working.

test.spf is a SWF Studio project file that you can use to build test.exe from the supplied SWF.

CopyFile.as is where the magic is. Just put that file in the same folder as your FLA so Flash can find it when you make a reference to the CopyFile object and you're pretty much done.

FILES is a folder that contains test.pdf, the file we're going to be copying.

This is the code that goes in your button release handler to copy a file from the FILES folder (in the same location as your EXE) to the desktop. This doesn't automatically do the copy, it shows the browse for folder dialog and sets the default location to the desktop. The user can change that if they want. If they cancel the dialog, then the file will not be copied.


var cf_obj:CopyFile = new CopyFile;
cf_obj.copyFileToFolder("startdir://FILES\\test.pdf", "desktop://");


The only thing you have to remember to do is put a check beside the FileSys plugin in the Plugins Tab (like I mentioned before) in your project file so SWF Studio includes that functionality in your EXE when you build it.

The startdir:// and desktop:// in the example are file monikers that are just symbolic names for folders that SWF Studio lets you use so you don't have to know the real paths (SWF Studio will figure out the real paths at runtime)


Posted by alexmc in SWF Studio V3 on Jan 30 2010, 12:16 pm

This was a particularly useful thread. Thanks for the time you are taking to make things clear. I used your code in SwishMAX3 and it worked really well to copy my file.

I was wondering if there is a straight forward way to add "renaming" the file to this. So "save as" as well where to?

Thanks very much. I am new to Northcode and really impressed so far.

Alex


Posted by northcode in SWF Studio V3 on Jan 30 2010, 01:47 pm

The example I created for katea99 was designed to use the Folder Browser dialog to allow the folder to be selected but there was no way to set or select the "to" file name. There is another dialog, the File Save dialog, that allows this and would make the code in the AS file a little simpler.

Your version ( alexmc_copyfile.zip) works like this...


var cf_obj:CopyFile = new CopyFile;
cf_obj.copyFile("startdir://FILES\\test.pdf", "desktop://", "newname.pdf", "PDF Files|*.pdf||");


The copyFile methd takes 4 parameters now, the first two are the same as in katea99's example. The third is the default new name of the file (the user can change this) and the last parameter is the filter that's used to determine what types of files the File Save dialog shows. You can read more about filters in the documentation for App.showFileSave.


Posted by alexmc in SWF Studio V3 on Feb 01 2010, 06:46 pm

Thanks so much Tim that is great. Really helpful.

Alex


Posted by ctimossi in SWF Studio V3 on Feb 08 2010, 03:40 pm

Thanks Tim, I used this code too and It works great.
Carlos