I know it is possible to open files with their default application using the openWithDefaultApplication method of the file class. However, when you try to open an un-associated file (perhaps a proprietary or custom extension) using this method, a runtime error is thrown.
Is there anyway to prompt the user to open a file with a particular application like Windows does with it's "Open with..." dialog?
If not using the AIR the framework, via the NativeProcessAPI?
SOLUTION - using Pixel Elephants answer below as a foundation:
var processInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
processInfo.workingDirectory = file.parent;
processInfo.executable = new File("C:\\WINDOWS\\system32\\cmd.exe");
var args:Vector.<String> = new Vector.<String>();
args.push("/c");
args.push(file.name);
processInfo.arguments = args;
var process:NativeProcess = new NativeProcess();
try
{
process.start(processInfo);
}
catch(e:Error)
{
//give up - open the folder
file.parent.openWithDefaultApplication();
}
The "Open with..." dialog on Windows shows whenever you try to launch an application that does not have an associate file type. Launch the application with NativeProcess.start() and Windows will take care of either opening it with the correct program, or presenting a selection screen if there is no file type associated.
Alternatively, depending on what you are trying to do, you may be interested in associating file types with your AIR application. For instance, you can associate PNG files with your AIR app so that they will by default open in your AIR app (e.g. double-clicking a PNG file will launch your app with an INVOKE event specifying the PNG file that was opened). See http://livedocs.adobe.com/flex/3/html/help.html?content=File_formats_1.html under "Declaring file type associations" for more information.
Of course, your AIR app may have no capabilities to handle opening whatever file you are trying to open and you want a different program to handle it, in which case you can just use the first method which will let the operating system take care of launching the application with the correct program.
EDIT :
I got confused as to what Native Process actually does. NativeProcess launches executables - NOT files, so the method above won't work.
Instead of trying to open the file directly with NativeProcess, try opening the program that you want to open the file with and pass the file in as an argument. For instance, if you want to open a PNG file with some special image program, you would do something like this:
var imageEditorProgram:File = new File("C:/Path/To/Program.exe");
var args:Vector.<String> = new Vector.<String>();
args.push("C:/Path/To/Image.png");
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = imageEditorProgram;
nativeProcessStartupInfo.arguments = args;
var nativeProcess:NativeProcess = new NativeProcess();
nativeProcess.start(nativeProcessStartupInfo);
This should open the image editing program with the image file passed in.
However, the downside with this method is that you can't just let the operating system determine which program to open the file with. You should be able to open a file browse dialog and allow the user to select the program to open the file with (similar to how the operating system does it). So, hopefully the following would do what you want:
var file:File = File.userDirectory;
file.addEventListener(Event.SELECT, selectHandler);
file.browse();
private function selectHandler(e:Event):void{
file.removeEventListener(Event.SELECT, selectHandler);
var filePath:String= file.nativePath;
var imageEditorProgram:File = new File(filePath);
var args:Vector.<String> = new Vector.<String>();
args.push("C:/Path/To/Image.png");
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = imageEditorProgram;
nativeProcessStartupInfo.arguments = args;
var nativeProcess:NativeProcess = new NativeProcess();
nativeProcess.start(nativeProcessStartupInfo);
}
I haven't tested the above code, but it should open a file browse dialog allowing the user to select a program to open the file with (perhaps you should add a FileFilter to restrict the user to selecting only .exe), and then launch that program using NativeProcess.
For user-friendliness you should consider saving user preferences for filetypes (so instead of forcing them to go through the file browser every time they want to open a PNG, just remember that they like to open PNG's with AwesomeImageEditor.exe).
Related
I want to add my FileFilter in as3. Because there is the main problem of extension typing. I already use this option
(saveFile.save(bytes,_dbookNameFill+".doc"));
but I want to automatically generate this extension when I save my file any names. Please Help Me I am Mini Programmer.
var bytes:ByteArray = document.save(Method.LOCAL);
var saveFile:FileReference = new FileReference()
var _dbookNameFill:String =
QSTPreviwForStudentMC._bookNameMc.bookNtxt.text;
var fileFilter:FileFilter=new FileFilter("*.doc","*.text;*.RTF;");
saveFile.save(bytes,_dbookNameFill+".doc");
I'm afraid this won't work. Using AS3's FileFilter class you're just able to limit
what's being displayed on a file dialog you've opened using Filereference.browse().
FileReference.save() isn't affected by a FileFilter and as far as I know there is
no way to force a particular file extension - perhaps due to security reasons.
I developed an Adobe Air App for a small intranet. All computers have been running Windows 7, but now are beginning to be replaced with Windows 10 systems. I can access the mapped drive "I" and the local "C" drive using the file class on Windows 7 machines, but only the mapped drive "I" on Windows 10.
Edit: Capabilities.localFileReadDisable returns false on both Windows 7 and Windows 10 systems.
****I could bypass the need for the local file if Air could get any specific information about the machine it is running on, serial number, mac address, computer name, etc. It really makes no difference what information I get, it just has to be unique to that computer. And using cookies isn't an option because they are volatile****
The following code accomplishes two things.
First, it displays the running version of the Air file and looks for a file on a mapped drive with the latest version available. If they are the same, the computer is running the latest version. If they aren't the same, the new version is displayed to the user, indicating the app should be updated.
Second, it grabs the name of the specific computer from a text file residing on the local drive. That name is used on reports to indicate which computer was being used. There is probably a far superior way to accomplish this, but on Windows 7, it works perfectly for me. Unfortunately, Windows 10 throws an error when trying to access the file on the local drive.
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: file:///C:/machineName.txt
Any help would be greatly appreciated.
var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
var ns:Namespace = appXML.namespace();
version_txt.text = "V"+appXML.ns::versionNumber;
// Define path to the version number
var updatePath:URLRequest = new URLRequest("file:///I:/air/update.txt");
// Define path to name of specific pc
var machineName:URLRequest = new URLRequest("file:///C:/machineName.txt");
// Define the URLLoaders
var updateLoader:URLLoader = new URLLoader();
function checkUpdate():void{
updateLoader.load(updatePath);
}
var nameLoader:URLLoader = new URLLoader();
function checkName():void{
nameLoader.load(machineName);
}
// Listen for when the file has finished loading.
updateLoader.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(e:Event):void
{
// The output of the text file is available via the data property
// of URLLoader.
if(Number(appXML.ns::versionNumber)<Number(updateLoader.data)){
update_txt.text = "UPDATE TO V"+updateLoader.data;
}
}
nameLoader.addEventListener(Event.COMPLETE, nameComplete);
var name_txt:String = new String;
function nameComplete(e:Event):void{
name_txt = nameLoader.data;
var holder:String = version_txt.text;
version_txt.text = name_txt+" ** "+holder;
}
I am trying to write a line of text to a text file. On the emulator or running from VS on the device, it works perfectly but when downloaded from the store, this code emits the error:
System.UnauthorizedAccessException: Access to the path
'C:\Data\Programs\{XXXXXX-XXXXX-XXX-XXXXX}\Install\Data\results.csv' is denied.
Here is my code:
var path = "Data/results.csv";
var uri = new Uri(path, UriKind.RelativeOrAbsolute).ToString();
using (var rd = new StreamWriter(uri, true))
{
var line = String.Format("{0};{1}", field1, field2);
rd.WriteLine(line);
rd.Close();
}
Am I doing something wrong? How can the code work on development?
Use:
var iso = IsolatedStorageFile.GetUserStoreForApplication();
It gets the storage that your app can use on users phone. You can then create directories (or files) for example:
iso.CreateDirectory("Data");
Why it works in emulator or on your device? I'd guess because MS does not care about access rights here.
I am looking for a quick and easy way to store variables in a text file, rather than use amfphp to connect to a database all I need to do is increment values every time a button is clicked .
Would be better if all the vars were in the same text file but if I have to have one per var that would be ok.
If your app is a desktop one, you can user AIR's File and FileStream classes in APPEND mode, like this:
// create the file reference
var file:File = File.documentsDirectory;
file = file.resolvePath("air_tests/saved_by_AIR.txt");
// create a stream object to read/write, and open in in APPEND mode
var stream:FileStream = new FileStream();
stream.open(file, FileMode.APPEND);
// add a new line to the text file
stream.writeUTFBytes( (new Date()).toString() + "\n" );
Desktop apps can use AIR's File and FileStream classes.
See protozoo's example for that.
However, if encryption is necessary, use the EncryptedLocalStore class.
var myVar:int=100; //This is what you'll increemnt.
var bytesToWrite:ByteArray=new ByteArray();
bytesToWrite.writeInt(myVar);
EncryptedLocalStore.setItem("myVar", bytesToWrite);
To retreive the value,
var storedValue:ByteArray = EncryptedLocalStore.getItem("myVar");
var readVar:int=storedValue.readInt();
You won't be able to save a file without a file dialog popping up (perhaps in AIR, but I haven't tested it), so this can quickly get annoying. If it's only for internal use - as in you don't need to access it outside of flash - then check out the SharedObject class instead: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html
Works pretty much like a normal Object, it's just persisted over multiple plays
How to put\save files into your application directory? (adobe air) (code example, please)
It's not recomended but it is possible. Construct your File reference like this:
var pathToFile:String = File.applicationDirectory.resolvePath('file.txt').nativePath;
var someFile:File = new File(pathToFile);
You can't write to your AIR app's Application Directory, it's not allowed. You can however write to a folder that your AIR app creates in the user's directory, called the Application Storage Directory. If you need config files and the like, that's probably the best place to put them. See 'applicationDirectory' in the docs link below:
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/
#glendon
if you try to save directly to applicationDirectory it will indeed throw an error, but it seems you can move the file in the filesystem. i used the code below after yours:
var sourceFile:File = File.applicationStorageDirectory.resolvePath ("file.txt");
var pathToFile:String = File.applicationDirectory.resolvePath ('file.txt').nativePath;
var destination:File = new File (pathToFile);
sourceFile.moveTo (destination, true);
the reason why you 'shouldnt' use the application folder is because not all users have rights to save files in such folder, while everyone will in applicationStorageDirectory.
The accepted answer works!
But if I do this instead:
var vFile = File.applicationDirectory.resolvePath('file.txt');
var vStream = new FileStream();
vStream.open(vFile, FileMode.WRITE);
vStream.writeUTFBytes("Hello World");
vStream.close();
It will give SecurityError: fileWriteResource. However, if I use applicationStorageDirectory instead, the above code will work. It'll only NOT work if it's applicationDirectory. Moreover, Adobe's documentation also says that an AIR app cannot write to its applicationDirectory.
Now, I wonder if it's a bug on Adobe's part that they allow writing to the applicationDirectory using the way suggested by the accepted answer.
try this.
var objFile:File = new File(“file:///”+File.applicationDirectory.resolvePath(strFilePath).nativePath);
the output would be like this…
file:///c:\del\userConf.xml
This will work fine.
If you want write file into ApplicationDirectory, right?
Please don't forget for write for nativeprocess via powershell with registry key for your currect adobe application ( example: C:\Program Files (x86)\AirApp\AirApp.exe with RunAsAdmin )
nativeprocess saves new registry file
AirApp will restarts into RunASAdmin
AirApp can be writable possible with file :)
Don't worry!
I know that trick like sometimes application write frist via registry file and calls powershell by writing nativeprocess into registry file into registry structures.
Look like my suggestion from adobe system boards / forum was better than access problem with writing stream with file :)
I hope you because you know my nice trick with nativeprocess via powershell + regedit /s \AirApp.reg
and AirApp changes into administratived AirApp than it works fine with Administratived mode :)
Than your solution should write and you try - Make sure for your writing process by AirApp.
this function gives your current air application folder which bypasses the security problem:
function SWFName(): String {
var swfName: String;
var mySWF = new File(this.loaderInfo.url).nativePath;
swfName= this.loaderInfo.loaderURL;
swfName = swfName.slice(swfName.lastIndexOf("/") + 1); // Extract the filename from the url
swfName = new URLVariables("path=" + swfName).path; // this is a hack to decode URL-encoded values
mySWF = mySWF.replace(swfName, "");
return mySWF;
}