Adobe Air - AS3 - Listing files in directory without the extension - actionscript-3

I'm listing files in the "applicationDirectory" and getting the names with the extension,
For example "myVid.mp4".
I would like to get only the filename without the extension(mp4)
Here is my code so far
var subjDir:File = File.applicationDirectory.resolvePath("videos");
var files:Array = subjDir.getDirectoryListing();
var filesName:Array = new Array();
for (var i:uint=0; i<files.length;i++)
{
trace(files[i].name);
trace(files[i].nativePath);
filesName.push({label:files[i].name,fpath:files[i].nativePath});
}
Thank's in advance!

trace (files[i].name.substr(0, files[i].name.lastIndexOf('.')));

This will remove the file extension:
trace(files[i].name.replace(/\.[^\.]*$/,""));

Related

Set the file extension of the temp file

In AIR there is the File.createTempFile() method docs.
Is there a way to change the extension?
Code so far:
var webpage:File = File.createTempFile();
webpage.extension = "html"; // Property is read-only
The simple answer is: no.
You're even not able to set the filename by yourself. As soon as you call File.createTempFile(); it will create a file like fla8121.tmp which is composed of the prefix 'fla' a random 4-digit hex number and finally the '.tmp' file extension.
On windows this file will be created in C:\Users\[Username]\AppData\Local\Temp\
If you want to create a temporary file on your own, you need to do it like this:
var file:File = File.cacheDirectory.resolvePath("test.html");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes("<html>This is a test</html>");
stream.close();
This will create test.html in the same directory as createTempFile()

FileReference save to local issue

My requirement is to save a bunch of files (more than 500) in a single zip file locally using FileReference. I am using ASZip to zip the files. Now the problem is if the number of files are more, then I am not even getting Save as dialog box.
I have tried different combinations of data to see whether it is number of files or file size limitation, but it looks like the script automatically stops (irrespective of number of files), if it can't give me the output within a minute.
This is the code that I am using
//*****test code
var myZip:ASZip = new ASZip (CompressionMethod.GZIP);
var myByteArray:ByteArray = new ByteArray();var pdfFile:PDF;
var newPage:Page;
var printPage:BorderContainer;
for (var i:int=0;i<330;i++)
{
printPage = new BorderContainer();
printPage.visible = false;
printPage.x=0;
printPage.y=0;
printPage.includeInLayout = false;
printPage.width = 816+10;
printPage.height = 1056+23;
this.addElement(printPage);
pdfFile = new PDF(Orientation.PORTRAIT, Unit.INCHES, Size.A3 );
pdfFile.setDisplayMode( Display.FULL_PAGE,Layout.SINGLE_PAGE );
newPage = new Page ( Orientation.PORTRAIT,Unit.INCHES,new Size([816+10,1056+10],"MyFavoriteSize",[8.5+10,11+10],[816/0.125,1056/0.218]));
pdfFile.addPage(newPage);
pdfFile.beginFill(new RGBColor(0xFFFFFF));
pdfFile.textStyle(new RGBColor(0x000000));
pdfFile.addImage(printPage,null,-0.5,-0.5,8.5+4,11.5+4);
myByteArray = pdfFile.save(org.alivepdf.saving.Method.LOCAL);
myZip.addFile(myByteArray,i + ".pdf");
}
Can you please let me know what can be done to fix this issue?
Thanks,
Satish.

Loading files in Android Mobile using AIR and Actionscript 3.0

Im building a Test Maker App. I need some help on loading files in Android mobile. The type i want is the type where the user can look for his file from a file browser. This is the code i have so far but it wont work. My mobile always says "No files were found".
var file = File.applicationStorageDirectory.resolvePath("saved projects");//This is a folder
file.browseForOpen("Open Your File", [new FileFilter("Text Files", "*.txt")]);
file.addEventListener(Event.SELECT, onFileSelected);
public function onFileSelected(e:Event):void {
var stream:FileStream = new FileStream();
stream.open(e.target as File, FileMode.READ);
loadTxt.text = stream.readUTF();
}
Plus additional question: Where is the best location to save files in the ANdroid mobile?
You can write your file manager, it should not be difficult. Scan a directory with method "getDirectoryListing":
var file:File = File.applicationDirectory.resolvePath('myDirectory/');
var filesList:Array = file.getDirectoryListing();
and show a list of files.

FileStream dosen´t work in actionscript 3 (Adobe Air)

I have a little problem here.. The code itself dosen´t get any errors, but it will not write to the .txt file... Hope someone can help me with this problem.
Here is my code:
test_btn.addEventListener(MouseEvent.CLICK, leggtil);
function leggtil(e:MouseEvent) {
var file:File = File.documentsDirectory.resolvePath("test.txt");
var fileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
var outputString:String = "test";
fileStream.writeUTFBytes(outputString);
fileStream.close();
}
I tested the code and it worked on my machine.
I believe that the code is working for you, but you're not aware of where the file has been written. Check your documents folder. In Windows, this would be C:\Users\[your user name]\Documents\. If you browse to that folder, you should see a file there named test.txt.

AIR Native process Error

I try to launch a programm with NativeProcess on Mac.
pathEV="/Applications/MyFolder/MyAppOSX.app"
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var fileEV:File = new File();
fileEV=fileEV.resolvePath(pathEV);
nativeProcessStartupInfo.executable = fileEV;
var process:NativeProcess = new NativeProcess();
process.start(nativeProcessStartupInfo);
But this error appear:
Error #3214: NativeProcessStartupInfo.executable does not specify a valid executable file
Can you help me to solve that?
Thanks
Haven't tested it yet, but I assume you'd need to run open with the path to your application as a parameter, like this:
pathEV:String="/Applications/MyFolder/MyAppOSX.app"
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var fileEV:File = new File();
fileEV=fileEV.resolvePath( '/usr/bin/open' );
nativeProcessStartupInfo.arguments = Vector.<String>([pathEV]);
nativeProcessStartupInfo.executable = fileEV;
var process:NativeProcess = new NativeProcess();
process.start(nativeProcessStartupInfo);
And yes, this definitely needs extendedDesktop rights
FYI: The Vector shizzle is correct, I use it to convert the array to Vector, not as a constructor function
Here's an explanation how to launch .app files from the terminal
Could it be that you need to add <supportedProfiles>extendedDesktop</supportedProfiles> to your manifest file?
Also, I believe.app is not an executable. Its actually just an application bundle within which exists the actual executable. Try pointing to /bin/ls first to make sure all else is OK and then dig inside the .app folder to find your executable.
Why do you need a nativeProcess? Why not just use fileEV.openWithDefaultApplication() ?