Tong Wang
Tong Wang

Reputation: 1612

Can an AIR application find out what OS it's running on?

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.

Upvotes: 1

Views: 1286

Answers (2)

Amarghosh
Amarghosh

Reputation: 59471

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).

Upvotes: 4

TML
TML

Reputation: 12976

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

Acutally, it turns out this is a duplicate question: Get Current Operating System In Adobe Air

Upvotes: 2

Related Questions