Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

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 .