Skip to content

Instantly share code, notes, and snippets.

@cardi
Created April 23, 2016 22:15
Show Gist options
  • Save cardi/3e2b527a2ec819d51916604528986e93 to your computer and use it in GitHub Desktop.
Save cardi/3e2b527a2ec819d51916604528986e93 to your computer and use it in GitHub Desktop.
os x lock screen
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
/*
lockscreen.m
to compile:
clang -framework Foundation lockscreen.m -o lockscreen
then bind a keyboard shortcut (e.g., with Hammerspoon or automator)
to run this program
original post:
http://apple.stackexchange.com/questions/80058/lock-screen-command-one-liner
original authors:
- http://apple.stackexchange.com/users/72534/jnk
- http://apple.stackexchange.com/users/68832/mrb
*/
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment