Set the file extension of the temp file - actionscript-3

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()

Related

AS3 SharedObject read/write file location change

I’m using the following AS3 code to write and read data in two arrays to a local file, using Animate CC 2019 on Windows 10 and AIR 30.0 for Desktop/Flash (.swf) publishing settings. I use two input text boxes, input1 & input2, to add new data to the arrays.
When I test the FLA, the data file created has a .sol extension and is placed in a folder path:
C:\Users\username\AppData\Roaming\FLA filename\Local Store#SharedObjects\FLA filename.swf\
If I publish and install the program using an .air installer package, the exact same file, in the same folder path, is also accessed by the installed version of the program. Same location is used if I install on another computer running Windows 7, so the file location seems pretty consistent.
Question:
How can I force the code to save to a different location on the local hard drive on Windows? For example, in the documents folder or to create a new folder on the system drive and save the file there? Or, even better, prompt the user to choose the folder and file himself?
Please consider I’m looking for an answer using SharedObject, if possible, and not alternative methods like URLLoader, File, FileStream, FileMode. The reason is this way I can store multiple array contents in a file, without having to deal with the in-file data arrangement. So, I can read back the data for each array easily as shown below.
Thanks in advance
This is the code I use to access the local file:
var datavariable:SharedObject = SharedObject.getLocal("filiename");
var data1:Array = new Array ();
var data2:Array = new Array ();
btn_read.addEventListener(MouseEvent.CLICK, readfromfile);
btn_write.addEventListener(MouseEvent.CLICK, writetofile);
btn_new.addEventListener(MouseEvent.CLICK, newentry);
//To add new data from input text boxes to the arrays:
function newentry(e:Event):void
{
data1.push(input1.text);
data2.push(input2.text);
}
//To write to the local file:
function readfromfile(e:Event):void
{
data1 = datavariable.data.d1
data2 = datavariable.data.d2
}
//To read from the local file:
function writetofile(e:Event):void
{
datavariable.data.d1 = data1
datavariable.data.d2 = data2
datavariable.flush();
}
I don't know of a way of changing the shared object storage location. That mechanism is designed to be abstracted out from the developer.
Since you are using AIR, you can actually forget shared objects, and just write your own files anywhere your app has permission to do so. You can do this using the same format as shared object and don't have to worry about in file data arrangement (you save an object, you read back an object - just like Shared Object does), the only difference is you load/save the file where you choose.
Here is an example:
function writetofile(e:Event):void
{
//create an object that holds your data, this will act the same as the 'data' value of a shared object
var saveObject = {
d1: data1,
d2: data2
}
//using the File and FileStream classes to read/save files
var file:File = File.applicationStorageDirectory.resolvePath("saveData.data"); //or where and whatever you want to store and call the save file
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeObject(saveObject); //write the object to this file
fileStream.close(); //close the File Stream
}
function readfromfile(e:Event):void
{
var file:File = File.applicationStorageDirectory.resolvePath("saveData.data");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var savedObject = fileStream.readObject();
fileStream.close();
data1 = savedObject.d1;
data2 = savedObject.d2;
}
If you want to save complex objects (objects that aren't primitives), you need to register the class first. This goes for shared objects as well. See this answer for example of that.

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.

How to create NEW txt file using Adobe AIR

How to create a REALY NEW text file (.txt) using AS3 adobe AIR. Most articles are writing to EXISTING text file (text file already exist).
like this one:
How to create new File txt by using Adobe Air
Thank you.
//create a reference to the file in the applicationStorage Directory
//(for more directories, look at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
var fileToCreate:File = File.applicationStorageDirectory;
fileToCreate = fileToCreate.resolvePath("myfile.txt");
//create a filestream to write to the file
//the write and/or creating the file is done with the FileMode in the open() function
//look at the table that describes what FileMode does what here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileMode.html
var fileStream:FileStream = new FileStream();
fileStream.open(fileToCreate, FileMode.WRITE);
//write some string to the file
fileStream.writeUTF('this is a string to write to the file');
//finally, close the filestream
fileStream.close();
EDIT: changed filemode from READ to WRITE. READ does not create the file ofcourse :)

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() ?