| Subcribe via RSS

Writing to applicationDirectory with Adobe Air

October 27th, 2011 Posted in Adobe Air, flash

I had a lot of painful trouble with flash.filesystem.File functionality the other day. The problem is that File.applicationDirectory is read-only. I understand that it’s read only as on a mac we’re inside the <myair>.app/Contents… folder when we’re executing scripts and it is not very wise to write stuff in there.

Most of the times the File.desktopDirectory or File.documentsDirectory will meet our needs just fine and we can write stuff there. That was not the case for me. I needed an air desktop app which would work like a website or projector with loading data next to itself (from ‘images’ or ‘data’ folder etc.). Also I really needed to write a log out of the app so I can see how people are using it and if it’s app’s problem when it crashed.

Long story short. Here is my solution which is a bit of a hack but not too bad and at least it works ‘unlocking’ the File.applicationDirectory so we can have read/write access to it.

The trick is to resolve path for the applicationDirectory first and according to it’s native path we resolve the folder as File.userDirectory. My code goes like this:


var appPath:File = File.applicationDirectory.resolvePath("");
var fromUserPath:File = File.userDirectory.resolvePath(appPath.nativePath);

fromUserPath will take us to the exact same place as appPath, but with a difference that it is read/write and also you can do fromUserPath.parent.parent.parent to it to get to the folder next to the air app on a mac.

So my code that handles the starting path for my app on win and mac (and debug mode) goes as follows:

if (Capabilities.os.toLowerCase().indexOf("win") > -1){
localPath = File.applicationDirectory.resolvePath("");
}else if (Capabilities.os.toLowerCase().indexOf("mac") > -1){
var appPath:File = File.applicationDirectory.resolvePath("");
var fromUser:File = File.userDirectory.resolvePath(appPath.nativePath);
localPath = fromUser.parent.parent.parent;
}
if(Capabilities.playerType == "Desktop" && Capabilities.isDebugger){
localPath = File.applicationDirectory.resolvePath("");
}

//if you trace localPath.nativePath now you can see that you’re on a directory that your air app is at.

I had a terrible headache with this and could not find a straight answer by googling so I thought I’d give something back to community. Enjoy.

2 Responses to “Writing to applicationDirectory with Adobe Air”

  1. Eero Says:

    Afaik Changing stuff in air app folder invalidates air updating… And why not logging to storage folder?


  2. Tanel Teemusk Says:

    I’m not sure if it invalidates the air updating. If it does then it’s a problem. But this solution focuses more for finding a path next to installation folder, not inside it.
    Sometimes logging to desktop, storage or documents folder is not enough. this was the case with my project, as I had to write log next to the air app so other parts of the application can access it, so basically this was the only way I could see it possible.


Leave a Reply

  • Ads