ActionScripts 3 check current file and folder exsist - actionscript-3

How to get current path of a compiled AIR executable application and check a file and folder exist in same location?
I tried to use this code but it doesn't work:
var File1:File = File.applicationDirectory.resolvePath('APPAR-NC.exe');
if (File1.exists)
{
trace("The file exists.");
}
else
{
trace("The file does not exists.")
};

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
// The folder where your app is installed to.
File.applicationDirectory:File
// The same result as above.
new File("app://")
// The same folder as a system path string.
File.applicationDirectory.nativePath:String
// Returns true if file/folder, represented by the File object, exists.
File.exists:Boolean
// Returns true if the path, represented by the File object, is a folder rather than a file.
File.isDirectory:Boolean

Just a small change in your code.
var File1:File = File.applicationDirectory.resolvePath("APPAR-NC.exe");
if (File1.exists)
{
if(File1.isDirectory)
trace("The folder exists.");
else
trace("The file exists.");
}
else
{
trace("The file does not exists.")
};

Related

How to write files to a specific directory?

I'm trying to let the user select a directory and then write files to that directory.
I have this code that lets a user browse for a directory:
var file:flash.filesystem.File = new flash.filesystem.File();
file.browseForDirectory("Select a directory");
file.addEventListener(Event.SELECT, selectHandler);
protected function selectHandler(event:Event):void {
// these contain the path where I want to save files to
Object(fileReference).url;
Object(fileReference).nativePath;
// how do I create a file in that directory?
}
How do I create a file in the directory that the user selects?
Your select handler code does not seem to be correct. You should get the reference of the Folder which is an object of the type File by doing event.currentTarget, and not Object or fileReference.
Next you can create a file using the FileStream class. Your selectHandler code should look like this:
protected function selectHandler(event:Event):void
{
var targetDirectory:File = event.currentTarget as File;
var file:File = targetDirectory.resolvePath("htmlFile.html");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes("Any Text you want to create");
stream.close();
}
Will work in AIR projects.
Hope this answers your question.

Accessing local MP3 id3 properties with ExternalInterface

I need to run a swf inside an Extendscript (java) window in Photoshop. The Extendscript part pops up a Folder Selection dialog and lets user to select a folder. Then it sends the Folder path and the list of the .mp3 files in that folder to the swf file.
The swf can load and play the song
But it can't get the id3 information
The swf is in "Access Network Files Only" mode
I'm pretty sure it is a security issue and wonder if anyone knows of a workaround. Here is my code...
Thanks in advance.
var mp3FileList;
var folder;
var songList;
var songsArray:Array = new Array();
var mp3Array:Array = new Array();
if (ExternalInterface.available) {
ExternalInterface.addCallback("transferSongList", transferSongList);
}
//Extendscript opens a Select Folder dialog
//and sends the folder path and the list of .mp3 files inside the selected folder to this swf
function transferSongList(folder, mp3FileList){
songList=quickReplace(mp3FileList, "%20", " ");
songsArray = songList.split(",");
var mp3File:Sound = new Sound();
mp3File.load(new URLRequest(folder+"/" + songsArray[0]));
mp3File.addEventListener(Event.COMPLETE, parseSound(mp3File));
}
function parseSound(mp3):Function{
return function(e:Event):void {
//IF I REMOVE THE NEXT LINE THE SONG PLAYS
alertbox.text=mp3.id3.songName
mp3.play();
};
}
function quickReplace(source:String, oldString:String, newString:String):String
{
return source.split(oldString).join(newString);
}

Changing the name of a file with FileWriter

I wonder if it is possible to rename a file with FileWriter function. At this point, I can save my file without problem. But I'd like to change her name when saving. Here is my function:
function saveFile() {
var s = fileEntry.name;
var new_name = s.substring(0, s.lastIndexOf(".")) + ".min" + s.substring(s.lastIndexOf("."))
fileEntry.name = new_name;
fileEntry.createWriter(function(fileWriter) {
fileWriter.onerror = function(e) {
console.log("Write failed: " + e.toString());
};
var blob = new Blob([textarea.value]);
fileWriter.truncate(blob.size);
fileWriter.onwriteend = function() {
fileWriter.onwriteend = function(e) {
console.log("Write completed.");
};
// fileWriter.write(blob);
}
}, errorHandler);
}
I guess it's possible, but I can not find how.
Thank you in advance!
To do this you need to use the DirectoryEntry API. To write to a new file you will need to use getFile to create a new file and write the contents into it.
To do this you also need to have access to the directory where you want to create the new file, and also permissions to create a file there. If the directory is within your sandboxed file system, this isn't a problem.
If you want to create the file on the user's file system it is more complicated. The user will need to have given your app access to the folder (via chrome.fileSystem.chooseEntry) and your app will need to have the chrome.fileSystem.directory and chrome.fileSystem.write permissions.

File.copyTo keeps old file name

I try the following code to copy a selected file to the storage directory:
private function onAddFileClick():void
{
m__file = new File();
m__file.addEventListener(Event.SELECT, onFileSelect);
m__file.browseForOpen("Select a sound", [c__filter]);
}
private function onFileSelect(e:Event):void
{
var l__target:File = File.applicationStorageDirectory.resolvePath("test.snd");
m__file.copyTo(l__target, true);
}
The copy works but the target file's name keeps the original file's name. If I try to copy a file name "Kalimba.mp3", the copy will be named "Kalimba.snd" and not "test.snd" as expected. The problem is that after the copy, my reference to the target file does not lead to anything since its nativePath sticks to "test.snd" which does not exist.
I use AIR 3.6 with Flex 4.6.
Renaming is done with File.moveTo().
copy first and then use moveTo() to rename it. Unless just moving it will do it for you! Obviously ;)
So after you copy:
var sourceFile:File = File.applicationStorageDirectory;
sourceFile = sourceFile.resolvePath("Kalimba.snd");
var destination:File = File.applicationStorageDirectory;
destination = destination.resolvePath("test.snd");
try
{
sourceFile.moveTo(destination, true);
}
catch (error:Error)
{
trace("Error:" + error.message);
}

Recursive Folder/Directory Copy with AS3 /Air

Is it possible to use pause/resume function to this??
source.copyTo( destination );
It would be great if you can send it at the earliest.
I found one solution here CookBook from Adobe
private function copyInto(directoryToCopy:File, locationCopyingTo:File):void
{
var directory:Array = directoryToCopy.getDirectoryListing();
for each (var f:File in directory)
{
if (f.isDirectory)
copyInto(f, locationCopyingTo.resolvePath(f.name));
else
f.copyTo(locationCopyingTo.resolvePath(f.name), true);
}
}
Or you could just use the File.copyTo() method:
var source:File = new File();
source.resolvePath( 'sourceFolder' );
var destination:File = new File();
destination.resolvePath( 'destinationFolder' );
source.copyTo( destination );
If the directories are large and you don't want your app to be stuck waiting for the copy, you can use copyToAsync, which will cause the source file to dispatch Event.COMPLETE when the job's done.
Here is the modified code from above if anyone wants to copy the entire directory; empty folders and all. Notice the "copyEmptyFolders" parameter to be used in the arguments.
//Recursivley copies directory.
private static function copyInto(directoryToCopy:File, locationCopyingTo:File, copyEmptyFolders:Boolean=true):void
{
var directory:Array = directoryToCopy.getDirectoryListing();
for each (var f:File in directory)
{
if (f.isDirectory)
{
// Copies a folder whether it is empty or not.
if( copyEmptyFolders ) f.copyTo(locationCopyingTo.resolvePath(f.name), true);
// Recurse thru folder.
copyInto(f, locationCopyingTo.resolvePath(f.name));
}
else
f.copyTo(locationCopyingTo.resolvePath(f.name), true);
}
}