views:

52

answers:

2

I was wondering if an AIR application can find out what OS it's running on, e.g. Windows XP, Vista, Mac OS, etc. Also, is there a way for it to find out the current OS user name? Thanks.

+1  A: 

Check into flash.system.Capabilities - I believe it has what you're looking for.

Acutally, it turns out this is a duplicate question: http://stackoverflow.com/questions/1062272/get-current-operating-system-in-adobe-air

TML
Thanks. What about the currently logged in OS user?
Tong Wang
I'm pretty sure you can't get that - that information shouldn't be available to flash at all.
TML
+2  A: 

As @TML stated, System.Capabilities.os will get you the operating system. Now I don't know of any direct way to get the user name, but AIR file class has a userDirectory property that will give you a reference to the logged in user's home directory. The nativePath of that object ought to have the logged in user's name.

//user directory path normally ends with the user name like
//xp   : C:\Documents and Settings\userName
//mac  : /Users/userName
//*nix : /home/username or /home/groupname/username

var os:String = System.Capabilities.os;
var usr:String = File.userDirectory.nativePath;
var sep:String = File.separator;
if(usr.charAt(usr.length - 1) == sep)
  usr = usr.substring(0, usr.length - 1);//remove trailing slash
usr = usr.substring(usr.lastIndexOf(sep) + 1);
trace(usr);

Test with various OS's and find if there are any edge cases before using this in production code (like cases where user name is not the last part of the user directory - I am not aware of any, but just in case).

Amarghosh