Running a script behind a locked screensaver

Hi all,

I’m new to the forums, and I was unable to find an existing post to answer this question, but if there is one, I apologize.

I’m trying to run a script using email rules in Mail in Leopard, and I’ve been successful getting the script to run when I’m at the computer. However, for security purposes, the computer is set to lock after a period of inactivity (5 min or so), and when the computer is locked, the script will not run correctly. I am able to launch programs using Automator while the screen is locked, however, anything beyond opening a program won’t work for me. The idea is to allow the tasks to be run while we are away from the office.

My question is: is there a way to run more complicated scripts while the computer remains locked, or would I need to unlock it in the script, with the password written in?

Thanks!

Model: Intel iMac
Operating System: Mac OS X (10.5)

What do you mean by locked? Is the screensaver on and requiring a password to quit, or is the computer in sleep mode?

By locked, I mean the screensaver comes on and in order to access the computer (and quit the screensaver), the password has to be entered. It doesn’t have to be an admin account, but it would be a hassle to maintain a separate account for just this purpose. The computer is set to never go to sleep.

The screensaver is that which comes with OS X, maintained through the system prefs, and not a third-party software.

Hmmm,
I see. I get the same problem, but I find that if I go to the login window instead of the screensaver, the script continues. Hope that helps.

I’m not sure what you mean by going to the login window - you mean activating the window by moving the mouse or a keystroke, and then, while the login prompt is visible, running the script? I tried this but it didn’t seem to work. I’ll try again, though.

And if that works, do you know how I might activate the window through the script?

Try this:

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

repeat 20 times
	delay 1
	set x to x + 1
	set b to button returned of (display dialog "running..." giving up after 3)
	if b is "OK" then exit repeat
end repeat
display dialog x

Wait about 10 seconds, then log back in. If you click “OK” when you get back in, you’ll see that x has incremented during the time you were at the login window.

That did work, but I need to be able to execute the script entirely behind the locked screensaver. I tried manually signing out with

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

before running the rest of the script, and I wasn’t even able to launch the application.

You’d want that line in the script, possibly based on some condition(s).
See this thread: http://bbs.macscripter.net/viewtopic.php?id=26297
about switching users when the system is idle. You could modify the script to -suspend instead of -switchToUserID 502. That would allow you to automatically get to the login screen instead of a screen saver (which you’d want turned off) an then any script(s) that you are running should continue to run.

I tried the script with that line in it, to log out first, and while it did log out, the rest didn’t work - didn’t even launch the application.
Since the main emphasis is on running the scripts remotely, my hope was that I could activate and run them entirely without logging in. So far that seems impossible.

Post the script. As you’ve seen from the one I posted, it is possible to start a script and go to the login screen while the script continues. There must be a problem with your script.

Thanks for all your help.
You know what, I just tried it again, and it did work, the script ran fine and completely while the system was logged out. My bad! :smiley:
I think that will work great, because the login screen is really identical to the locked screensaver.

Thanks!

So as it turns out, my script runs a program which uses the iSight camera for security purposes, and while the script runs great - launching the app and sending an email - if I logout first, the iSight is disabled. But if I don’t logout first, and simply run the script while the screensaver is on and locked, the app is launched and the iSight works, but the email doesn’t send. Bah :frowning:

I did some looking on Apple’s dev forums, and it seems like it’s impossible to have the iSight activated while the user is not logged in.

I think the best solution is going to be setting up a very restricted account, and running it from there.

Thanks again.

Here’s my script, btw -


tell application "Iris"
	activate
end tell
do shell script "/bin/sleep 10"
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
tell application "System Events" to set irisIsRunning to (name of processes) contains "Iris"
if irisIsRunning = true then
	tell application "Mail"
		activate
		set newMessage to make new outgoing message with properties {account:"Gmail", subject:"iSight Armed", content:"Do Not Reply"}
		tell newMessage
			make new to recipient at end of to recipients with properties {address:"*****@gmail.com"}
			set visible to true
		end tell
		send newMessage
	end tell
else
	tell application "Mail"
		activate
		set newMessage to make new outgoing message with properties {account:"Gmail", subject:"Error: iSight NOT Armed", content:"Do Not Reply"}
		tell newMessage
			make new to recipient at end of to recipients with properties {address:"*****@gmail.com"}
			set visible to true
		end tell
		send newMessage
	end tell
end if

EDIT: whoops, posted my disarm script first. This is the correct one.

This might not be the ‘cleanest’ way to do it, but I usually use GUI scripting for this sort of thing.
I have a lot of scripts running over here at night, when the servers are locked with the screensaver. The reason I use this approach is because I need to do some more GUI scripting further on (which does not work when the screensaver is running).

I basically check the process list for a process named ‘ScreenSaverEngine’ and when this exists I log in to the computer and then continue running my scripts.
This way has been working great for me so far.

Here a sample code of the screensaver part:


tell application "System Events"
	set v_proclist to name of every process
end tell

repeat with v_teller from 1 to (count of text items in v_proclist)
	set v_currentapp to (text item v_teller of v_proclist)
	if (v_currentapp is in {"ScreenSaverEngine"}) then
		tell application "System Events"
			delay 2
			keystroke "T"
			delay 2
			keystroke "<thePassword>"
			keystroke tab
			delay 1
			keystroke tab
			delay 1
			keystroke return
		end tell
		exit repeat
	end if
end repeat

delay 2

Notice that I just keystroke the password because the admin user is ok to log in for me. If you want to log in with another user, you first have to keystroke tab with shift down, then keystroke the username, then keystroke tab again.

I don’t know if this will help at all, but this way, the screensaver should be gone and your iSight and mailing app will be working.

Greetz,
caenel