Reading and writing files in Adobe Air

The biggest fan (and only user) of my Adobe Air Tea Timer is Michael. Michael is a big fan of the Tea Timer, but he’s also the biggest critic, as you can see in the review he left it on Softpedia. Three stars for GUI and Four stars for features? Heart braking.

I figured to try and gain at least another .5 stars from Mickey, I thought I’d try a few new features.

The first is saving and loading preferences in a file. So that you can have your favorite brew time lengths ready for next time.

I found it really hard to find any decent documentation on this. The main issue I struggled with was that you cannot write files in the application directory, no matter how hard I tried, it just wouldn’t happen. Then, after my 400th search, I found a post on the Adobe blog stating that you can’t. SO, what you need to do is write files in the application’s storage directory. This took a long time to find and resolve.

In a nutshell, what we do is;

  1. Determine if the preferences file exists in the application storage directory.
  2. If it doesn’t exist, copy one from the application directory.
  3. Write the current settings to that file.
  4. Read the settings on each subsequent load.

Start with checking for the file, copying the empty one and returning it.

Next we write the prefs.

Lastly, we read the prefs.

Its worth noting the use of the Dom Parser in the final gist. This allows us to interact with the XML in the prefs file as we would interact with any DOM document (such as X/HTML.) This means I can safe the prefs in a structure that makes it easy to add / remove data with minimal effort (and no need to parse the file manually.)

The full javascript file for the Tea Timer app can be viewed on Github here. This post wasn’t intended as an in depth how to, but more of a reference for people wanting to interact with files and find the docs suck.

blog comments powered by Disqus