As if Flash Lite development combined with FMS wasn't enough fun already I decided to start yet another side project, this time using AIR. The idea behind the project is to build (yet another) webcam snapshot tool - but with a twist.

I use Freeagent for my company accounts (which I can highly recommend btw - just note that multiple currency support is in the works and not yet supported - sorry to be harping on about this Olly ;-) and I would like to have a little tool that lets me capture, upload and attach receipts and other paper related snippets to specific transactions. Luckily, Freeagent have a cool API which I think will allow me to achieve what I want.

In short, you should eventually load up my AIR app, bring up a list of your company transactions and then by simply holding a receipt in front of the camera and pressing the space bar on your keyboard it will capture, save, upload and attach that image to the selected transaction. That surely must be easier than having to scan, save and upload the same receipt using traditional upload methods.
At worst I'll never finish it and waste a few days of my life but what the heck.

Ok, back on track to finally add some value to this post. While messing around with this application I couldn't quite figure out how in AIR I could pre-populate the File Save-As dialogue. I didn't want to the user to choose just any old name for the file (but they still could if they like to), and I also needed to add the default file extension to the image, in this case .png. So after some Tweeting (thanks @weyert) and Googling I came up with this - dead simple, but as usual only if you know how :-)

view plain
1//called when user presses save button in my app
2private function onSave(e:MouseEvent):void
3{
4 //get a reference to the desktop and suggets a filename plus extension
5
6var f:File = File.desktopDirectory.resolvePath('img.png');
7 //listen for the select event
8
9f.addEventListener(Event.SELECT, onBrowse);
10 //open the browse for save dialogue
11
12f.browseForSave("Save Image");
13}
14
15// actually save the image
16
17private function onBrowse(e:Event):void
18{            
19// stuff from other parts of my app
20
21var pngenc:PNGEncoder = new PNGEncoder();
22var ba:ByteArray = pngenc.encode(bmd);
23
24// grab the file that the user chose - hopefully as suggested by me
25
26var file:File = File(e.target);
27var fileStream:FileStream = new FileStream();
28
29try{
30fileStream.open(file,FileMode.WRITE);
31fileStream.writeBytes(ba);                    
32 //close the file
33
34fileStream.close();                
35 }
36 catch(e:Error)
37 {                
38 trace(e.message);                
39 }
40}

There. Documented here for your pleasure.
Oh, and while you're here: why not subscribe to my RSS Feed? I mean how hard can it really be to reach 1000 readers :)