Skip to main content
Based on the comments, the average Apple user needs a little more explanation on how to build this. I've made it totally copy-pasta.
Source Link

Note: This solution is unique in that it uses the Keychain Access menu bar status functionality, but it does not require you to enable the Show keychain status in menu bar option as the AppleScript methods do.

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. You can create a small binary for locking your screen by pasting this into your terminal...

# Do our work in the temporary directory that gets cleaned on boot
cd /tmp

# Create the source file
cat > main.m <<END_OF_FILE

#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 the above code in a file called main.m. Compile the file in Terminal with:


END_OF_FILE

# Compile the source file
clang -framework Foundation main.m -o lockscreen

Execute the program by typing:

./lockscreen

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 performSelector:@selector(_lockScreenMenuHit:) withObject:nil];
    
    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

Note: This solution is unique in that it uses the Keychain Access menu bar status functionality, but it does not require you to enable the Show keychain status in menu bar option as the AppleScript methods do.

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. You can create a small binary for locking your screen by pasting this into your terminal...

# Do our work in the temporary directory that gets cleaned on boot
cd /tmp

# Create the source file
cat > main.m <<END_OF_FILE

#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;
}

END_OF_FILE

# Compile the source file
clang -framework Foundation main.m -o lockscreen

Execute the program by typing:

./lockscreen
Change method call to avoid compile warning
Source Link

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 performSelector:@selector(_lockScreenMenuHit:NULL];) withObject:nil];
    
    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

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

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 performSelector:@selector(_lockScreenMenuHit:) withObject:nil];
    
    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

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

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;
}

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
Source Link
Loading