Skip to main content

I was also looking for a solution for this. Today I just had some time to play around and found a way to programmatically actually call the functionality from the keychain menu plugin. This solution works perfectly as long as Apple doesn't change the relevant parts in the keychain menu plugin.

#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 _lockScreenMenuHit:NULL];
    
    return 0;
}

Save the above code in a file called main.m. Compile the file in Terminal with:

clang -framework Foundation main.m -o lockscreen

Execute the program by typing:

./lockscreen