Using AppleScript to lock screen I need an applescript which locks my Mac in the same way pressing shift+ctrl+eject would. I tried two different approaches, but both failed. The first one was to search the internet for a script, which failed. There are only solutions with screensaver or other applications, which I don’t want. I just want to use what I described. The second one was to tell applescript to press the three keys which didn’t work. If someone could tell me how to lock my Mac or how to tell applescript to press three keys, I would be really happy, because I am getting kinda’ frustrated.
Getting information from this answer and this answer:


Press the Spotlight button at the top right of the screen (Cmd + Space)
Search Keychain Access. Open this
Open Preferences (Cmd + ,(comma))
Check Show Status in Menu Bar
Press the Spotlight button at the top right of the screen (Cmd + Space)
Search Automator. Select this
Create new Service
Search Run Applescript next to the Actions and Variables buttons. Double click this
Make sure the checkboxes are Service receives no input in any application
Paste this Applescript:




tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "Keychain menu extra")
        click
        click menu item "Lock Screen" of menu 1
    end tell
end tell


Save this Automator as something like Lock screen (note: code in GIF is different!)





To make a shortcut:


Press the Apple button at the top left of the screen
Click System Preferences
Click Keyboard
Click Keyboard Shortcuts tab
Click Services
Find the name of the service you just created
Click the blank space on the right
Type your shortcut in


Use your shortcut in any application!





OR...:


You could buy Alfred
You could use Cmd+Shift+Eject