macOS Retrieve (full)username: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: macOS Retrieve (full)username
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Retrieve username | |
| 2 | + | ||
| 3 | + | 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. | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | ... | |
| 7 | + | ||
| 8 | + | Uses | |
| 9 | + | CocoaAll, | |
| 10 | + | ... | |
| 11 | + | ||
| 12 | + | function NSUserName: CFStringRef | |
| 13 | + | external name '_NSUserName'; | |
| 14 | + | ... | |
| 15 | + | ||
| 16 | + | procedure TForm1.MenuItem21Click(Sender: TObject); | |
| 17 | + | var | |
| 18 | + | usernameStr: ShortString; | |
| 19 | + | status: Boolean = false; | |
| 20 | + | begin | |
| 21 | + | status := CFStringGetPascalString(CFStringRef(NSusername),@usernameStr,255,CFStringGetSystemEncoding); | |
| 22 | + | if(status = true) then | |
| 23 | + | ShowMessage(usernameStr) | |
| 24 | + | else | |
| 25 | + | ShowMessage('Error retrieving username'); | |
| 26 | + | end; | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | ## Retrieve full username | |
| 30 | + | ||
| 31 | + | 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. | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | ... | |
| 35 | + | ||
| 36 | + | Uses | |
| 37 | + | CocoaAll, | |
| 38 | + | ... | |
| 39 | + | ||
| 40 | + | function NSFullUserName: CFStringRef | |
| 41 | + | external name '_NSFullUserName'; | |
| 42 | + | ... | |
| 43 | + | ||
| 44 | + | procedure TForm1.MenuItem22Click(Sender: TObject); | |
| 45 | + | var | |
| 46 | + | fullUsernameStr: ShortString; | |
| 47 | + | status: Boolean = false; | |
| 48 | + | begin | |
| 49 | + | status := CFStringGetPascalString(CFStringRef(NSFullUserName),@fullUsernameStr,255,CFStringGetSystemEncoding); | |
| 50 | + | if(status = true) then | |
| 51 | + | ShowMessage(fullUsernameStr) | |
| 52 | + | else | |
| 53 | + | ShowMessage('Error retrieving full username'); | |
| 54 | + | end; | |
| 55 | + | ``` | |
| 56 | + | ||
| 57 | + | ## See also | |
| 58 | + | ||
| 59 | + | - [[Accessing macOS System Information|NSProcessInfo]] | |
| 60 | + | ||
| 61 | + | --- | |
| 62 | + | *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](/wiki-contact).* | |
| 63 | + | Category: Code Snippets | |
| 64 | + | Category: macOS |
