Gary asked me an interesting question relating to AIR applications. He wanted to know if there was a way for an AIR application to know the username of the current user. I would have guessed that this would be part of the AIR API, but after a bit of searching and asking around, it turns out that this is not the case.
While there isn't a direct API, there is a nice workaround that I found on Stack Overflow (Get the current logged in OS user in Adobe Air). The solution simply assumes that all users have a base directory that includes their username. On my Mac, it is "/Users/ray". On my Windows box (yes, I'm ashamed, I still keep one around), the directory is "c:\documents and settings\administrator". So this technique seems like a good one. You could certainly use it to suggest the current username. Here is a simple demo that Gary cooked up using this technique:
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center" verticalAlign="middle"><mx:Script> <![CDATA[
public function currentOSUser():String { var userDir:String = File.userDirectory.nativePath; var userName:String = userDir.substr(userDir.lastIndexOf(File.separator) + 1); return userName; }
]]> </mx:Script>
<mx:Text text="{currentOSUser()}" fontSize="75" horizontalCenter="true"/> <mx:Text text="is a winner!" fontSize="20" />
</mx:WindowedApplication>
I modified it a bit just to simplify things. Running it on my Mac I see:
On my PC (oh, and I loved how the AIR installer noticed my PC was a bit behind on the AIR SDK and updated itself) it displayed:

Ok, one last tip. Gary was trying to use binding with this method and had trouble getting it working. Let's look at what he did.
public function currentOSUser():String { var userDir:String = File.userDirectory.nativePath; var userName:String = userDir.substr(userDir.lastIndexOf(File.separator) + 1); return userName; }protected function list_creationCompleteHandler(event:FlexEvent):void { getMyUnitResult.token = Widget.getMyUnit("{currentOSUser()}"); }
This didn't work for him. Notice the binding inside the function? That's simply not a place where you can use binding. In this case the solution was simpler code:
getMyUnitResult.token = Widget.gyMyUnit(currentOSUser())
My understanding is that you can only use bindings in the attributes of components. However, don't take my word for it. I found a nice article at Adobe specifically on binding: Using data binding
Hope this helps!
Archived Comments
Nice blog Ray,
I can see this coming in handy for internal air apps.
p.s. add me on twitter 'samhamilton'
Sam.
You can certainly add bindings via actionscript. You just need to use changewatcher or bindingutils. They allow you to add listeners for changes to a property which then fires your handler. An alternate method is to fire the event yourself, then use a meta tag with event="yourEventHere" which will fire once you dispatch the event.
Sweet tip that should work 99% of the time.
Thank you Ray. Let's hope Adobe adds this in the future.
Now, how about one to get the Computer Name. Please....