如何在 OS X GUI 中找到当前登录的用户?

gak*_*gak 12 user-accounts login macos

试图找出特定用户是否登录到机器,特别是使用图形用户界面的用户。

这可以通过命令行吗?

Chr*_*sen 22

图形用户界面:

  • 在“系统偏好设置”中打开“帐户”偏好设置面板。预先选择的用户帐户将是活动用户帐户。
  • 如果快速用户切换处于活动状态,它的额外菜单(菜单栏右侧的菜单)可以配置为显示活动用户的名称。

命令行:

在 C 程序中:

技术问答 QA1133:确定控制台用户登录状态 中的 C 代码显示了如何确定哪个用户拥有活动的 GUI 会话。

例如:

/* Adapted from QA1133:
 *    http://developer.apple.com/mac/library/qa/qa2001/qa1133.html
 */
#include <assert.h>
#include <SystemConfiguration/SystemConfiguration.h>

int main(int argc, char **argv) {
    SCDynamicStoreRef store;
    CFStringRef name;
    uid_t uid;
#define BUFLEN 256
    char buf[BUFLEN];
    Boolean ok;

    store = SCDynamicStoreCreate(NULL, CFSTR("GetConsoleUser"), NULL, NULL);
    assert(store != NULL);
    name = SCDynamicStoreCopyConsoleUser(store, &uid, NULL);
    CFRelease(store);

    if (name != NULL) {
        ok = CFStringGetCString(name, buf, BUFLEN, kCFStringEncodingUTF8);
        assert(ok == true);
        CFRelease(name);
    } else {
        strcpy(buf, "<none>");
    }

    printf("%d %s\n", uid, buf);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Joh*_*n T 8

通过命令行,whousers应工作。