Lock Screen?

Hey guys, i noticed in keychain that theres a option to activate a screen lock is there anyway to activate this in a applescript, i searched the dictonary but couldnt find anything on it.

hi sem,

here’s a neat way to access that kind of functionality:

set normalCommand to "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"

set theQuestion to "Do you really want to lock the screen?"

set theAnswer to button returned of (display dialog theQuestion buttons {"OK", "Cancel"} default button "Cancel" with icon note)

if theAnswer is "OK" then
	do shell script normalCommand
end if

you could compile this script and set it as a “keyboard shortcut” if you wanted. you could also remove the warning, but i feel it needs that so that you don’t accidentally log out. what’s neat about this is that it does not close your open applications, but just locks the screen. i also don’t believe you need to have, “fast user switching” enabled in order to do this.

here’s where i found the command (for further reading):


EDITED: to add the “?” to my question.

Thanks that works great, its kinda of like fast user switching but not since i dont have it :S

If you have a password required to wake from screen saver, running the screen saver wil work.

do shell script "open /System/Library/Frameworks/Screensaver.framework/Versions/A/Resources/ScreenSaverEngine.app"

I think you can also script menu extras, like if you had the keychain menu extra shown.

It is simpler (and faster) to start the screensaver this way:

activate application "ScreenSaverEngine"

Thanks Adam. I had found similar code but it wasn’t quite right and didn’t work.

Note that, if the user attempts to dismiss the screen saver immediately after the activate command has been sent, a Process Manager error number -609 [connectionInvalid] may occur: “ScreenSaverEngine got an error: Connection is invalid.”

This can usually be avoided by using the launch command, instead:

launch application "ScreenSaverEngine"

Excellent!! I occasionally get that error when I’ve touched the space bar or a key after invoking a script with “activate” in it and then I do indeed get “Connection is invalid” and hadn’t a clue why. The script is changed. Thanks.