Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Lock screen with password on a per-use basis System
I like to be able to lock my computer with a password, however, I don't like to have the screensaver require a password every time it is turned on. I have found the Lock Screen from the Keychain menu item to be buggy, and I don't like the extra clutter in my menu bar for just the one function.

Hunting around, I couldn't find anything that did quite what I wanted to, so I wrote this AppleScript to allow you to lock the screen. Then, when you come back from that locked session, it returns to normal screensaver functionality. Simply paste this code into Script Editor and save it as a Stay Open application.
--This application will require the password to wake from
--sleep and then start the screen saver. Once the screen
--saver is finished running, the password is no longer required

global screenSaverOn
on run
    -- Sets computer to require a password
    tell application "System Events"
        tell security preferences
            set require password to wake to true
        end tell
    end tell
    -- Launches the screen saver
    tell application id "com.apple.ScreenSaver.Engine" to launch
    set screenSaverOn to true
end run

-- Polls the system to see if the screen saver is running
on idle
    tell application "System Events"
        -- Polls the system to see if the screen saver is running
        set screenSaverOn to (name of processes) contains "ScreenSaverEngine"
    end tell
    if not screenSaverOn then
        -- Once screensaver is turned off, prompts user to return to not require password. Without this, someone could simply wake the screensaver, cancel the password prompt, and then wake the screensaver again to access the session.
        display dialog "Return to no password required to wake?" buttons {"No", "Yes"} default button 2 giving up after 30 --Change this number to adjust time before prompt disappears
        if the button returned of the result is "Yes" then
            tell application "System Events"
                tell security preferences
                    set require password to wake to false
                end tell
            end tell
            tell me to quit
        end if
    end if
    
    -- delays 5 seconds before next check to reduce CPU load
    return 5
end idle

-- Allows script to be quit cleanly as when scheduled shutdown or logout occurs.
on quit
    continue quit
end quit
[robg adds: I haven't tested this one.]
    •    
  • Currently 1.89 / 5
  You rated: 5 / 5 (9 votes cast)
 
[14,232 views]  

Lock screen with password on a per-use basis | 28 comments | Create New Account
Click here to return to the 'Lock screen with password on a per-use basis' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Lock screen with password on a per-use basis
Authored by: morespace54 on Feb 13, '09 07:52:27AM
Why not simply type (in Terminal):

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
Or if you want it in an Applescript:

tell application "Terminal"
	do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
end tell


[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: djbe on Feb 13, '09 08:01:22AM

Is there a way to do such a 'lock' without my mac logging out from my wireless network and because of that pausing all transfers I have going (torrents, ...)



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: djbe on Feb 13, '09 08:05:42AM

Nevermind, for those wondering:
in network preferences for airport, go to advanced and uncheck "Disconnect from wireless networks when logging out"



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: DarthMagnus on Feb 13, '09 11:18:14PM

I was unaware of this shell script at the time I wrote this, thanks for the tip. However, doesn't the system behave differently under the Fast User Switching to the login screen than it would during the screen saver mode? I'm not looking to suspend my session but just simply achieve requiring a password for accessing the session again. Perhaps someone with more intimate knowledge of the system than I can answer that question here.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: morespace54 on Feb 17, '09 07:26:24AM

Actually, I did set my "Login Options" to display login window as "Name and password" instead of "List of users" (or Automatically Login) and I don't have Fast users switching enabled, so for me, FUS is not an issue.

But a quick test with FUS enabled (with those Login settings) seems to behave just like a screen saver login would (that is, both login name and password shows as blank field).

Just keep in mind that anybody with admin password can unlock your computer and get into your session (since the session is simply suspended), but that's also the case with the screen saver lock option.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: bluebeachram on Feb 13, '09 08:01:48AM

A simpler solution is to just use a combination of Butler [manytricks.com] and MacLock [eternalstorms.at]. By using these you can assign a hotkey, or a separate button (in a dockling) to lock your computer. MacLock also lets you keep all of your windows and programs running and in the same place as they were before.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: morespace54 on Feb 13, '09 08:08:11AM

So does the terminal Suspend session command. ;)
I actually use the Applescript version (with only the Terminal command in it) and assign it to a keyboard shortcut with Quicksilver. Buttler works as well.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: bluebeachram on Feb 14, '09 05:53:13AM

Sorry bout that,
Macloc is actually distributed by ourapples.com...dunno why I was thinking eternal storms :(



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: sadiekiller on Feb 13, '09 08:17:42AM
I did something like this, but I use the bluetooth tool "Proximity" to run apple scripts when my phone (and probably me) gets out of range of the computer. Also if I want it to lock right away I can always just turn bluetooth off on my phone. :P

http://blog.jankovskyapplications.com/?p=19

Guide to do what I did.

[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: iRideSnow on Feb 13, '09 11:26:05AM

Pure genius! I love it!



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: Nem on Feb 13, '09 08:47:51AM

Is this Leopard only? Script doesn't compile for me on Tiger.

"tell security preferences" on line 5 (of actual code) throws an error:

"Expected end of line but found application constant or consideration."


---
Nem W. Schlecht
http://geekmuse.net/



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: DarthMagnus on Feb 13, '09 11:10:48PM

Yes, it is Leopard only. The security preferences abilities for AppleScript were added in 10.5



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: ferret-slayer on Feb 13, '09 09:00:56AM
I just use SizzlingKeys, which lets me assign a key-combo to background my current session, and switch to the login screen. This leaves everything running, and is more practical than a screen-lock if others may be borrowing your computer.

SizzlingKeys is shareware, but this (and most other) functions are available without payment.

http://www.yellowmug.com/sk4it/

The command is "Lock Computer" in the "Extras" tab.
This works regardless of whether you have Fast User Switching enabled.




[ Reply to This | # ]
Locker Widget
Authored by: catinthebox on Feb 13, '09 10:01:39AM
is much easier to use and will not change any settings or kick you off a network, similar to screensaver or sleep password but since you have to manually double-click the widget to activate it won't interrupt or annoy you.

http://www.apple.com/downloads/dashboard/networking_security/locker.html


[ Reply to This | # ]
Why not just use system features
Authored by: MacParc on Feb 13, '09 10:59:59AM

Open Keychain Access.app (within the "Utilities" folder).

Go to ->"Preferences…" ->"General" and activate the "Show Status in Menu Bar" option.

The keychain menu item offers "Lock Screen" which is exactly what you wanted: an occasional password protected screen.


Christian Bürli [macfix.de]


[ Reply to This | # ]
Why not just use system features
Authored by: zane on Feb 13, '09 02:01:26PM
Except the author writes:
I have found the Lock Screen from the Keychain menu item to be buggy, and I don't like the extra clutter in my menu bar for just the one function


[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: kikjou on Feb 13, '09 11:29:22AM

I do something that does not require any additional application. Since screen savers are not really needed to prevent burn-in of LCDs (I am talking about hours not months) I use my screen saver solely for the purpose of locking my screen. In Systems Preferences, under Exposé, I have set the upper left corner to "disable screen saver" and the upper right to "start screen saver". So when I go away from my computer for a short time, I move my mouse in the upper left corner and the (locking) screen saver never comes on. When I go away for longer, I move my mouse in the upper right corner and the screen is locked.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: Peganthyrus on Feb 13, '09 03:44:31PM
I use MarcoPolo to automatically lock my screen whenever I leave the vicinity of my home wireless network. If this is the use-case for "locking the screen sometimes" then you could stop thinking about this ever again.

[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: Xaositek on Feb 13, '09 04:11:15PM

Try this Applescript:

do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: DarthMagnus on Feb 13, '09 11:21:15PM

I've noticed that this script has started to give me errors stating that the script has timed out. I'm working on debugging this but I think that it may be due to the line "giving up after 30" in the display dialog to return to normal screen saving mode. I'll post back when I have a solution.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: Tim.Lance on Feb 14, '09 04:54:35PM

What is meant by "has started to give me errors ...?" Why would it just start and not do so earlier (barring OS changes and whatnot)? So far so good here (10.5.6, 2.4 GHz C2D, 4 GB RAM). Thanks!



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: DarthMagnus on Feb 15, '09 12:45:42PM

The first few days I ran this it worked perfectly. Recently when I return from the screensaver though, I am getting an error "Script event timed out." While this doesn't seem to be causing any problems as far as crashing the system, or even causing the script to stop functioning, it might cause problems for people who have their systems set to log out or shut down automatically. If it seems to be working fine for everyone else maybe (hopefully) it's just me having this problem.

So far I know it was not related to the "giving up after 30" command as I had suspected previously.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: Anonymous on Feb 16, '09 02:49:03PM

Your Mac has issues. First, the Keychain menu item is buggy, now your own cryptic workaround has started giving you errors.

I think I'll pass on your tips, thanks all the same!



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: DarthMagnus on Feb 19, '09 07:16:26AM

The issues I was having with the script turned out to be caused by MacSpeech Dictate, which I was recently trying out. After adding in a trigger to quit Dictate before locking the screen, the error went away.



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: MotherBrain on Feb 16, '09 09:57:11AM

How about using the following shell script in automator as a bin/bash shell:

/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend

I don't remember where I got this script but I added a yes/no confirmation dialog box to it explaining you will need to enter credentials to log back in.

I exported it as an application and drug it to my dock. So now I can click on it when I want to lock my computer. Simple, Sweet, Painless. :)



[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: rizzoli on Feb 17, '09 02:38:09AM
Thanks for the hint, it was inspiring, since I was looking for a similar solution to a slightly different problem. When I am home or work I do not want to enter the password to unlock the laptop every time, assuming it's "safe". On the other hand, when I'm on the road, I want the laptop to be locked at max security, in order to give (at least) some hard time to those wo got it from me. My solution was to create two simple applescripts (copied from Apple's website) to lock and unlock the security settings. I have then created two applications using the Script Editor and put them in a folder that I later dropped on my Dock.

Here you are the scripts, in case you're interested and you don't bother to look it up on Apple's website:

Lock system
tell application "System Events"
tell security preferences
get properties
-- returns: {require password to wake:false, class:security preferences object, secure virtual memory:false, require password to unlock:false, automatic login:false, log out when inactive:false, log out when inactive interval:60}
set properties to {require password to wake:true, require password to unlock:true, automatic login:false, log out when inactive:true, log out when inactive interval:60}
end tell
end tell

Unlock system
tell application "System Events"
tell security preferences
get properties
-- returns: {require password to wake:false, class:security preferences object, secure virtual memory:false, require password to unlock:false, automatic login:false, log out when inactive:false, log out when inactive interval:60}
set properties to {require password to wake:false, require password to unlock:false, automatic login:true, log out when inactive:false, log out when inactive interval:60}
end tell
end tell


[ Reply to This | # ]

Lock screen with password on a per-use basis
Authored by: john11 on Feb 17, '09 10:06:51AM
Transparent Screen Lock enables IT professionals to prevent unauthorized system access, while maintaining the ability to view applications running on the desktop for monitoring purposes.

Transparent Screen Lock PRO delivers system security measures that are essential in today's open-concept corporate offices with free movement of employees and visitors, as well as in high security environments such as financial institutions and banks, hospitals and pharmaceutical research facilities, government offices and military installations.

With its ability to limit system access to authorized individuals in both stand-alone and multiple-user shared computer environments, Transparent Screen Lock PRO is ideal for facilities which must ensure compliance to the 21 CFR Part 11 code of federal regulations.

With new optional support for a hardware proximity sensor, TSL-PRO delivers maximum security by automatically locking the system when the user steps away from the station. when the user approaches the system, TSL-PRO's password-protected logon screen is automatically displayed.
-----------
john11
http://www.fastrealestate.net - real estate

[ Reply to This | # ]
Lock screen with password on a per-use basis
Authored by: lihtox on Feb 18, '09 08:04:47PM

As far as I can tell, this product is for Windows only, and therefore the comment is probably spam. I've reported it as such.



[ Reply to This | # ]