Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock screen from command-line as same as Keychain Access does on OS X

Tags:

macos

cocoa

I noticed that /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend and Keychain Access's Lock Screen (open its Preferences, check 'Show keychain status in menu bar' then hit Lock Screen from the status bar icon.) has different functionality. Is there any way to run Keychain Access's Lock Screen from command-line through Terminal.app or Cocoa API?

Mac OS 10.9. Thanks!

like image 323
Nobu Avatar asked Dec 02 '13 00:12

Nobu


People also ask

How do I lock my Mac screen in Terminal?

Press Command+Space to open Spotlight Search. Type “Terminal” in the Search bar, and then click it when it appears in the search results. Your Mac will now be in sleep mode. If you selected the option to require a password to wake it, it is now also effectively locked.

How do I show my Lock Screen on Mac?

Go to Apple menu > System Preferences > Security & Privacy. If the lock at the bottom left is locked, click on it and type in your password. In the General tab, uncheck the box next to Require password. Confirm by clicking Turn Off Screen Lock.

Is keychain password same as Apple ID?

Your login keychain password is normally the same as your user password (the password you use to log in to the computer). At login, if your keychain password somehow differs from your user password, it doesn't automatically unlock, and you're asked to enter the keychain's password.


1 Answers

I just found a solution for this: https://apple.stackexchange.com/a/123738/72534

#import <objc/runtime.h>
#import <Foundation/Foundation.h>

int main () {
    NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/Utilities/Keychain Access.app/Contents/Resources/Keychain.menu"];

    Class principalClass = [bundle principalClass];

    id instance = [[principalClass alloc] init];

    [instance performSelector:@selector(_lockScreenMenuHit:) withObject:nil];

    return 0;
}

Save as lockscreen.m and compile with:

clang -framework Foundation -o lockscreen lockscreen.m

Works perfectly, as it just calls the same routine as the status bar icon.

like image 56
mrArkwright Avatar answered Sep 22 '22 13:09

mrArkwright