Sign in Join
Wiki copytemporary

macOS Retrieve (full)username

Retrieve username

The code snippet below will retrieve the current user's username (eg joe). In the example code below, the function to retrieve the username is called from a menu item.

pascal
...

Uses
  CocoaAll,
...

function NSUserName: CFStringRef
   external name '_NSUserName';
...

procedure TForm1.MenuItem21Click(Sender: TObject);
var
  usernameStr: ShortString;
  status: Boolean = false;
begin
  status := CFStringGetPascalString(CFStringRef(NSusername),@usernameStr,255,CFStringGetSystemEncoding);
  if(status = true) then
     ShowMessage(usernameStr)
  else
     ShowMessage('Error retrieving username');
end;

Retrieve full username

The code snippet below will retrieve the current user's full username (eg Joe Bloggs). In the example code below, the function to retrieve the full username is called from a menu item.

pascal
...

Uses
  CocoaAll,
...

function NSFullUserName: CFStringRef
   external name '_NSFullUserName';
...

procedure TForm1.MenuItem22Click(Sender: TObject);
var
  fullUsernameStr: ShortString;
  status: Boolean = false;
begin
  status := CFStringGetPascalString(CFStringRef(NSFullUserName),@fullUsernameStr,255,CFStringGetSystemEncoding);
  if(status = true) then
     ShowMessage(fullUsernameStr)
  else
     ShowMessage('Error retrieving full username');
end;

See also


From the Free Pascal wiki, page "macOS Retrieve (full)username", as it stood on 2020-06-13 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
TrevNew macOS content :-
TrevCategorised page (minor)
TrevAdded another category (minor)
TrevSee also: New section added

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.